Skip to content

Commit

Permalink
Merge pull request #778 from postmanlabs/fix/merge-allof-enum-issue
Browse files Browse the repository at this point in the history
Fixed an issue where allOf schemas with both having enum were not resolved correctly.
  • Loading branch information
VShingala committed Jan 12, 2024
2 parents b7439b8 + 436b08a commit 2b06ebd
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/deref.js
Expand Up @@ -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));
}
}
});
}
Expand Down
5 changes: 4 additions & 1 deletion libV2/schemaUtils.js
Expand Up @@ -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));
}
}
});
}
Expand Down
64 changes: 64 additions & 0 deletions 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
22 changes: 22 additions & 0 deletions test/unit/convertV2.test.js
Expand Up @@ -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 = {
Expand Down
12 changes: 12 additions & 0 deletions test/unit/deref.test.js
Expand Up @@ -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' }
},
Expand All @@ -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']
}
}
}
Expand All @@ -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',
Expand Down

0 comments on commit 2b06ebd

Please sign in to comment.