Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 94 additions & 61 deletions src/spec/jsonschema/known_walker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct Variant {
SchemaWalkerResult result;
};

constexpr std::array<Variant, 728> VARIANTS{{
constexpr std::array<Variant, 732> VARIANTS{{
{.vocabulary = Known::JSON_SCHEMA_2020_12_CORE,
.secondary = std::nullopt,
.result = {SchemaKeywordType::Other,
Expand Down Expand Up @@ -2021,13 +2021,46 @@ constexpr std::array<Variant, 728> VARIANTS{{
{},
{},
INSTANCES_ANY}},
// The OpenAPI revisions define formats over numbers too, unlike every
// JSON Schema dialect, whose defined formats all describe strings, and
// leave the keyword open to any value whose applicable types a registry
// states. These revisions build on a JSON Schema dialect, so the keyword
// stays in the vocabulary that defines it and only its instances widen
{.vocabulary = Known::JSON_SCHEMA_2020_12_FORMAT_ASSERTION,
.secondary = Known::OPENAPI_3_2_BASE,
.result = {SchemaKeywordType::Assertion,
Known::JSON_SCHEMA_2020_12_FORMAT_ASSERTION,
{},
{},
INSTANCES_ANY}},
{.vocabulary = Known::JSON_SCHEMA_2020_12_FORMAT_ASSERTION,
.secondary = Known::OPENAPI_3_1_BASE,
.result = {SchemaKeywordType::Assertion,
Known::JSON_SCHEMA_2020_12_FORMAT_ASSERTION,
{},
{},
INSTANCES_ANY}},
{.vocabulary = Known::JSON_SCHEMA_2020_12_FORMAT_ASSERTION,
.secondary = std::nullopt,
.result = {SchemaKeywordType::Assertion,
Known::JSON_SCHEMA_2020_12_FORMAT_ASSERTION,
{},
{},
INSTANCES_STRING}},
{.vocabulary = Known::JSON_SCHEMA_2020_12_FORMAT_ANNOTATION,
.secondary = Known::OPENAPI_3_2_BASE,
.result = {SchemaKeywordType::Annotation,
Known::JSON_SCHEMA_2020_12_FORMAT_ANNOTATION,
{},
{},
INSTANCES_ANY}},
{.vocabulary = Known::JSON_SCHEMA_2020_12_FORMAT_ANNOTATION,
.secondary = Known::OPENAPI_3_1_BASE,
.result = {SchemaKeywordType::Annotation,
Known::JSON_SCHEMA_2020_12_FORMAT_ANNOTATION,
{},
{},
INSTANCES_ANY}},
{.vocabulary = Known::JSON_SCHEMA_2020_12_FORMAT_ANNOTATION,
.secondary = std::nullopt,
.result = {SchemaKeywordType::Annotation,
Expand Down Expand Up @@ -5255,84 +5288,84 @@ constexpr std::array<Keyword, 104> KEYWORDS{{
.legacy_ref = false},
{.name = "extends", .offset = 269, .count = 8, .legacy_ref = false},
{.name = "externalDocs", .offset = 277, .count = 3, .legacy_ref = false},
{.name = "format", .offset = 280, .count = 18, .legacy_ref = false},
{.name = "format", .offset = 280, .count = 22, .legacy_ref = false},
{.name = "fragmentResolution",
.offset = 298,
.offset = 302,
.count = 5,
.legacy_ref = true},
{.name = "headerSchema", .offset = 303, .count = 2, .legacy_ref = true},
{.name = "href", .offset = 305, .count = 8, .legacy_ref = true},
{.name = "hrefSchema", .offset = 313, .count = 3, .legacy_ref = true},
{.name = "id", .offset = 316, .count = 10, .legacy_ref = false},
{.name = "if", .offset = 326, .count = 4, .legacy_ref = false},
{.name = "items", .offset = 330, .count = 17, .legacy_ref = false},
{.name = "links", .offset = 347, .count = 8, .legacy_ref = true},
{.name = "maxContains", .offset = 355, .count = 2, .legacy_ref = false},
{.name = "maxDecimal", .offset = 357, .count = 4, .legacy_ref = false},
{.name = "maxItems", .offset = 361, .count = 17, .legacy_ref = false},
{.name = "maxLength", .offset = 378, .count = 17, .legacy_ref = false},
{.name = "maxProperties", .offset = 395, .count = 9, .legacy_ref = false},
{.name = "maximum", .offset = 404, .count = 17, .legacy_ref = false},
{.name = "maximumCanEqual", .offset = 421, .count = 6, .legacy_ref = false},
{.name = "media", .offset = 427, .count = 2, .legacy_ref = true},
{.name = "mediaType", .offset = 429, .count = 6, .legacy_ref = true},
{.name = "method", .offset = 435, .count = 5, .legacy_ref = true},
{.name = "minContains", .offset = 440, .count = 2, .legacy_ref = false},
{.name = "minItems", .offset = 442, .count = 17, .legacy_ref = false},
{.name = "minLength", .offset = 459, .count = 17, .legacy_ref = false},
{.name = "minProperties", .offset = 476, .count = 9, .legacy_ref = false},
{.name = "minimum", .offset = 485, .count = 17, .legacy_ref = false},
{.name = "minimumCanEqual", .offset = 502, .count = 6, .legacy_ref = false},
{.name = "multipleOf", .offset = 508, .count = 9, .legacy_ref = false},
{.name = "not", .offset = 517, .count = 9, .legacy_ref = false},
{.name = "nullable", .offset = 526, .count = 1, .legacy_ref = false},
{.name = "oneOf", .offset = 527, .count = 9, .legacy_ref = false},
{.name = "optional", .offset = 536, .count = 6, .legacy_ref = false},
{.name = "pathStart", .offset = 542, .count = 5, .legacy_ref = true},
{.name = "pattern", .offset = 547, .count = 17, .legacy_ref = false},
{.name = "headerSchema", .offset = 307, .count = 2, .legacy_ref = true},
{.name = "href", .offset = 309, .count = 8, .legacy_ref = true},
{.name = "hrefSchema", .offset = 317, .count = 3, .legacy_ref = true},
{.name = "id", .offset = 320, .count = 10, .legacy_ref = false},
{.name = "if", .offset = 330, .count = 4, .legacy_ref = false},
{.name = "items", .offset = 334, .count = 17, .legacy_ref = false},
{.name = "links", .offset = 351, .count = 8, .legacy_ref = true},
{.name = "maxContains", .offset = 359, .count = 2, .legacy_ref = false},
{.name = "maxDecimal", .offset = 361, .count = 4, .legacy_ref = false},
{.name = "maxItems", .offset = 365, .count = 17, .legacy_ref = false},
{.name = "maxLength", .offset = 382, .count = 17, .legacy_ref = false},
{.name = "maxProperties", .offset = 399, .count = 9, .legacy_ref = false},
{.name = "maximum", .offset = 408, .count = 17, .legacy_ref = false},
{.name = "maximumCanEqual", .offset = 425, .count = 6, .legacy_ref = false},
{.name = "media", .offset = 431, .count = 2, .legacy_ref = true},
{.name = "mediaType", .offset = 433, .count = 6, .legacy_ref = true},
{.name = "method", .offset = 439, .count = 5, .legacy_ref = true},
{.name = "minContains", .offset = 444, .count = 2, .legacy_ref = false},
{.name = "minItems", .offset = 446, .count = 17, .legacy_ref = false},
{.name = "minLength", .offset = 463, .count = 17, .legacy_ref = false},
{.name = "minProperties", .offset = 480, .count = 9, .legacy_ref = false},
{.name = "minimum", .offset = 489, .count = 17, .legacy_ref = false},
{.name = "minimumCanEqual", .offset = 506, .count = 6, .legacy_ref = false},
{.name = "multipleOf", .offset = 512, .count = 9, .legacy_ref = false},
{.name = "not", .offset = 521, .count = 9, .legacy_ref = false},
{.name = "nullable", .offset = 530, .count = 1, .legacy_ref = false},
{.name = "oneOf", .offset = 531, .count = 9, .legacy_ref = false},
{.name = "optional", .offset = 540, .count = 6, .legacy_ref = false},
{.name = "pathStart", .offset = 546, .count = 5, .legacy_ref = true},
{.name = "pattern", .offset = 551, .count = 17, .legacy_ref = false},
{.name = "patternProperties",
.offset = 564,
.offset = 568,
.count = 10,
.legacy_ref = false},
{.name = "prefixItems", .offset = 574, .count = 1, .legacy_ref = false},
{.name = "properties", .offset = 575, .count = 19, .legacy_ref = false},
{.name = "propertyNames", .offset = 594, .count = 6, .legacy_ref = false},
{.name = "readOnly", .offset = 600, .count = 6, .legacy_ref = true},
{.name = "readonly", .offset = 606, .count = 4, .legacy_ref = true},
{.name = "rel", .offset = 610, .count = 8, .legacy_ref = true},
{.name = "required", .offset = 618, .count = 11, .legacy_ref = false},
{.name = "requires", .offset = 629, .count = 6, .legacy_ref = false},
{.name = "root", .offset = 635, .count = 4, .legacy_ref = true},
{.name = "schema", .offset = 639, .count = 1, .legacy_ref = true},
{.name = "prefixItems", .offset = 578, .count = 1, .legacy_ref = false},
{.name = "properties", .offset = 579, .count = 19, .legacy_ref = false},
{.name = "propertyNames", .offset = 598, .count = 6, .legacy_ref = false},
{.name = "readOnly", .offset = 604, .count = 6, .legacy_ref = true},
{.name = "readonly", .offset = 610, .count = 4, .legacy_ref = true},
{.name = "rel", .offset = 614, .count = 8, .legacy_ref = true},
{.name = "required", .offset = 622, .count = 11, .legacy_ref = false},
{.name = "requires", .offset = 633, .count = 6, .legacy_ref = false},
{.name = "root", .offset = 639, .count = 4, .legacy_ref = true},
{.name = "schema", .offset = 643, .count = 1, .legacy_ref = true},
{.name = "submissionEncType",
.offset = 640,
.offset = 644,
.count = 1,
.legacy_ref = true},
{.name = "submissionMediaType",
.offset = 641,
.offset = 645,
.count = 2,
.legacy_ref = true},
{.name = "submissionSchema", .offset = 643, .count = 3, .legacy_ref = true},
{.name = "targetHints", .offset = 646, .count = 2, .legacy_ref = true},
{.name = "targetMediaType", .offset = 648, .count = 2, .legacy_ref = true},
{.name = "targetSchema", .offset = 650, .count = 6, .legacy_ref = true},
{.name = "templatePointers", .offset = 656, .count = 2, .legacy_ref = true},
{.name = "templateRequired", .offset = 658, .count = 2, .legacy_ref = true},
{.name = "then", .offset = 660, .count = 4, .legacy_ref = false},
{.name = "title", .offset = 664, .count = 17, .legacy_ref = false},
{.name = "type", .offset = 681, .count = 19, .legacy_ref = false},
{.name = "submissionSchema", .offset = 647, .count = 3, .legacy_ref = true},
{.name = "targetHints", .offset = 650, .count = 2, .legacy_ref = true},
{.name = "targetMediaType", .offset = 652, .count = 2, .legacy_ref = true},
{.name = "targetSchema", .offset = 654, .count = 6, .legacy_ref = true},
{.name = "templatePointers", .offset = 660, .count = 2, .legacy_ref = true},
{.name = "templateRequired", .offset = 662, .count = 2, .legacy_ref = true},
{.name = "then", .offset = 664, .count = 4, .legacy_ref = false},
{.name = "title", .offset = 668, .count = 17, .legacy_ref = false},
{.name = "type", .offset = 685, .count = 19, .legacy_ref = false},
{.name = "unevaluatedItems",
.offset = 700,
.offset = 704,
.count = 3,
.legacy_ref = false},
{.name = "unevaluatedProperties",
.offset = 703,
.offset = 707,
.count = 3,
.legacy_ref = false},
{.name = "uniqueItems", .offset = 706, .count = 13, .legacy_ref = false},
{.name = "writeOnly", .offset = 719, .count = 5, .legacy_ref = true},
{.name = "x-definitions", .offset = 724, .count = 1, .legacy_ref = false},
{.name = "xml", .offset = 725, .count = 3, .legacy_ref = false},
{.name = "uniqueItems", .offset = 710, .count = 13, .legacy_ref = false},
{.name = "writeOnly", .offset = 723, .count = 5, .legacy_ref = true},
{.name = "x-definitions", .offset = 728, .count = 1, .legacy_ref = false},
{.name = "xml", .offset = 729, .count = 3, .legacy_ref = false},
}};

constexpr auto REF_DEPENDENCIES{std::to_array<std::string_view>({"$ref"})};
Expand Down
96 changes: 96 additions & 0 deletions test/spec/jsonschema/jsonschema_walker_openapi_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ static const sourcemeta::core::SchemaVocabularies VOCABULARIES_OPENAPI_3_2{
{"https://json-schema.org/draft/2020-12/vocab/core", true},
{"https://spec.openapis.org/oas/3.2/vocab/base", true}};

static const sourcemeta::core::SchemaVocabularies
VOCABULARIES_OPENAPI_3_1_FORMAT_ANNOTATION{
{"https://json-schema.org/draft/2020-12/vocab/core", true},
{"https://json-schema.org/draft/2020-12/vocab/format-annotation", true},
{"https://spec.openapis.org/oas/3.1/vocab/base", true}};

static const sourcemeta::core::SchemaVocabularies
VOCABULARIES_OPENAPI_3_1_FORMAT_ASSERTION{
{"https://json-schema.org/draft/2020-12/vocab/core", true},
{"https://json-schema.org/draft/2020-12/vocab/format-assertion", true},
{"https://spec.openapis.org/oas/3.1/vocab/base", true}};

static const sourcemeta::core::SchemaVocabularies
VOCABULARIES_OPENAPI_3_2_FORMAT_ANNOTATION{
{"https://json-schema.org/draft/2020-12/vocab/core", true},
{"https://json-schema.org/draft/2020-12/vocab/format-annotation", true},
{"https://spec.openapis.org/oas/3.2/vocab/base", true}};

static const sourcemeta::core::SchemaVocabularies
VOCABULARIES_OPENAPI_3_2_FORMAT_ASSERTION{
{"https://json-schema.org/draft/2020-12/vocab/core", true},
{"https://json-schema.org/draft/2020-12/vocab/format-assertion", true},
{"https://spec.openapis.org/oas/3.2/vocab/base", true}};

static const sourcemeta::core::SchemaVocabularies VOCABULARIES_OPENAPI_3_0{
{"tag:spec.openapis.org,2017:oas/3.0/vocab/base", true}};

Expand Down Expand Up @@ -123,6 +147,42 @@ TEST(openapi_3_1_example_without_vocabulary) {
EXPECT_TRUE(result.instances.none());
}

TEST(openapi_3_1_format_annotation) {
using namespace sourcemeta::core;
const auto &result{
schema_walker("format", VOCABULARIES_OPENAPI_3_1_FORMAT_ANNOTATION)};
EXPECT_EQ(result.type, SchemaKeywordType::Annotation);
EXPECT_TRUE(result.vocabulary.has_value());
EXPECT_VOCABULARY_KNOWN(result.vocabulary.value(),
JSON_SCHEMA_2020_12_FORMAT_ANNOTATION);
EXPECT_TRUE(result.dependencies.empty());
EXPECT_TRUE(result.order_dependencies.empty());
EXPECT_TRUE(result.instances.none());
}

TEST(openapi_3_1_format_assertion) {
using namespace sourcemeta::core;
const auto &result{
schema_walker("format", VOCABULARIES_OPENAPI_3_1_FORMAT_ASSERTION)};
EXPECT_EQ(result.type, SchemaKeywordType::Assertion);
EXPECT_TRUE(result.vocabulary.has_value());
EXPECT_VOCABULARY_KNOWN(result.vocabulary.value(),
JSON_SCHEMA_2020_12_FORMAT_ASSERTION);
EXPECT_TRUE(result.dependencies.empty());
EXPECT_TRUE(result.order_dependencies.empty());
EXPECT_TRUE(result.instances.none());
}

TEST(openapi_3_1_format_without_format_vocabulary) {
using namespace sourcemeta::core;
const auto &result{schema_walker("format", VOCABULARIES_OPENAPI_3_1)};
EXPECT_EQ(result.type, SchemaKeywordType::Unknown);
EXPECT_FALSE(result.vocabulary.has_value());
EXPECT_TRUE(result.dependencies.empty());
EXPECT_TRUE(result.order_dependencies.empty());
EXPECT_TRUE(result.instances.none());
}

TEST(openapi_3_2_discriminator) {
using namespace sourcemeta::core;
using namespace sourcemeta::core;
Expand Down Expand Up @@ -215,6 +275,42 @@ TEST(openapi_3_2_example_without_vocabulary) {
EXPECT_TRUE(result.instances.none());
}

TEST(openapi_3_2_format_annotation) {
using namespace sourcemeta::core;
const auto &result{
schema_walker("format", VOCABULARIES_OPENAPI_3_2_FORMAT_ANNOTATION)};
EXPECT_EQ(result.type, SchemaKeywordType::Annotation);
EXPECT_TRUE(result.vocabulary.has_value());
EXPECT_VOCABULARY_KNOWN(result.vocabulary.value(),
JSON_SCHEMA_2020_12_FORMAT_ANNOTATION);
EXPECT_TRUE(result.dependencies.empty());
EXPECT_TRUE(result.order_dependencies.empty());
EXPECT_TRUE(result.instances.none());
}

TEST(openapi_3_2_format_assertion) {
using namespace sourcemeta::core;
const auto &result{
schema_walker("format", VOCABULARIES_OPENAPI_3_2_FORMAT_ASSERTION)};
EXPECT_EQ(result.type, SchemaKeywordType::Assertion);
EXPECT_TRUE(result.vocabulary.has_value());
EXPECT_VOCABULARY_KNOWN(result.vocabulary.value(),
JSON_SCHEMA_2020_12_FORMAT_ASSERTION);
EXPECT_TRUE(result.dependencies.empty());
EXPECT_TRUE(result.order_dependencies.empty());
EXPECT_TRUE(result.instances.none());
}

TEST(openapi_3_2_format_without_format_vocabulary) {
using namespace sourcemeta::core;
const auto &result{schema_walker("format", VOCABULARIES_OPENAPI_3_2)};
EXPECT_EQ(result.type, SchemaKeywordType::Unknown);
EXPECT_FALSE(result.vocabulary.has_value());
EXPECT_TRUE(result.dependencies.empty());
EXPECT_TRUE(result.order_dependencies.empty());
EXPECT_TRUE(result.instances.none());
}

TEST(openapi_3_0_title) {
using namespace sourcemeta::core;
const auto &result{schema_walker("title", VOCABULARIES_OPENAPI_3_0)};
Expand Down
Loading