Skip to content

Commit 7752c01

Browse files
authored
fix(c++): std::unordered_map cannot be used in struct. (#3727) (#3728)
## Why? Cannot compile when there is a std::unordered_map in struct. ## What does this PR do? Fix that . ## Related issues Issue #3727 . ## AI Contribution Checklist - [ ] Substantial AI assistance was used in this PR: `yes` / `no` - [ ] If `yes`, I included a completed [AI Contribution Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs) in this PR description and the required `AI Usage Disclosure`. - [ ] If `yes`, my PR description includes the required `ai_review` summary and screenshot evidence of the final clean AI review results from both fresh reviewers on the current PR diff or current HEAD after the latest code changes. ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark
1 parent 0ec7261 commit 7752c01

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

cpp/fory/serialization/map_serializer.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,22 +1080,15 @@ struct Serializer<std::unordered_map<K, V, Args...>> {
10801080
using MapType = std::unordered_map<K, V, Args...>;
10811081

10821082
static inline void write(const MapType &map, WriteContext &ctx,
1083-
RefMode ref_mode, bool write_type) {
1083+
RefMode ref_mode, bool write_type,
1084+
bool has_generics = false) {
10841085
write_not_null_ref_flag(ctx, ref_mode);
10851086

10861087
if (write_type) {
10871088
ctx.write_uint8(static_cast<uint8_t>(type_id));
10881089
}
10891090

1090-
constexpr bool is_fast_path =
1091-
!is_polymorphic_v<K> && !is_polymorphic_v<V> && !is_shared_ref_v<K> &&
1092-
!is_shared_ref_v<V> && !is_nullable_v<K> && !is_nullable_v<V>;
1093-
1094-
if constexpr (is_fast_path) {
1095-
write_map_data_fast<K, V>(map, ctx, false);
1096-
} else {
1097-
write_map_data_slow<K, V>(map, ctx, true);
1098-
}
1091+
write_data_generic(map, ctx, has_generics);
10991092
}
11001093

11011094
static inline void write_data(const MapType &map, WriteContext &ctx) {

cpp/fory/serialization/map_serializer_test.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,46 @@ TEST(MapSerializerTest, NestedMapRoundtrip) {
9797
test_map_roundtrip(nested);
9898
}
9999

100+
struct MapsInStruct {
101+
std::map<int, std::string> map{};
102+
std::unordered_map<std::string, std::string> unordered_map{};
103+
104+
auto operator==(const MapsInStruct &rhs) const -> bool {
105+
return map == rhs.map && unordered_map == rhs.unordered_map;
106+
}
107+
108+
FORY_STRUCT(MapsInStruct, map, unordered_map);
109+
};
110+
111+
TEST(MapSerializerTest, UnorderedMapInStruct) {
112+
MapsInStruct original{};
113+
original.map[1] = "1";
114+
original.map[2] = "2";
115+
original.unordered_map["1"] = "2";
116+
original.unordered_map["2"] = "2";
117+
118+
// Create Fory instance with default config
119+
auto fory = Fory::builder().xlang(true).compatible(true).build();
120+
121+
fory.register_struct<MapsInStruct>(0);
122+
123+
// Serialize
124+
auto serialize_result = fory.serialize(original);
125+
ASSERT_TRUE(serialize_result.ok())
126+
<< "Serialization failed: " << serialize_result.error().message();
127+
auto bytes = serialize_result.value();
128+
129+
// Deserialize
130+
auto deserialize_result =
131+
fory.deserialize<MapsInStruct>(bytes.data(), bytes.size());
132+
ASSERT_TRUE(deserialize_result.ok())
133+
<< "Deserialization failed: " << deserialize_result.error().message();
134+
auto deserialized = deserialize_result.value();
135+
136+
// Compare
137+
EXPECT_EQ(original, deserialized);
138+
}
139+
100140
// ============================================================================
101141
// Map with Optional Values (Polymorphic-like behavior)
102142
// ============================================================================

0 commit comments

Comments
 (0)