From 1ecfaf593829801185563c0754525fccc7cf36fe Mon Sep 17 00:00:00 2001 From: Yoshihiro Kameda Date: Tue, 5 May 2020 13:41:39 +0900 Subject: [PATCH] feat(oas3): add support for schema example in oas3 parameters --- .../lib/parser/oas/parseParameterObject.js | 13 +- .../test/integration/fixtures/petstore.json | 120 ------------------ .../fixtures/petstore.sourcemap.json | 120 ------------------ .../parser/oas/parseParameterObject-test.js | 28 ++-- 4 files changed, 26 insertions(+), 255 deletions(-) diff --git a/packages/openapi3-parser/lib/parser/oas/parseParameterObject.js b/packages/openapi3-parser/lib/parser/oas/parseParameterObject.js index 681af8e4b..28d357723 100644 --- a/packages/openapi3-parser/lib/parser/oas/parseParameterObject.js +++ b/packages/openapi3-parser/lib/parser/oas/parseParameterObject.js @@ -1,6 +1,6 @@ const R = require('ramda'); const { - isExtension, hasKey, hasValue, getValue, + isObject, isExtension, hasKey, hasValue, getValue, } = require('../../predicates'); const { createError, @@ -17,7 +17,7 @@ const name = 'Parameter Object'; const requiredKeys = ['name', 'in']; const unsupportedKeys = [ 'deprecated', 'allowEmptyValue', 'style', 'allowReserved', - 'schema', 'examples', 'content', + 'examples', 'content', ]; const isUnsupportedKey = R.anyPass(R.map(hasKey, unsupportedKeys)); @@ -102,6 +102,7 @@ function parseParameterObject(context, object) { [hasKey('required'), parseBoolean(context, name, false)], [hasKey('explode'), parseBoolean(context, name, false)], [hasKey('example'), e => e.clone()], + [hasKey('schema'), e => e.clone()], [isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)], @@ -163,7 +164,13 @@ function parseParameterObject(context, object) { R.when(hasLocation('header'), validateHeaderName), R.when(hasExplodeWithoutQueryIn, attachWarning(createUnsupportedExplodeWarning)), (parameter) => { - const example = parameter.get('example'); + let example = parameter.get('example'); + if (!example) { + const schema = parameter.get('schema'); + if (schema && isObject(schema)) { + example = schema.get('example'); + } + } const member = new namespace.elements.Member(parameter.get('name'), example); const description = parameter.get('description'); diff --git a/packages/openapi3-parser/test/integration/fixtures/petstore.json b/packages/openapi3-parser/test/integration/fixtures/petstore.json index 6600c6ea8..2eb09b7f9 100644 --- a/packages/openapi3-parser/test/integration/fixtures/petstore.json +++ b/packages/openapi3-parser/test/integration/fixtures/petstore.json @@ -857,66 +857,6 @@ }, "content": "'Operation Object' contains unsupported key 'tags'" }, - { - "element": "annotation", - "meta": { - "classes": { - "element": "array", - "content": [ - { - "element": "string", - "content": "warning" - } - ] - } - }, - "attributes": { - "sourceMap": { - "element": "array", - "content": [ - { - "element": "sourceMap", - "content": [ - { - "element": "array", - "content": [ - { - "element": "number", - "attributes": { - "line": { - "element": "number", - "content": 21 - }, - "column": { - "element": "number", - "content": 11 - } - }, - "content": 414 - }, - { - "element": "number", - "attributes": { - "line": { - "element": "number", - "content": 21 - }, - "column": { - "element": "number", - "content": 17 - } - }, - "content": 6 - } - ] - } - ] - } - ] - } - }, - "content": "'Parameter Object' contains unsupported key 'schema'" - }, { "element": "annotation", "meta": { @@ -1097,66 +1037,6 @@ }, "content": "'Operation Object' contains unsupported key 'tags'" }, - { - "element": "annotation", - "meta": { - "classes": { - "element": "array", - "content": [ - { - "element": "string", - "content": "warning" - } - ] - } - }, - "attributes": { - "sourceMap": { - "element": "array", - "content": [ - { - "element": "sourceMap", - "content": [ - { - "element": "array", - "content": [ - { - "element": "number", - "attributes": { - "line": { - "element": "number", - "content": 62 - }, - "column": { - "element": "number", - "content": 9 - } - }, - "content": 1504 - }, - { - "element": "number", - "attributes": { - "line": { - "element": "number", - "content": 62 - }, - "column": { - "element": "number", - "content": 15 - } - }, - "content": 6 - } - ] - } - ] - } - ] - } - }, - "content": "'Parameter Object' contains unsupported key 'schema'" - }, { "element": "annotation", "meta": { diff --git a/packages/openapi3-parser/test/integration/fixtures/petstore.sourcemap.json b/packages/openapi3-parser/test/integration/fixtures/petstore.sourcemap.json index ab8f48b55..97a4fa014 100644 --- a/packages/openapi3-parser/test/integration/fixtures/petstore.sourcemap.json +++ b/packages/openapi3-parser/test/integration/fixtures/petstore.sourcemap.json @@ -1807,66 +1807,6 @@ }, "content": "'Operation Object' contains unsupported key 'tags'" }, - { - "element": "annotation", - "meta": { - "classes": { - "element": "array", - "content": [ - { - "element": "string", - "content": "warning" - } - ] - } - }, - "attributes": { - "sourceMap": { - "element": "array", - "content": [ - { - "element": "sourceMap", - "content": [ - { - "element": "array", - "content": [ - { - "element": "number", - "attributes": { - "line": { - "element": "number", - "content": 21 - }, - "column": { - "element": "number", - "content": 11 - } - }, - "content": 414 - }, - { - "element": "number", - "attributes": { - "line": { - "element": "number", - "content": 21 - }, - "column": { - "element": "number", - "content": 17 - } - }, - "content": 6 - } - ] - } - ] - } - ] - } - }, - "content": "'Parameter Object' contains unsupported key 'schema'" - }, { "element": "annotation", "meta": { @@ -2047,66 +1987,6 @@ }, "content": "'Operation Object' contains unsupported key 'tags'" }, - { - "element": "annotation", - "meta": { - "classes": { - "element": "array", - "content": [ - { - "element": "string", - "content": "warning" - } - ] - } - }, - "attributes": { - "sourceMap": { - "element": "array", - "content": [ - { - "element": "sourceMap", - "content": [ - { - "element": "array", - "content": [ - { - "element": "number", - "attributes": { - "line": { - "element": "number", - "content": 62 - }, - "column": { - "element": "number", - "content": 9 - } - }, - "content": 1504 - }, - { - "element": "number", - "attributes": { - "line": { - "element": "number", - "content": 62 - }, - "column": { - "element": "number", - "content": 15 - } - }, - "content": 6 - } - ] - } - ] - } - ] - } - }, - "content": "'Parameter Object' contains unsupported key 'schema'" - }, { "element": "annotation", "meta": { diff --git a/packages/openapi3-parser/test/unit/parser/oas/parseParameterObject-test.js b/packages/openapi3-parser/test/unit/parser/oas/parseParameterObject-test.js index 34d67ac21..26da40c4a 100644 --- a/packages/openapi3-parser/test/unit/parser/oas/parseParameterObject-test.js +++ b/packages/openapi3-parser/test/unit/parser/oas/parseParameterObject-test.js @@ -315,6 +315,22 @@ describe('Parameter Object', () => { bar: 'baz', }); }); + + it('attaches integer example to member of schema object', () => { + const parameter = new namespace.elements.Object({ + name: 'schema_example', + in: 'query', + schema: { type: 'string', example: 10 }, + }); + + const parseResult = parse(context, parameter); + + console.log(parseResult.get(0)); + + expect(parseResult.length).to.equal(1); + expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Member); + expect(parseResult.get(0).value.toValue()).to.equal(10); + }); }); describe('#explode', () => { @@ -518,18 +534,6 @@ describe('Parameter Object', () => { expect(parseResult).to.contain.warning("'Parameter Object' contains unsupported key 'allowReserved'"); }); - it('provides warning for unsupported schema property', () => { - const parameter = new namespace.elements.Object({ - name: 'example', - in: 'query', - schema: { type: 'string' }, - }); - - const parseResult = parse(context, parameter); - - expect(parseResult).to.contain.warning("'Parameter Object' contains unsupported key 'schema'"); - }); - it('provides warning for unsupported examples property', () => { const parameter = new namespace.elements.Object({ name: 'direction',