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

fix(ns-json-schema-draft-7): add support for headerSchema #4652

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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,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
Loading