|
22 | 22 | #include <boost/test/unit_test.hpp> |
23 | 23 |
|
24 | 24 | #include "Compiler.hh" |
| 25 | +#include "Node.hh" |
25 | 26 | #include "ValidSchema.hh" |
26 | 27 |
|
27 | 28 | // Assert that empty defaults don't make json schema compilation violate bounds |
@@ -82,12 +83,74 @@ void test2dArray() { |
82 | 83 | BOOST_CHECK_EQUAL(expected, actual.str()); |
83 | 84 | } |
84 | 85 |
|
| 86 | +void testRecordWithNamedReference() { |
| 87 | + std::string nestedSchema = "{\"name\":\"NestedRecord\",\"type\":\"record\",\"fields\":[{\"name\":\"stringField\",\"type\":\"string\"}]}"; |
| 88 | + // The root schema references the nested schema above by name only. |
| 89 | + // This mimics tools that allow schemas to have references to other schemas. |
| 90 | + std::string rootSchema = "{\"name\":\"RootRecord\",\"type\":\"record\",\"fields\":[{\"name\": \"nestedField\",\"type\":\"NestedRecord\"}]}"; |
| 91 | + |
| 92 | + // First compile the nested schema |
| 93 | + avro::ValidSchema nestedRecord = avro::compileJsonSchemaFromString(nestedSchema); |
| 94 | + |
| 95 | + // Create a map of named references |
| 96 | + std::map<avro::Name, avro::ValidSchema> namedReferences; |
| 97 | + namedReferences[avro::Name("NestedRecord")] = nestedRecord; |
| 98 | + |
| 99 | + // Parse the root schema with named references |
| 100 | + std::istringstream rootSchemaStream(rootSchema); |
| 101 | + avro::ValidSchema rootRecord = avro::compileJsonSchemaWithNamedReferences(rootSchemaStream, namedReferences); |
| 102 | + |
| 103 | + // Verify the schema was compiled correctly |
| 104 | + BOOST_CHECK_EQUAL("RootRecord", rootRecord.root()->name().simpleName()); |
| 105 | + |
| 106 | + // Get the nested field and verify its type |
| 107 | + const avro::NodePtr &rootNode = rootRecord.root(); |
| 108 | + BOOST_CHECK_EQUAL(avro::AVRO_RECORD, rootNode->type()); |
| 109 | + BOOST_CHECK_EQUAL(1, rootNode->leaves()); |
| 110 | + |
| 111 | + const avro::NodePtr &nestedFieldNode = rootNode->leafAt(0); |
| 112 | + BOOST_CHECK_EQUAL("NestedRecord", nestedFieldNode->name().simpleName()); |
| 113 | +} |
| 114 | + |
| 115 | +// Verify recursive schemas don't create shared_ptr cycles by ensuring the |
| 116 | +// root node expires once the ValidSchema goes out of scope. Example: binary |
| 117 | +// tree node with left/right as union of null and the node type itself. |
| 118 | +void testRecursiveBinaryTreeWeakPtrExpires() { |
| 119 | + std::weak_ptr<avro::Node> weakRoot; |
| 120 | + |
| 121 | + { |
| 122 | + const std::string schema = R"({ |
| 123 | + "type": "record", |
| 124 | + "name": "Node", |
| 125 | + "fields": [ |
| 126 | + {"name": "value", "type": "int"}, |
| 127 | + {"name": "left", "type": ["null", "Node"], "default": null}, |
| 128 | + {"name": "right", "type": ["null", "Node"], "default": null} |
| 129 | + ] |
| 130 | + })"; |
| 131 | + |
| 132 | + avro::ValidSchema s = avro::compileJsonSchemaFromString(schema); |
| 133 | + // Capture a weak reference to the root node while the schema is alive. |
| 134 | + weakRoot = s.root(); |
| 135 | + |
| 136 | + // Optionally exercise the schema to ensure validation completed. |
| 137 | + BOOST_CHECK_EQUAL(avro::AVRO_RECORD, s.root()->type()); |
| 138 | + BOOST_CHECK_EQUAL("Node", s.root()->name().simpleName()); |
| 139 | + } |
| 140 | + |
| 141 | + // After the ValidSchema (and any strong references) go out of scope, |
| 142 | + // the weak pointer must not be lockable if there are no cycles. |
| 143 | + BOOST_CHECK(weakRoot.expired()); |
| 144 | +} |
| 145 | + |
85 | 146 | boost::unit_test::test_suite * |
86 | 147 | init_unit_test_suite(int /*argc*/, char * /*argv*/[]) { |
87 | 148 | using namespace boost::unit_test; |
88 | 149 |
|
89 | 150 | auto *ts = BOOST_TEST_SUITE("Avro C++ unit tests for Compiler.cc"); |
90 | 151 | ts->add(BOOST_TEST_CASE(&testEmptyBytesDefault)); |
91 | 152 | ts->add(BOOST_TEST_CASE(&test2dArray)); |
| 153 | + ts->add(BOOST_TEST_CASE(&testRecordWithNamedReference)); |
| 154 | + ts->add(BOOST_TEST_CASE(&testRecursiveBinaryTreeWeakPtrExpires)); |
92 | 155 | return ts; |
93 | 156 | } |
0 commit comments