Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue where allOf schemas with both having enum were not resolved correctly. #778

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/deref.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
});

// Need to handle collaping of folders
it.skip('Should generate collection with collapsing unnecessary folders ' +

Check warning on line 305 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (14.x)

Unexpected skipped mocha test

Check warning on line 305 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (16.x)

Unexpected skipped mocha test

Check warning on line 305 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (18.x)

Unexpected skipped mocha test
multipleFoldersSpec, function(done) {
var openapi = fs.readFileSync(multipleFoldersSpec, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, {}, (err, conversionResult) => {
Expand All @@ -314,7 +314,7 @@
done();
});
});
it.skip('Should collapse child and parent folder when parent has only one child' +

Check warning on line 317 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (14.x)

Unexpected skipped mocha test

Check warning on line 317 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (16.x)

Unexpected skipped mocha test

Check warning on line 317 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (18.x)

Unexpected skipped mocha test
multipleFoldersSpec1, function(done) {
var openapi = fs.readFileSync(multipleFoldersSpec1, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
Expand All @@ -328,7 +328,7 @@
done();
});
});
it.skip('Should generate collection without creating folders and having only one request' +

Check warning on line 331 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (14.x)

Unexpected skipped mocha test

Check warning on line 331 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (16.x)

Unexpected skipped mocha test

Check warning on line 331 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (18.x)

Unexpected skipped mocha test
multipleFoldersSpec2, function(done) {
var openapi = fs.readFileSync(multipleFoldersSpec2, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
Expand Down Expand Up @@ -2069,6 +2069,28 @@
});
});

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
Original file line number Diff line number Diff line change
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