Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ns-json-schema-draft-4): handle inheritance of $schema and id #4653

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
import { Mixin } from 'ts-mixer';
import { always } from 'ramda';
import { always, defaultTo } from 'ramda';
import { isNonEmptyString, isUndefined } from 'ramda-adjunct';
import {
ObjectElement,
ArrayElement,
isStringElement,
cloneDeep,
toValue,
} from '@swagger-api/apidom-core';

import FixedFieldsVisitor, {
FixedFieldsVisitorOptions,
SpecPath,
} from '../generics/FixedFieldsVisitor.ts';
import ParentSchemaAwareVisitor, {
ParentSchemaAwareVisitorOptions,
} from './ParentSchemaAwareVisitor.ts';
import FallbackVisitor, { FallbackVisitorOptions } from '../FallbackVisitor.ts';
import JSONSchemaElement from '../../../elements/JSONSchema.ts';
import { isJSONSchemaElement } from '../../../predicates.ts';

/**
* @public
*/
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 @@ -28,6 +45,54 @@ class JSONSchemaVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
this.element = new JSONSchemaElement();
this.specPath = always(['document', 'objects', 'JSONSchema']);
}

// eslint-disable-next-line class-methods-use-this
get default$schema(): string {
char0n marked this conversation as resolved.
Show resolved Hide resolved
return 'http://json-schema.org/draft-04/schema#';
}

ObjectElement(objectElement: ObjectElement) {
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);
}

handleDialectIdentifier(objectElement: ObjectElement): void {
// handle $schema keyword in embedded resources
if (isUndefined(this.parent) && !isStringElement(objectElement.get('$schema'))) {
// no parent available and no $schema is defined, set default $schema
this.element.setMetaProperty('inheritedDialectIdentifier', this.default$schema);
} else if (isJSONSchemaElement(this.parent) && !isStringElement(objectElement.get('$schema'))) {
// parent is available and no $schema is defined, set parent $schema
const inheritedDialectIdentifier = defaultTo(
toValue(this.parent.meta.get('inheritedDialectIdentifier')),
toValue(this.parent.$schema),
);
this.element.setMetaProperty('inheritedDialectIdentifier', inheritedDialectIdentifier);
}
}

handleSchemaIdentifier(objectElement: ObjectElement): void {
char0n marked this conversation as resolved.
Show resolved Hide resolved
// handle id keyword in embedded resources
// fetch parent's ancestorsSchemaIdentifiers
const ancestorsSchemaIdentifiers =
this.parent !== undefined
? cloneDeep(this.parent.getMetaProperty('ancestorsSchemaIdentifiers', []))
: new ArrayElement();
// get current id keyword
const id = toValue(objectElement.get('id'));

// remember id keyword if it's a non-empty strings
if (isNonEmptyString(id)) {
ancestorsSchemaIdentifiers.push(id);
}

this.element.setMetaProperty('ancestorsSchemaIdentifiers', ancestorsSchemaIdentifiers);
}
}

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 4 shape should refract to JSONSchema Element 1`] = `
{
"element": "JSONSchemaDraft4",
"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 4 shape shou
"content": "additionalItems"
},
"value": {
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
},
Expand Down Expand Up @@ -300,7 +320,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
},
"content": [
{
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
]
}
Expand Down Expand Up @@ -521,7 +550,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
"content": "prop1"
},
"value": {
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
}
Expand All @@ -548,7 +586,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
"content": "additionalProperties"
},
"value": {
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
},
Expand Down Expand Up @@ -603,7 +650,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
"content": "[a-z]+"
},
"value": {
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
}
Expand Down Expand Up @@ -662,7 +718,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
"content": "dep1"
},
"value": {
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
}
Expand Down Expand Up @@ -786,7 +851,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
},
"content": [
{
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
]
}
Expand Down Expand Up @@ -825,7 +899,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
},
"content": [
{
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
]
}
Expand Down Expand Up @@ -864,7 +947,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
},
"content": [
{
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
]
}
Expand All @@ -889,7 +981,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
"content": "not"
},
"value": {
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
},
Expand Down Expand Up @@ -944,7 +1045,16 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 4 shape shou
"content": "def1"
},
"value": {
"element": "JSONSchemaDraft4"
"element": "JSONSchemaDraft4",
"meta": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-04/schema#"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
}
}
}
Expand Down
Loading