@@ -73,15 +73,78 @@ TEST_CASE("IdentifiableSchema transition with preferred identifiers",
7373 std::move (linkedSchemas), preferredIdentifiers);
7474
7575 REQUIRE (identifiableSchemas.size () == 2 );
76- REQUIRE (identifiableSchemas[0 ].dependencies_ .contains (
77- baseUri.withPointer (pointer / " properties" / " a" )));
78- REQUIRE (identifiableSchemas[0 ].dependencies_ .at (
79- baseUri.withPointer (pointer / " properties" / " a" )) == 0 );
76+ REQUIRE ((identifiableSchemas[0 ].dependencies_ .contains (
77+ baseUri.withPointer (pointer / " properties" / " a" )) ||
78+ identifiableSchemas[1 ].dependencies_ .contains (
79+ baseUri.withPointer (pointer / " properties" / " a" ))));
80+
81+ if (identifiableSchemas[0 ].dependencies_ .contains (
82+ baseUri.withPointer (pointer / " properties" / " a" ))) {
83+ REQUIRE (identifiableSchemas[0 ].dependencies_ .at (
84+ baseUri.withPointer (pointer / " properties" / " a" )) == 0 );
85+ } else if (identifiableSchemas[1 ].dependencies_ .contains (
86+ baseUri.withPointer (pointer / " properties" / " a" ))) {
87+ REQUIRE (identifiableSchemas[1 ].dependencies_ .at (
88+ baseUri.withPointer (pointer / " properties" / " a" )) == 0 );
89+ } else {
90+ FAIL (" Dependency not found" );
91+ }
8092 // Identifiers are unique
8193
8294 REQUIRE (identifiableSchemas[1 ].identifier_ !=
8395 identifiableSchemas[0 ].identifier_ );
8496
85- REQUIRE (identifiableSchemas[0 ].identifier_ == " MySchema" );
86- REQUIRE (identifiableSchemas[1 ].identifier_ == " MySchemaA" );
97+ REQUIRE ((identifiableSchemas[0 ].identifier_ == " MySchema" ||
98+ identifiableSchemas[1 ].identifier_ == " MySchema" ));
99+ REQUIRE ((identifiableSchemas[1 ].identifier_ == " MySchemaA" ||
100+ identifiableSchemas[0 ].identifier_ == " MySchemaA" ));
101+ }
102+
103+ // / @brief Preferred identifiers caused an incident where some schemas were
104+ // / handled before the rest of the schemas, causing swaps in the array, breaking
105+ // / dependencies. This test case ensures that the dependencies are not broken.
106+ TEST_CASE (" IdentifiableSchema preferred identifiers don't break dependencies" ,
107+ " [IdentifiableSchema]" ) {
108+ const auto json = R"(
109+ {
110+ "$id": "https://example.com/schema",
111+ "title": "Schema",
112+ "properties": {
113+ "a": {
114+ "type": "string"
115+ },
116+ "b": {
117+ "$ref": "#/properties/a"
118+ }
119+ },
120+ "additionalProperties": false,
121+ "required": ["a"],
122+ "type": "object"
123+ })" _json;
124+ const UriWrapper baseUri (" https://example.com/schema" );
125+ const JSONPointer pointer = JSONPointer ();
126+ const Draft draft = Draft::DRAFT_07;
127+ std::vector<std::unique_ptr<LinkedSchema>> linkedSchemas;
128+ linkedSchemas.emplace_back (std::make_unique<LinkedSchema>(
129+ json[" properties" ][" a" ], baseUri, pointer / " properties" / " a" , draft,
130+ std::map<UriWrapper, size_t >{}));
131+ linkedSchemas.emplace_back (std::make_unique<LinkedSchema>(
132+ json[" properties" ][" b" ], baseUri, pointer / " properties" / " b" , draft,
133+ std::map<UriWrapper, size_t >{
134+ {baseUri.withPointer (pointer / " properties" / " a" ), 0 }}));
135+ linkedSchemas.emplace_back (std::make_unique<LinkedSchema>(
136+ json, baseUri, pointer, draft,
137+ std::map<UriWrapper, size_t >{
138+ {baseUri.withPointer (pointer / " properties" / " a" ), 0 },
139+ {baseUri.withPointer (pointer / " properties" / " b" ), 1 }}));
140+ std::map<UriWrapper, std::string> preferredIdentifiers = {
141+ {baseUri.withPointer (pointer / " properties" / " b" ), " MySchemaB" }};
142+
143+ auto identifiableSchemas = IdentifiableSchema::transition (
144+ std::move (linkedSchemas), preferredIdentifiers);
145+
146+ REQUIRE (identifiableSchemas.size () == 3 );
147+ REQUIRE (identifiableSchemas[0 ].pointer_ == pointer / " properties" / " a" );
148+ REQUIRE (identifiableSchemas[1 ].pointer_ == pointer / " properties" / " b" );
149+ REQUIRE (identifiableSchemas[2 ].pointer_ == pointer);
87150}
0 commit comments