Skip to content

Commit

Permalink
fix(ns-json-schema-draft-7): add support for headerSchema (#4652)
Browse files Browse the repository at this point in the history
Refs #1819
  • Loading branch information
glowcloud authored Jan 16, 2025
1 parent 980073c commit 81682cc
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { StringElement, ObjectElement, ArrayElement } from '@swagger-api/apidom-core';
import {
StringElement,
ObjectElement,
ArrayElement,
BooleanElement,
} from '@swagger-api/apidom-core';
import { UnsupportedOperationError } from '@swagger-api/apidom-error';
import { LinkDescriptionElement } from '@swagger-api/apidom-ns-json-schema-draft-6';
import {
LinkDescriptionElement,
JSONReferenceElement,
} from '@swagger-api/apidom-ns-json-schema-draft-6';

import JSONSchema from './JSONSchema.ts';

/* eslint-disable class-methods-use-this */

Expand Down Expand Up @@ -104,6 +114,19 @@ class LinkDescription extends LinkDescriptionElement {
this.set('$comment', $comment);
}

/**
* Link Input.
*
* URI: https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-hyperschema-01#section-6.6
*/
get headerSchema(): JSONSchema | BooleanElement | JSONReferenceElement | undefined {
return this.get('headerSchema');
}

set headerSchema(headerSchema: JSONSchema | BooleanElement | JSONReferenceElement | undefined) {
this.set('headerSchema', headerSchema);
}

/**
* Submitting Data for Processing.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ const schema = {
targetHints(...args: any[]) {
return new ObjectElement(...args);
},
headerSchema(...args: any[]) {
return new JSONSchemaElement(...args);
},
},
'json-schema-properties': {
'[key: *]': function key(...args: any[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ const specification = pipe(
['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', '$comment'],
specificationObj.visitors.value,
),
assocPath(
['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', 'headerSchema'],
specificationObj.visitors.JSONSchemaOrJSONReferenceVisitor,
),
dissocPath([
'visitors',
'document',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ exports[`refractor elements LinkDescription should refract to semantic ApiDOM tr
(MemberElement
(StringElement)
(JSONSchemaDraft7Element))
(MemberElement
(StringElement)
(JSONSchemaDraft7Element))
(MemberElement
(StringElement)
(StringElement))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('refractor', function () {
templateRequired: ['id'],
href: 'things/{id}',
hrefSchema: {},
headerSchema: {},
rel: 'image/png',
title: 'title',
targetSchema: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ exports[`given JSON Schema definition with no empty values should do nothing 1`]
(JSONSchemaDraft7Element)))))
`;

exports[`given empty value for LinkDescription.headerSchema field should replace empty value with semantic element 1`] = `
(JSONSchemaDraft7Element
(MemberElement
(StringElement)
(StringElement))
(MemberElement
(StringElement)
(ArrayElement
(LinkDescriptionElement
(MemberElement
(StringElement)
(JSONSchemaDraft7Element))))))
`;

exports[`given empty value for LinkDescription.hrefSchema field should replace empty value with semantic element 1`] = `
(JSONSchemaDraft7Element
(MemberElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,22 @@ describe('given empty value for LinkDescription.targetHints field', function ()
});
});

describe('given empty value for LinkDescription.headerSchema field', function () {
it('should replace empty value with semantic element', async function () {
const yamlDefinition = dedent`
$schema: 'http://json-schema.org/draft-07/schema#'
links:
- headerSchema:
`;
const apiDOM = await parse(yamlDefinition);
const jsonSchemaElement = JSONSchemaElement.refract(apiDOM.result, {
plugins: [refractorPluginReplaceEmptyElement()],
}) as JSONSchemaElement;

expect(sexprs(jsonSchemaElement)).toMatchSnapshot();
});
});

describe('given JSON Schema definition with no empty values', function () {
it('should do nothing', async function () {
const yamlDefinition = dedent`
Expand Down

0 comments on commit 81682cc

Please sign in to comment.