Skip to content

Commit

Permalink
AVRO-4026: [c++] Allow non-string custom attributes in schemas. Custo…
Browse files Browse the repository at this point in the history
…m attributes are JSON string to avoid ambiguity
  • Loading branch information
jmd-z committed Aug 6, 2024
1 parent e91fd0e commit c6e2111
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lang/c++/impl/Compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ static void getCustomAttributes(const Object &m, CustomAttributes &customAttribu
const std::unordered_set<std::string> &kKnownFields = getKnownFields();
for (const auto &entry : m) {
if (kKnownFields.find(entry.first) == kKnownFields.end()) {
customAttributes.addAttribute(entry.first, entry.second.stringValue());
customAttributes.addAttribute(entry.first, entry.second.toString());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lang/c++/impl/CustomAttributes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ void CustomAttributes::printJson(std::ostream &os,
if (attributes().find(name) == attributes().end()) {
throw Exception(name + " doesn't exist");
}
os << "\"" << name << "\": \"" << attributes().at(name) << "\"";
os << "\"" << name << "\": " << attributes().at(name);
}
} // namespace avro
5 changes: 4 additions & 1 deletion lang/c++/test/SchemaTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ const char *basicSchemas[] = {
"[{\"name\": \"f1\",\"type\": \"long\",\"extra field\": \"1\"}]}",
"{\"type\": \"record\",\"name\": \"Test\",\"fields\": "
"[{\"name\": \"f1\",\"type\": \"long\","
"\"extra field1\": \"1\",\"extra field2\": \"2\"}]}"};
"\"extra field1\": \"1\",\"extra field2\": \"2\"}]}",
R"({"type": "record","name": "Test","fields":
[{"name": "f1","type": "string", "extra": {"custom1": "value","custom2": true }}]})"
};

const char *basicSchemaErrors[] = {
// Record
Expand Down
12 changes: 6 additions & 6 deletions lang/c++/test/unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ struct TestSchema {
concepts::MultiAttribute<CustomAttributes> customAttributes;

CustomAttributes cf;
cf.addAttribute("stringField", std::string("\\\"field value with \\\"double quotes\\\"\\\""));
cf.addAttribute("stringField", std::string(R"("\"field value with \"double quotes\"\"")"));
cf.addAttribute("booleanField", std::string("true"));
cf.addAttribute("numberField", std::string("1.23"));
cf.addAttribute("nullField", std::string("null"));
Expand All @@ -457,11 +457,11 @@ struct TestSchema {
std::string expectedJsonWithCustomAttribute =
"{\"type\": \"record\", \"name\": \"Test\",\"fields\": "
"[{\"name\": \"f1\", \"type\": \"long\", "
"\"arrayField\": \"[1]\", "
"\"booleanField\": \"true\", "
"\"mapField\": \"{\\\"key1\\\":\\\"value1\\\", \\\"key2\\\":\\\"value2\\\"}\", "
"\"nullField\": \"null\", "
"\"numberField\": \"1.23\", "
"\"arrayField\": [1], "
"\"booleanField\": true, "
"\"mapField\": {\\\"key1\\\":\\\"value1\\\", \\\"key2\\\":\\\"value2\\\"}, "
"\"nullField\": null, "
"\"numberField\": 1.23, "
"\"stringField\": \"\\\"field value with \\\"double quotes\\\"\\\"\""
"}]}";
testNodeRecord(nodeRecordWithCustomAttribute,
Expand Down

0 comments on commit c6e2111

Please sign in to comment.