@@ -856,6 +856,60 @@ static void testAddCustomAttributes() {
856856 BOOST_CHECK_EQUAL (removeWhitespaceFromSchema (json), removeWhitespaceFromSchema (expected));
857857}
858858
859+ static void testCustomAttributesJson2Schema2Json () {
860+ const std::string schema = R"( {
861+ "type": "record",
862+ "name": "my_record",
863+ "fields": [
864+ { "name": "long_field", "type": "long", "int_key": 1, "str_key": "1" }
865+ ]
866+ })" ;
867+ ValidSchema compiledSchema = compileJsonSchemaFromString (schema);
868+
869+ // Verify custom attributes from parsed schema
870+ auto customAttributes = compiledSchema.root ()->customAttributesAt (0 );
871+ BOOST_CHECK_EQUAL (customAttributes.getAttribute (" int_key" ).value (), " 1" );
872+ BOOST_CHECK_EQUAL (customAttributes.getAttribute (" str_key" ).value (), " 1" );
873+
874+ // Verify custom attributes from json result
875+ std::string json = compiledSchema.toJson ();
876+ BOOST_CHECK_EQUAL (removeWhitespaceFromSchema (json), removeWhitespaceFromSchema (schema));
877+ }
878+
879+ static void testCustomAttributesSchema2Json2Schema () {
880+ const std::string expected = R"( {
881+ "type": "record",
882+ "name": "my_record",
883+ "fields": [
884+ { "name": "long_field", "type": "long", "int_key": 1, "str_key": "1" }
885+ ]
886+ })" ;
887+
888+ auto recordNode = std::make_shared<NodeRecord>();
889+ {
890+ CustomAttributes customAttributes;
891+ customAttributes.addAttribute (" int_key" , " 1" , /* addQuotes=*/ false );
892+ customAttributes.addAttribute (" str_key" , " 1" , /* addQuotes=*/ true );
893+ recordNode->addCustomAttributesForField (customAttributes);
894+ recordNode->addLeaf (std::make_shared<NodePrimitive>(AVRO_LONG));
895+ recordNode->addName (" long_field" );
896+ recordNode->setName (Name (" my_record" ));
897+ }
898+
899+ // Verify custom attributes from json result
900+ ValidSchema schema (recordNode);
901+ std::string json = schema.toJson ();
902+ BOOST_CHECK_EQUAL (removeWhitespaceFromSchema (json), removeWhitespaceFromSchema (expected));
903+
904+ // Verify custom attributes from parsed schema
905+ {
906+ auto parsedSchema = compileJsonSchemaFromString (json);
907+ auto customAttributes = parsedSchema.root ()->customAttributesAt (0 );
908+ BOOST_CHECK_EQUAL (customAttributes.getAttribute (" int_key" ).value (), " 1" );
909+ BOOST_CHECK_EQUAL (customAttributes.getAttribute (" str_key" ).value (), " 1" );
910+ }
911+ }
912+
859913} // namespace schema
860914} // namespace avro
861915
@@ -882,5 +936,7 @@ init_unit_test_suite(int /*argc*/, char * /*argv*/[]) {
882936 ts->add (BOOST_TEST_CASE (&avro::schema::testCustomLogicalType));
883937 ts->add (BOOST_TEST_CASE (&avro::schema::testParseCustomAttributes));
884938 ts->add (BOOST_TEST_CASE (&avro::schema::testAddCustomAttributes));
939+ ts->add (BOOST_TEST_CASE (&avro::schema::testCustomAttributesJson2Schema2Json));
940+ ts->add (BOOST_TEST_CASE (&avro::schema::testCustomAttributesSchema2Json2Schema));
885941 return ts;
886942}
0 commit comments