-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Implement a function to turn absolute schema references into re…
…lative Signed-off-by: Juan Cruz Viotti <[email protected]>
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <sourcemeta/jsontoolkit/jsonschema.h> | ||
|
||
#include <iostream> // TODO DEBUG | ||
|
||
namespace sourcemeta::jsontoolkit { | ||
|
||
// TODO: Test embedded schema resources with their own ids. References within | ||
// those ones have to be made relative too | ||
auto relativize(JSON &schema, const SchemaWalker &walker, | ||
const SchemaResolver &resolver, | ||
const std::optional<std::string> &default_dialect, | ||
const std::optional<std::string> &default_id) -> void { | ||
Frame frame; | ||
frame.analyse(schema, walker, resolver, default_dialect, default_id); | ||
|
||
for (const auto &entry : frame.locations()) { | ||
if (entry.second.type != Frame::LocationType::Resource && | ||
entry.second.type != Frame::LocationType::Subschema) { | ||
continue; | ||
} | ||
|
||
auto &subschema{get(schema, entry.second.pointer)}; | ||
assert(is_schema(subschema)); | ||
if (!subschema.is_object()) { | ||
continue; | ||
} | ||
|
||
const auto base{URI{entry.second.base}.canonicalize()}; | ||
for (const auto &property : subschema.as_object()) { | ||
if (walker(property.first, frame.vocabularies(entry.second, resolver)) | ||
.type != KeywordType::Reference || | ||
!property.second.is_string()) { | ||
continue; | ||
} | ||
|
||
URI reference{property.second.to_string()}; | ||
reference.canonicalize(); | ||
reference.relative_to(base); | ||
|
||
if (reference.is_relative()) { | ||
subschema.assign(property.first, JSON{reference.recompose()}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} // namespace sourcemeta::jsontoolkit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <sourcemeta/jsontoolkit/json.h> | ||
#include <sourcemeta/jsontoolkit/jsonschema.h> | ||
|
||
TEST(JSONSchema_relativize, draft4_1) { | ||
auto schema = sourcemeta::jsontoolkit::parse(R"JSON({ | ||
"id": "http://asyncapi.com/definitions/1.0.0/asyncapi.json", | ||
"$schema": "http://json-schema.org/draft-04/schema", | ||
"title": "AsyncAPI 1.0 schema.", | ||
"type": "object", | ||
"required": [ | ||
"asyncapi", | ||
"info", | ||
"topics" | ||
], | ||
"additionalProperties": false, | ||
"patternProperties": { | ||
"^x-": { | ||
"$ref": "http://asyncapi.com/definitions/1.0.0/vendorExtension.json" | ||
} | ||
} | ||
})JSON"); | ||
|
||
sourcemeta::jsontoolkit::relativize( | ||
schema, sourcemeta::jsontoolkit::default_schema_walker, | ||
sourcemeta::jsontoolkit::official_resolver); | ||
|
||
const auto expected = sourcemeta::jsontoolkit::parse(R"JSON({ | ||
"id": "http://asyncapi.com/definitions/1.0.0/asyncapi.json", | ||
"$schema": "http://json-schema.org/draft-04/schema", | ||
"title": "AsyncAPI 1.0 schema.", | ||
"type": "object", | ||
"required": [ | ||
"asyncapi", | ||
"info", | ||
"topics" | ||
], | ||
"additionalProperties": false, | ||
"patternProperties": { | ||
"^x-": { | ||
"$ref": "vendorExtension.json" | ||
} | ||
} | ||
})JSON"); | ||
|
||
EXPECT_EQ(schema, expected); | ||
} |