Skip to content

Commit

Permalink
feat(ns-json-schema-draft-6): handle inheritance of $schema and $id (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud authored Jan 17, 2025
1 parent 2b65f9e commit 86ddd8b
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 18 deletions.
6 changes: 2 additions & 4 deletions packages/apidom-ns-json-schema-draft-4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ export type {
default as EnumVisitor,
EnumVisitorOptions,
} from './refractor/visitors/json-schema/EnumVisitor.ts';
export type {
default as JSONSchemaVisitor,
JSONSchemaVisitorOptions,
} from './refractor/visitors/json-schema/index.ts';
export { default as JSONSchemaVisitor } from './refractor/visitors/json-schema/index.ts';
export type { JSONSchemaVisitorOptions } from './refractor/visitors/json-schema/index.ts';
export type {
default as ItemsVisitor,
ItemsVisitorOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { ObjectElement, BooleanElement } from '@swagger-api/apidom-core';
import {
FixedFieldsVisitor,
FixedFieldsVisitorOptions,
ParentSchemaAwareVisitor,
ParentSchemaAwareVisitorOptions,
FallbackVisitor,
FallbackVisitorOptions,
SpecPath,
JSONSchemaVisitor as JSONSchemaDraft4Visitor,
} from '@swagger-api/apidom-ns-json-schema-draft-4';

import JSONSchemaElement from '../../../elements/JSONSchema.ts';
Expand All @@ -16,12 +19,17 @@ import JSONSchemaElement from '../../../elements/JSONSchema.ts';
*/
export interface JSONSchemaVisitorOptions
extends FixedFieldsVisitorOptions,
ParentSchemaAwareVisitorOptions,
FallbackVisitorOptions {}

/**
* @public
*/
class JSONSchemaVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
class JSONSchemaVisitor extends Mixin(
FixedFieldsVisitor,
ParentSchemaAwareVisitor,
FallbackVisitor,
) {
declare public element: JSONSchemaElement;

declare protected readonly specPath: SpecPath<['document', 'objects', 'JSONSchema']>;
Expand All @@ -31,8 +39,18 @@ class JSONSchemaVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
this.specPath = always(['document', 'objects', 'JSONSchema']);
}

// eslint-disable-next-line class-methods-use-this
get defaultDialectIdentifier(): string {
return 'http://json-schema.org/draft-06/schema#';
}

ObjectElement(objectElement: ObjectElement) {
this.element = new JSONSchemaElement();
this.handleDialectIdentifier(objectElement);
this.handleSchemaIdentifier(objectElement);

// for further processing consider this Schema Element as parent for all embedded Schema Elements
this.parent = this.element;

return FixedFieldsVisitor.prototype.ObjectElement.call(this, objectElement);
}
Expand All @@ -43,6 +61,18 @@ class JSONSchemaVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {

return result;
}

handleDialectIdentifier(objectElement: ObjectElement): void {
return JSONSchemaDraft4Visitor.prototype.handleDialectIdentifier.call(this, objectElement);
}

handleSchemaIdentifier(objectElement: ObjectElement, identifierKeyword: string = '$id'): void {
return JSONSchemaDraft4Visitor.prototype.handleSchemaIdentifier.call(
this,
objectElement,
identifierKeyword,
);
}
}

export default JSONSchemaVisitor;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape should refract to JSONSchema Element 1`] = `
{
"element": "JSONSchemaDraft6",
"meta": {
"ancestorsSchemaIdentifiers": {
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
},
"content": [
{
"element": "member",
Expand Down Expand Up @@ -263,7 +274,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
"content": "additionalItems"
},
"value": {
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
},
Expand Down Expand Up @@ -300,7 +320,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
},
"content": [
{
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
]
}
Expand Down Expand Up @@ -397,7 +426,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
"content": "contains"
},
"value": {
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
},
Expand Down Expand Up @@ -544,7 +582,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
"content": "prop1"
},
"value": {
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
}
Expand All @@ -571,7 +618,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
"content": "additionalProperties"
},
"value": {
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
},
Expand Down Expand Up @@ -626,7 +682,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
"content": "[a-z]+"
},
"value": {
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
}
Expand Down Expand Up @@ -685,7 +750,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
"content": "dep1"
},
"value": {
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
}
Expand All @@ -712,7 +786,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
"content": "propertyNames"
},
"value": {
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
},
Expand Down Expand Up @@ -856,7 +939,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
},
"content": [
{
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
]
}
Expand Down Expand Up @@ -895,7 +987,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
},
"content": [
{
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
]
}
Expand Down Expand Up @@ -934,7 +1035,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
},
"content": [
{
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
]
}
Expand All @@ -959,7 +1069,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
"content": "not"
},
"value": {
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
},
Expand Down Expand Up @@ -1014,7 +1133,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 6 shape shou
"content": "def1"
},
"value": {
"element": "JSONSchemaDraft6"
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
exports[`refractor elements Boolean JSON Schema support should refract to semantic ApiDOM tree 1`] = `
{
"element": "JSONSchemaDraft6",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-06/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
},
"content": [
{
"element": "member",
Expand Down

0 comments on commit 86ddd8b

Please sign in to comment.