From 6348aa8382cb167a15955b995770f6a31489ceee Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Mon, 22 Feb 2021 22:25:03 +0000 Subject: [PATCH] feat(oas3): support Schema Object const --- .../lib/parser/oas/parseSchemaObject.js | 14 +++++++++ .../unit/parser/oas/parseSchemaObject-test.js | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/openapi3-parser/lib/parser/oas/parseSchemaObject.js b/packages/openapi3-parser/lib/parser/oas/parseSchemaObject.js index 5122d3f64..ccd07fcde 100644 --- a/packages/openapi3-parser/lib/parser/oas/parseSchemaObject.js +++ b/packages/openapi3-parser/lib/parser/oas/parseSchemaObject.js @@ -238,6 +238,12 @@ function parseSchema(context) { return element; }); + const parseConst = (value) => { + const element = value.clone(); + element.attributes.set('typeAttributes', ['fixed']); + return element; + }; + const parseMember = R.cond([ [hasKey('type'), R.compose(parseType(context), getValue)], [hasKey('enum'), R.compose(parseEnum(context, name), getValue)], @@ -251,6 +257,11 @@ function parseSchema(context) { [hasKey('example'), e => e.clone()], [hasKey('oneOf'), R.compose(parseOneOf, getValue)], + [ + R.both(hasKey('const'), R.always(context.isOpenAPIVersionMoreThanOrEqual(3, 1))), + R.compose(parseConst, getValue), + ], + [isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)], // Return a warning for additional properties @@ -265,11 +276,14 @@ function parseSchema(context) { let element; const oneOf = schema.get('oneOf'); + const constValue = schema.get('const'); const enumerations = schema.get('enum'); const type = schema.getValue('type') || []; if (oneOf) { element = oneOf; + } else if (constValue) { + element = constValue; } else if (enumerations) { element = enumerations; } else if (type.length > 1) { diff --git a/packages/openapi3-parser/test/unit/parser/oas/parseSchemaObject-test.js b/packages/openapi3-parser/test/unit/parser/oas/parseSchemaObject-test.js index df68f1611..057814fbb 100644 --- a/packages/openapi3-parser/test/unit/parser/oas/parseSchemaObject-test.js +++ b/packages/openapi3-parser/test/unit/parser/oas/parseSchemaObject-test.js @@ -310,6 +310,36 @@ describe('Schema Object', () => { }); }); + describe('#const', () => { + it('warns when used on OpenAPI 3.0', () => { + const schema = new namespace.elements.Object({ + const: { message: 'Hello' }, + }); + const parseResult = parse(context, schema); + + expect(parseResult).to.contain.warning( + "'Schema Object' contains invalid key 'const'" + ); + }); + + it('returns a fixed element', () => { + context.openapiVersion = { major: 3, minor: 1 }; + const schema = new namespace.elements.Object({ + const: { message: 'Hello' }, + }); + const parseResult = parse(context, schema); + + expect(parseResult.length).to.equal(1); + expect(parseResult.get(0)).to.be.instanceof(namespace.elements.DataStructure); + expect(parseResult).to.not.contain.annotations; + + const element = parseResult.get(0).content; + expect(element).to.be.instanceof(namespace.elements.Object); + expect(element.toValue()).to.deep.equal({ message: 'Hello' }); + expect(element.attributes.getValue('typeAttributes')).to.deep.equal(['fixed']); + }); + }); + describe('object type', () => { describe('#required', () => { it('warns when required is not an array', () => {