Skip to content

Commit

Permalink
fix(ns-json-schema): collect properly metadata for dialect and schema…
Browse files Browse the repository at this point in the history
… identifiers (#4689)
  • Loading branch information
char0n authored Jan 26, 2025
1 parent ee2045e commit a69a698
Show file tree
Hide file tree
Showing 12 changed files with 786 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -340,7 +346,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -446,7 +458,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -602,7 +620,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -638,7 +662,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -702,7 +732,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -770,7 +806,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -806,7 +848,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -959,7 +1007,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -1007,7 +1061,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -1055,7 +1115,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -1089,7 +1155,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -1153,7 +1225,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -1561,7 +1639,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -1593,7 +1677,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down Expand Up @@ -1751,7 +1841,13 @@ exports[`refractor given generic ApiDOM object in JSON Schema 2019-09 shape shou
"content": "https://json-schema.org/draft/2019-09/schema"
},
"ancestorsSchemaIdentifiers": {
"element": "array"
"element": "array",
"content": [
{
"element": "string",
"content": "http://x.y.z/rootschema.json#"
}
]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert, expect } from 'chai';
import { ObjectElement, sexprs, toValue } from '@swagger-api/apidom-core';
import { ObjectElement, sexprs, toValue, find, isElement } from '@swagger-api/apidom-core';

import { JSONSchemaElement } from '../../../../src/index.ts';
import { JSONSchemaElement, isJSONSchemaElement } from '../../../../src/index.ts';

describe('refractor', function () {
context('elements', function () {
Expand Down Expand Up @@ -118,6 +118,64 @@ describe('refractor', function () {
});
});

context('given JSONSchema ancestors are embedded resources', function () {
specify('should expose ancestors schema identifiers as metadata', function () {
const jsonSchemaElement = JSONSchemaElement.refract({
type: 'array',
oneOf: [
{
$id: '$id1',
type: 'number',
items: { $id: '$id2', type: 'object' },
},
],
items: {
type: 'string',
},
});
const foundJsonSchemaElement = find(
(e) => isJSONSchemaElement(e) && isElement(e.$id) && e.$id.equals('$id2'),
jsonSchemaElement,
);
const ancestorsSchemaIdentifiers = foundJsonSchemaElement!.meta.get(
'ancestorsSchemaIdentifiers',
);

assert.deepEqual(toValue(ancestorsSchemaIdentifiers), ['$id1', '$id2']);
});
});

context('given JSONSchema switches dialect via parent schema', function () {
specify('should expose ancestors schema dialect identifier as metadata', function () {
const jsonSchemaElement = JSONSchemaElement.refract({
type: 'object',
oneOf: [
{
type: 'number',
$schema: 'schema1',
items: { type: 'object' },
},
],
items: {
type: 'string',
},
}) as JSONSchemaElement;

assert.strictEqual(
toValue(jsonSchemaElement.meta.get('inheritedDialectIdentifier')),
'https://json-schema.org/draft/2019-09/schema',
);
assert.strictEqual(
toValue(jsonSchemaElement.items!.meta.get('inheritedDialectIdentifier')),
'https://json-schema.org/draft/2019-09/schema',
);
assert.strictEqual(
toValue(jsonSchemaElement.oneOf!.get(0).items.meta.get('inheritedDialectIdentifier')),
'schema1',
);
});
});

context('given $ref fields', function () {
specify('should refract to semantic ApiDOM tree', function () {
const jsonSchemaElement = JSONSchemaElement.refract({
Expand Down
Loading

0 comments on commit a69a698

Please sign in to comment.