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
15 changes: 15 additions & 0 deletions tests/schema/pass/json_schema_dialect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
openapi: 3.1.0
info:
summary: Testing jsonSchemaDialect
title: My API
version: 1.0.0
license:
name: Apache 2.0
identifier: Apache-2.0
jsonSchemaDialect: https://spec.openapis.org/oas/3.1/dialect/WORK-IN-PROGRESS
components:
schemas:
WithDollarSchema:
$id: "locked-metaschema"
$schema: https://spec.openapis.org/oas/3.1/dialect/WORK-IN-PROGRESS
paths: {}
1 change: 0 additions & 1 deletion tests/schema/pass/mega.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ info:
license:
name: Apache 2.0
identifier: Apache-2.0
jsonSchemaDialect: https://spec.openapis.org/oas/3.1/dialect/base
paths:
/:
get:
Expand Down
63 changes: 60 additions & 3 deletions tests/schema/schema.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readdirSync, readFileSync } from "node:fs";
import YAML from "yaml";
import { validate, setMetaSchemaOutputFormat } from "@hyperjump/json-schema/openapi-3-1";
import { BASIC } from "@hyperjump/json-schema/experimental";
import { registerSchema, validate, setMetaSchemaOutputFormat } from "@hyperjump/json-schema/openapi-3-1";
import { BASIC, addKeyword, defineVocabulary } from "@hyperjump/json-schema/experimental";
import { describe, test, expect } from "vitest";

import contentTypeParser from "content-type";
Expand All @@ -26,7 +26,64 @@ const parseYamlFromFile = (filePath) => {

setMetaSchemaOutputFormat(BASIC);

const validateOpenApi = await validate("./src/schemas/validation/schema.yaml");
addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/discriminator",
interpret: (discriminator, instance, context) => {
return true;
},
/* discriminator is not exactly an annotation, but it's not allowed
* to change the validation outcome (hence returing true from interopret())
* and for our purposes of testing, this is sufficient.
*/
annotation: (discriminator) => {
return discriminator;
},
});

addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/example",
interpret: (example, instance, context) => {
return true;
},
annotation: (example) => {
return example;
},
});

addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/externalDocs",
interpret: (externalDocs, instance, context) => {
return true;
},
annotation: (externalDocs) => {
return externalDocs;
},
});

addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/xml",
interpret: (xml, instance, context) => {
return true;
},
annotation: (xml) => {
return xml;
},
});

defineVocabulary(
"https://spec.openapis.org/oas/3.1/vocab/base",
{
"discriminator": "https://spec.openapis.org/oas/schema/vocab/keyword/discriminator",
"example": "https://spec.openapis.org/oas/schema/vocab/keyword/example",
"externalDocs": "https://spec.openapis.org/oas/schema/vocab/keyword/externalDocs",
"xml": "https://spec.openapis.org/oas/schema/vocab/keyword/xml",
},
);
Comment on lines +29 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to define the vocabulary or keywords. They're already loaded when you import from @hyperjump/json-schema/openapi-3-1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdesrosiers thanks for commenting- the problem is that we're using a schema with a different $id from the published 3.1 schema, as we're testing the in-development branches. Also I was mostly trying to get something else unblocked so I didn't dig as deeply as I might have otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem is that we're using a schema with a different $id from the published 3.1 schema

Right. Registering the meta-schemas immediately after this block addresses that concern. It's just defining the vocabulary (defineVocabulary) and the keywords (addKeyword) that's unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just defining the vocabulary (defineVocabulary) and the keywords (addKeyword) that's unnecessary.

Apparently we still need defineVocabulary: just registering the meta-schemas results in

Error: Unrecognized vocabulary: https://spec.openapis.org/oas/3.2/vocab/base.
You can define this vocabulary with the 'defineVocabulary' function.

when validating 3.2 OpenAPI descriptions.

I expected that registering meta.yaml - whose title and description claim that it defines the OAS Base Vocabulary - would actually define the vocabulary 😞

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah. I never added support for 3.2. I'll work on it.


registerSchema(parseYamlFromFile("./src/schemas/validation/meta.yaml"));
registerSchema(parseYamlFromFile("./src/schemas/validation/dialect.yaml"));
registerSchema(parseYamlFromFile("./src/schemas/validation/schema.yaml"));
const validateOpenApi = await validate("./src/schemas/validation/schema-base.yaml");
const fixtures = './tests/schema';

describe("v3.1", () => {
Expand Down
Loading