diff --git a/lib/deref.js b/lib/deref.js index 8cd61107..7bbcabbd 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 64a68887..1e3be334 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/data/valid_openapi/enumAllOfConflicts.yaml b/test/data/valid_openapi/enumAllOfConflicts.yaml new file mode 100644 index 00000000..e7ddc3cd --- /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 diff --git a/test/unit/convertV2.test.js b/test/unit/convertV2.test.js index 19e77cfd..194f9903 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 7139cb0f..9d99a5b0 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',