Skip to content

Commit 9dbb868

Browse files
authored
Fix integer map value JSON generation (#4)
Integer map values were previously output as with format %@, which caused a crash because the parameter was shortValue. This seems to be an existing bug, but no FE thrift uses map<k, int> at runtime, so it hasn't affected anything. List and Set seem to work, so special-case Map to always use primitive format instead of id format.
1 parent e7de13f commit 9dbb868

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

compiler/cpp/src/generate/t_cocoa_generator.cc

+15-4
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ class t_cocoa_generator : public t_oop_generator {
160160
void generate_cocoa_struct_validator(std::ofstream& out, t_struct* tstruct);
161161
void generate_cocoa_struct_description(std::ofstream& out, t_struct* tstruct);
162162
void generate_cocoa_struct_to_json(std::ofstream& out, t_struct* tstruct);
163-
void generate_cocoa_struct_to_json_method(std::ofstream& out, t_field *field, int &counter, size_t levels, bool needs_isset_test);
163+
void generate_cocoa_struct_to_json_method(std::ofstream& out, t_field *field, int &counter, size_t levels, bool is_container_element);
164+
void generate_cocoa_struct_to_json_method(std::ofstream& out, t_field *field, int &counter, size_t levels, bool is_container_element, bool is_map_element);
164165

165166
std::string function_result_helper_struct_type(t_function* tfunction);
166167
std::string function_args_helper_struct_type(t_function* tfunction);
@@ -1668,12 +1669,22 @@ void t_cocoa_generator::generate_cocoa_struct_to_json(ofstream& out, t_struct* t
16681669
out << "}\n\n";
16691670
}
16701671

1672+
void t_cocoa_generator::generate_cocoa_struct_to_json_method(
1673+
std::ofstream& out,
1674+
t_field *field,
1675+
int &counter,
1676+
size_t levels,
1677+
bool is_container_element) {
1678+
generate_cocoa_struct_to_json_method(out, field, counter, levels, is_container_element, false);
1679+
}
1680+
16711681
void t_cocoa_generator::generate_cocoa_struct_to_json_method(
16721682
std::ofstream& out,
16731683
t_field *field,
16741684
int &counter,
16751685
size_t levels,
1676-
bool is_container_element) {
1686+
bool is_container_element,
1687+
bool is_map_element) {
16771688
using boost::lexical_cast;
16781689

16791690
t_type *type = field->get_type();
@@ -1721,7 +1732,7 @@ void t_cocoa_generator::generate_cocoa_struct_to_json_method(
17211732
}
17221733
} else if (base == t_base_type::TYPE_I16 || base == t_base_type::TYPE_I32 || base == t_base_type::TYPE_I64) {
17231734
string format_spec;
1724-
if (is_container_element) {
1735+
if (is_container_element && !is_map_element) {
17251736
format_spec = "@\"%@\"";
17261737
} else if (base == t_base_type::TYPE_I16) {
17271738
format_spec = "@\"%\" PRId16";
@@ -1862,7 +1873,7 @@ void t_cocoa_generator::generate_cocoa_struct_to_json_method(
18621873
}
18631874

18641875
indent(out, nl + 1) << "[builder appendString:@\": \"];\n";
1865-
generate_cocoa_struct_to_json_method(out, &fake_val_field, counter, nl + 1, true);
1876+
generate_cocoa_struct_to_json_method(out, &fake_val_field, counter, nl + 1, true, true);
18661877

18671878
indent(out, nl) << "}\n";
18681879
indent(out, nl) << "[builder appendString:@\"}\"];\n";

0 commit comments

Comments
 (0)