Skip to content

Commit

Permalink
Add test for deprecated_terse_writes with cpp.Ref{Shared} behavior
Browse files Browse the repository at this point in the history
Summary: Unlike cpp.Ref{Unique}, we serialize non-optional cpp.Ref{Shared} even if it's nullptr.

Reviewed By: thedavekwon

Differential Revision: D68807799

fbshipit-source-id: 95e2657a1c5d8ef06ddaa53c9753a871b23c579e
  • Loading branch information
TJ Yin authored and facebook-github-bot committed Jan 30, 2025
1 parent b578687 commit 721b463
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions thrift/test/StructTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,9 @@ TEST_F(StructTest, TerseFields) {
EXPECT_TRUE(serializedField<ident::exception_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_ref_struct_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_ref_union_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_shared_ref_struct_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_shared_ref_union_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_shared_ref_exception_field>(terse));

// cpp.ref exceptions are nullptr by default
EXPECT_FALSE(serializedField<ident::cpp_ref_exception_field>(terse));
Expand All @@ -842,6 +845,9 @@ TEST_F(StructTest, TerseFields) {
terse.cpp_ref_struct_field() = nullptr;
terse.cpp_ref_union_field() = nullptr;
terse.cpp_ref_exception_field() = nullptr;
terse.cpp_shared_ref_struct_field() = nullptr;
terse.cpp_shared_ref_union_field() = nullptr;
terse.cpp_shared_ref_exception_field() = nullptr;

// Numeric fields are serialized if they don't equal custom default
EXPECT_TRUE(serializedField<ident::bool_field>(terse));
Expand All @@ -864,7 +870,11 @@ TEST_F(StructTest, TerseFields) {
EXPECT_TRUE(serializedField<ident::struct_field>(terse));
EXPECT_TRUE(serializedField<ident::union_field>(terse));
EXPECT_TRUE(serializedField<ident::exception_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_shared_ref_struct_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_shared_ref_union_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_shared_ref_exception_field>(terse));

// @cpp.Ref has special logic that it skips nullptr fields
EXPECT_FALSE(serializedField<ident::cpp_ref_struct_field>(terse));
EXPECT_FALSE(serializedField<ident::cpp_ref_union_field>(terse));
EXPECT_FALSE(serializedField<ident::cpp_ref_exception_field>(terse));
Expand Down
13 changes: 13 additions & 0 deletions thrift/test/structs_terse.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,29 @@ struct TerseFieldsWithCustomDefault {
@cpp.Ref{type = cpp.RefType.Unique}
@cpp.AllowLegacyNonOptionalRef
@cpp.DeprecatedTerseWrite
@cpp.AllowLegacyDeprecatedTerseWritesRef
16: NestedStruct cpp_ref_struct_field;
@cpp.Ref{type = cpp.RefType.Unique}
@cpp.AllowLegacyNonOptionalRef
@cpp.DeprecatedTerseWrite
@cpp.AllowLegacyDeprecatedTerseWritesRef
17: NestedUnion cpp_ref_union_field;
@cpp.Ref{type = cpp.RefType.Unique}
@cpp.AllowLegacyNonOptionalRef
@cpp.DeprecatedTerseWrite
@cpp.AllowLegacyDeprecatedTerseWritesRef
18: NestedException cpp_ref_exception_field;

@cpp.DeprecatedTerseWrite
19: i32 redundant_custom_default = 0;

@cpp.Ref{type = cpp.RefType.Shared}
@cpp.AllowLegacyNonOptionalRef
20: NestedStruct cpp_shared_ref_struct_field;
@cpp.Ref{type = cpp.RefType.Shared}
@cpp.AllowLegacyNonOptionalRef
21: NestedUnion cpp_shared_ref_union_field;
@cpp.Ref{type = cpp.RefType.Shared}
@cpp.AllowLegacyNonOptionalRef
22: NestedException cpp_shared_ref_exception_field;
}

0 comments on commit 721b463

Please sign in to comment.