Skip to content

Commit 2b06ebd

Browse files
authored
Merge pull request #778 from postmanlabs/fix/merge-allof-enum-issue
Fixed an issue where allOf schemas with both having enum were not resolved correctly.
2 parents b7439b8 + 436b08a commit 2b06ebd

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

lib/deref.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ module.exports = {
133133
}), {
134134
resolvers: {
135135
// for keywords in OpenAPI schema that are not standard defined JSON schema keywords, use default resolver
136-
defaultResolver: (compacted) => { return compacted[0]; }
136+
defaultResolver: (compacted) => { return compacted[0]; },
137+
138+
// Default resolver seems to fail for enum, so adding custom resolver that will return all unique enum values
139+
enum: (values) => {
140+
return _.uniq(_.concat(...values));
141+
}
137142
}
138143
});
139144
}

libV2/schemaUtils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,10 @@ let QUERYPARAM = 'query',
458458
}), {
459459
resolvers: {
460460
// for keywords in OpenAPI schema that are not standard defined JSON schema keywords, use default resolver
461-
defaultResolver: (compacted) => { return compacted[0]; }
461+
defaultResolver: (compacted) => { return compacted[0]; },
462+
enum: (values) => {
463+
return _.uniq(_.concat(...values));
464+
}
462465
}
463466
});
464467
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
responses:
17+
'200':
18+
description: An paged array of pets
19+
headers:
20+
x-next:
21+
description: A link to the next page of responses
22+
schema:
23+
type: string
24+
content:
25+
application/json:
26+
schema:
27+
$ref: "#/components/schemas/StatusSEP24Test"
28+
default:
29+
description: unexpected error
30+
content:
31+
application/json:
32+
schema:
33+
$ref: "#/components/schemas/Error"
34+
components:
35+
schemas:
36+
StatusSEPGeneric:
37+
type: string
38+
description: Statuses common for all SEP transactions
39+
enum:
40+
- incomplete
41+
- completed
42+
- refunded
43+
StatusSEP24Test:
44+
description: Possible status value for SEP-24 transactions
45+
properties:
46+
status:
47+
allOf:
48+
- $ref: '#/components/schemas/StatusSEPGeneric'
49+
- type: string
50+
description: Unique SEP-24 statuses
51+
enum:
52+
- no_market
53+
- too_small
54+
- too_large
55+
Error:
56+
required:
57+
- code
58+
- message
59+
properties:
60+
code:
61+
type: integer
62+
format: int32
63+
message:
64+
type: string

test/unit/convertV2.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,6 +2069,28 @@ describe('The convert v2 Function', function() {
20692069
});
20702070
});
20712071

2072+
it('[GITHUB #417] - should convert file with enum as conflicts while merging allOf keyword', function() {
2073+
const fileSource = path.join(__dirname, VALID_OPENAPI_PATH, 'enumAllOfConflicts.yaml'),
2074+
fileData = fs.readFileSync(fileSource, 'utf8'),
2075+
input = {
2076+
type: 'string',
2077+
data: fileData
2078+
};
2079+
2080+
Converter.convertV2(input, {
2081+
optimizeConversion: false
2082+
}, (err, result) => {
2083+
const expectedResponseBody = JSON.parse(result.output[0].data.item[0].item[0].response[0].body);
2084+
expect(err).to.be.null;
2085+
expect(result.result).to.be.true;
2086+
expect(expectedResponseBody).to.be.an('object');
2087+
expect(expectedResponseBody).to.have.property('status');
2088+
expect(expectedResponseBody.status).to.be.a('string');
2089+
expect(expectedResponseBody.status).to.be.oneOf(['no_market', 'too_small', 'too_large',
2090+
'incomplete', 'completed', 'refunded']);
2091+
});
2092+
});
2093+
20722094
it('Should convert a swagger document with XML example correctly', function(done) {
20732095
const fileData = fs.readFileSync(path.join(__dirname, SWAGGER_20_FOLDER_YAML, 'xml_example.yaml'), 'utf8'),
20742096
input = {

test/unit/deref.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ describe('DEREF FUNCTION TESTS ', function() {
377377
'type': 'string',
378378
'format': 'uuid'
379379
},
380+
'status': {
381+
'type': 'string',
382+
'enum': ['incomplete', 'completed', 'refunded']
383+
},
380384
'actionId': { 'type': 'integer', 'minimum': 5 },
381385
'result': { 'type': 'object' }
382386
},
@@ -390,6 +394,10 @@ describe('DEREF FUNCTION TESTS ', function() {
390394
'err': { 'type': 'string' },
391395
'data': { 'type': 'object' }
392396
}
397+
},
398+
'status': {
399+
'type': 'string',
400+
'enum': ['no_market', 'too_small', 'too_large']
393401
}
394402
}
395403
}
@@ -408,6 +416,10 @@ describe('DEREF FUNCTION TESTS ', function() {
408416
type: 'string',
409417
format: 'uuid'
410418
},
419+
status: {
420+
type: 'string',
421+
enum: ['incomplete', 'completed', 'refunded', 'no_market', 'too_small', 'too_large']
422+
},
411423
actionId: { 'type': 'integer', 'minimum': 5 },
412424
result: {
413425
type: 'object',

0 commit comments

Comments
 (0)