Skip to content

Commit

Permalink
Handle additional OAS keywords (#141)
Browse files Browse the repository at this point in the history
* log errors in strict mode

* add changeset

* fix typos

* change changeset message

* change title

* update comment

* remove unused line

* add line between functions

---------

Co-authored-by: nthapa <[email protected]>
  • Loading branch information
nabinked and nthapa-atl authored Oct 5, 2023
1 parent 35ee143 commit e81f606
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-falcons-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'oas3-chow-chow': patch
---

handle additional open api keywords
51 changes: 51 additions & 0 deletions __test__/chow-keywords.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { OpenAPIObject } from 'openapi3-ts';
import ChowChow from '../src';
const doc: (additionalKeywords: Record<string, any>) => OpenAPIObject = (
additionalKeywords = {}
) => ({
openapi: '3.0.1',
info: {
title: 'service open api spec',
version: '1.0.1',
},
components: {
schemas: {
ResolveUnsupportedError: {
type: 'object',
properties: {
error: {},
},
...additionalKeywords,
},
},
},
paths: {
'/resolve': {
post: {
operationId: 'resolve',
responses: {
'404': {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/ResolveUnsupportedError',
},
},
},
},
},
},
},
},
});

describe('additional open api keywords', () => {
it.each([
{ discriminator: '' },
{ example: '' },
{ externalDocs: '' },
{ xml: '' },
])('"%s" keyword should be allowed by default', async (additionalKeyword) => {
expect(await ChowChow.create(doc(additionalKeyword))).toBeDefined();
});
});
49 changes: 49 additions & 0 deletions __test__/chow-strict.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { OpenAPIObject, PathItemObject } from 'openapi3-ts';
import ChowChow from '../src';

/**
* https://ajv.js.org/strict-mode.html
*/
describe('strict mode', () => {
it('show log warn and not throw by default', async () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation();
const doc: OpenAPIObject = {
openapi: '3.0.1',
info: {
title: 'service open api spec',
version: '1.0.1',
},
components: {
schemas: {
ResolveUnsupportedError: {
type: 'array',
additionalItems: false,
},
},
},
paths: {
'/resolve': {
post: {
operationId: 'resolve',
responses: {
'404': {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/ResolveUnsupportedError',
},
},
},
},
},
},
},
},
};
expect(await ChowChow.create(doc)).toBeDefined();
expect(warnSpy).toHaveBeenCalledWith(
'strict mode: "additionalItems" is ignored when "items" is not an array of schemas'
);
warnSpy.mockRestore();
});
});
18 changes: 16 additions & 2 deletions src/compiler/CompiledSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ export default class CompiledSchema {
private validator: Ajv.ValidateFunction;

constructor(schema: SchemaObject, opts?: Ajv.Options, context?: any) {
this.schemaObject = schema;
/**
* Remove unsupported additional OpenAPI keywords.
* https://swagger.io/docs/specification/data-models/keywords/ See "Additional Keywords"
* Does not include all keywords listed in that page/section because some of them are supported by ajv https://ajv.js.org/json-schema.html#openapi-support
* and some are explictly added within this class.
*/
const {
discriminator,
example,
externalDocs,
xml,
...schemaObject
} = schema;
this.schemaObject = schemaObject;

const ajvInstance = ajv(opts);
ajvInstance.removeKeyword('writeOnly');
ajvInstance.removeKeyword('readOnly');
Expand All @@ -22,7 +36,7 @@ export default class CompiledSchema {
validate: (schema: any) =>
schema ? context.schemaContext === 'response' : true,
});
this.validator = ajvInstance.compile(schema);
this.validator = ajvInstance.compile(schemaObject);
}

public validate(value: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ajv, { Options } from 'ajv';
import addFormats from 'ajv-formats';

const options: Options = {};
const options: Options = { strict: 'log' };

export default function ajv(opts: Options = {}) {
const ajv = new Ajv({
Expand Down

0 comments on commit e81f606

Please sign in to comment.