Skip to content

Commit

Permalink
refactor: use methods defined for draft 6 visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud committed Jan 17, 2025
1 parent 3ee7575 commit 39b2a2b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 81 deletions.
6 changes: 2 additions & 4 deletions packages/apidom-ns-json-schema-draft-6/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ export type {
default as LinkDescriptionVisitor,
LinkDescriptionVisitorOptions,
} from './refractor/visitors/json-schema/link-description/index.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 ExamplesVisitor,
ExamplesVisitorOptions,
Expand Down
6 changes: 2 additions & 4 deletions packages/apidom-ns-json-schema-draft-7/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ export type {
JSONSchemaDraft4ItemsVisitor,
} from '@swagger-api/apidom-ns-json-schema-draft-6';

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 LinkDescriptionVisitor,
LinkDescriptionVisitorOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Mixin } from 'ts-mixer';
import { always, defaultTo } from 'ramda';
import { isNonEmptyString, isUndefined } from 'ramda-adjunct';
import {
ObjectElement,
BooleanElement,
ArrayElement,
isStringElement,
cloneDeep,
toValue,
} from '@swagger-api/apidom-core';
import { always } from 'ramda';
import { ObjectElement, BooleanElement } from '@swagger-api/apidom-core';
import {
FixedFieldsVisitor,
FixedFieldsVisitorOptions,
Expand All @@ -17,10 +9,10 @@ import {
FallbackVisitor,
FallbackVisitorOptions,
SpecPath,
JSONSchemaVisitor as JSONSchemaDraft6Visitor,
} from '@swagger-api/apidom-ns-json-schema-draft-6';

import JSONSchemaElement from '../../../elements/JSONSchema.ts';
import { isJSONSchemaElement } from '../../../predicates.ts';

/**
* @public
Expand All @@ -42,17 +34,20 @@ class JSONSchemaVisitor extends Mixin(

declare protected readonly specPath: SpecPath<['document', 'objects', 'JSONSchema']>;

protected readonly default$schema = 'http://json-schema.org/draft-07/schema#';

constructor(options: JSONSchemaVisitorOptions) {
super(options);
this.specPath = always(['document', 'objects', 'JSONSchema']);
}

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

ObjectElement(objectElement: ObjectElement) {
this.element = new JSONSchemaElement();
this.handle$schema(objectElement);
this.handle$id(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;
Expand All @@ -67,37 +62,16 @@ class JSONSchemaVisitor extends Mixin(
return result;
}

handle$schema(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('inherited$schema', this.default$schema);
} else if (isJSONSchemaElement(this.parent) && !isStringElement(objectElement.get('$schema'))) {
// parent is available and no $schema is defined, set parent $schema
const inherited$schema = defaultTo(
toValue(this.parent.meta.get('inherited$schema')),
toValue(this.parent.$schema),
);
this.element.setMetaProperty('inherited$schema', inherited$schema);
}
handleDialectIdentifier(objectElement: ObjectElement): void {
return JSONSchemaDraft6Visitor.prototype.handleDialectIdentifier.call(this, objectElement);
}

handle$id(objectElement: ObjectElement): void {
// handle $id keyword in embedded resources
// fetch parent's inherited$id
const inherited$id =
this.parent !== undefined
? cloneDeep(this.parent.getMetaProperty('inherited$id', []))
: 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)) {
inherited$id.push($id);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
{
"element": "JSONSchemaDraft7",
"meta": {
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array",
"content": [
{
Expand Down Expand Up @@ -276,11 +276,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
"value": {
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -322,11 +322,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
{
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -428,11 +428,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
"value": {
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -584,11 +584,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
"value": {
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -620,11 +620,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
"value": {
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -684,11 +684,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
"value": {
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -752,11 +752,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
"value": {
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -788,11 +788,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
"value": {
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -941,11 +941,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
{
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -989,11 +989,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
{
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -1037,11 +1037,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
{
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -1071,11 +1071,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
"value": {
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down Expand Up @@ -1135,11 +1135,11 @@ exports[`refractor given generic ApiDOM object in JSON Schema Draft 7 shape shou
"value": {
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ exports[`refractor elements Boolean JSON Schema support should refract to semant
{
"element": "JSONSchemaDraft7",
"meta": {
"inherited$schema": {
"inheritedDialectIdentifier": {
"element": "string",
"content": "http://json-schema.org/draft-07/schema#"
},
"inherited$id": {
"ancestorsSchemaIdentifiers": {
"element": "array"
}
},
Expand Down

0 comments on commit 39b2a2b

Please sign in to comment.