From a26279fdbc83b6ade737ae632440a01d341646e9 Mon Sep 17 00:00:00 2001 From: Vishal Shingala Date: Fri, 12 Jan 2024 12:46:11 +0530 Subject: [PATCH 1/2] Fixed an issue where allOf schemas with both having enum were not resolved correctly --- lib/deref.js | 7 ++++++- libV2/schemaUtils.js | 5 ++++- test/unit/convertV2.test.js | 22 ++++++++++++++++++++++ test/unit/deref.test.js | 12 ++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/deref.js b/lib/deref.js index 8cd61107c..7bbcabbdc 100644 --- a/lib/deref.js +++ b/lib/deref.js @@ -133,7 +133,12 @@ module.exports = { }), { resolvers: { // for keywords in OpenAPI schema that are not standard defined JSON schema keywords, use default resolver - defaultResolver: (compacted) => { return compacted[0]; } + defaultResolver: (compacted) => { return compacted[0]; }, + + // Default resolver seems to fail for enum, so adding custom resolver that will return all unique enum values + enum: (values) => { + return _.uniq(_.concat(...values)); + } } }); } diff --git a/libV2/schemaUtils.js b/libV2/schemaUtils.js index 64a688879..1e3be3340 100644 --- a/libV2/schemaUtils.js +++ b/libV2/schemaUtils.js @@ -458,7 +458,10 @@ let QUERYPARAM = 'query', }), { resolvers: { // for keywords in OpenAPI schema that are not standard defined JSON schema keywords, use default resolver - defaultResolver: (compacted) => { return compacted[0]; } + defaultResolver: (compacted) => { return compacted[0]; }, + enum: (values) => { + return _.uniq(_.concat(...values)); + } } }); } diff --git a/test/unit/convertV2.test.js b/test/unit/convertV2.test.js index 19e77cfd5..194f9903f 100644 --- a/test/unit/convertV2.test.js +++ b/test/unit/convertV2.test.js @@ -2069,6 +2069,28 @@ describe('The convert v2 Function', function() { }); }); + it('[GITHUB #417] - should convert file with enum as conflicts while merging allOf keyword', function() { + const fileSource = path.join(__dirname, VALID_OPENAPI_PATH, 'enumAllOfConflicts.yaml'), + fileData = fs.readFileSync(fileSource, 'utf8'), + input = { + type: 'string', + data: fileData + }; + + Converter.convertV2(input, { + optimizeConversion: false + }, (err, result) => { + const expectedResponseBody = JSON.parse(result.output[0].data.item[0].item[0].response[0].body); + expect(err).to.be.null; + expect(result.result).to.be.true; + expect(expectedResponseBody).to.be.an('object'); + expect(expectedResponseBody).to.have.property('status'); + expect(expectedResponseBody.status).to.be.a('string'); + expect(expectedResponseBody.status).to.be.oneOf(['no_market', 'too_small', 'too_large', + 'incomplete', 'completed', 'refunded']); + }); + }); + it('Should convert a swagger document with XML example correctly', function(done) { const fileData = fs.readFileSync(path.join(__dirname, SWAGGER_20_FOLDER_YAML, 'xml_example.yaml'), 'utf8'), input = { diff --git a/test/unit/deref.test.js b/test/unit/deref.test.js index 7139cb0f8..9d99a5b0a 100644 --- a/test/unit/deref.test.js +++ b/test/unit/deref.test.js @@ -377,6 +377,10 @@ describe('DEREF FUNCTION TESTS ', function() { 'type': 'string', 'format': 'uuid' }, + 'status': { + 'type': 'string', + 'enum': ['incomplete', 'completed', 'refunded'] + }, 'actionId': { 'type': 'integer', 'minimum': 5 }, 'result': { 'type': 'object' } }, @@ -390,6 +394,10 @@ describe('DEREF FUNCTION TESTS ', function() { 'err': { 'type': 'string' }, 'data': { 'type': 'object' } } + }, + 'status': { + 'type': 'string', + 'enum': ['no_market', 'too_small', 'too_large'] } } } @@ -408,6 +416,10 @@ describe('DEREF FUNCTION TESTS ', function() { type: 'string', format: 'uuid' }, + status: { + type: 'string', + enum: ['incomplete', 'completed', 'refunded', 'no_market', 'too_small', 'too_large'] + }, actionId: { 'type': 'integer', 'minimum': 5 }, result: { type: 'object', From 436b08a0c65a089a000cc9595472dc56a3b101eb Mon Sep 17 00:00:00 2001 From: Vishal Shingala Date: Fri, 12 Jan 2024 13:09:17 +0530 Subject: [PATCH 2/2] Added test file --- .../valid_openapi/enumAllOfConflicts.yaml | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 test/data/valid_openapi/enumAllOfConflicts.yaml diff --git a/test/data/valid_openapi/enumAllOfConflicts.yaml b/test/data/valid_openapi/enumAllOfConflicts.yaml new file mode 100644 index 000000000..e7ddc3cde --- /dev/null +++ b/test/data/valid_openapi/enumAllOfConflicts.yaml @@ -0,0 +1,64 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Swagger Petstore + license: + name: MIT +servers: + - url: http://petstore.swagger.io/v1 +paths: + /pets: + get: + summary: List all pets + operationId: listPets + tags: + - pets + responses: + '200': + description: An paged array of pets + headers: + x-next: + description: A link to the next page of responses + schema: + type: string + content: + application/json: + schema: + $ref: "#/components/schemas/StatusSEP24Test" + default: + description: unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" +components: + schemas: + StatusSEPGeneric: + type: string + description: Statuses common for all SEP transactions + enum: + - incomplete + - completed + - refunded + StatusSEP24Test: + description: Possible status value for SEP-24 transactions + properties: + status: + allOf: + - $ref: '#/components/schemas/StatusSEPGeneric' + - type: string + description: Unique SEP-24 statuses + enum: + - no_market + - too_small + - too_large + Error: + required: + - code + - message + properties: + code: + type: integer + format: int32 + message: + type: string