Skip to content

parametersResolution: Handle ENUM properties #818

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

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion libV2/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ let QUERYPARAM = 'query',
if (schema.hasOwnProperty('type')) {
let { parametersResolution } = context.computedOptions;

// Override default value to schema for CONVERSION only for parmeter resolution set to schema
// Override default value to schema for CONVERSION only for parameter resolution set to schema
if (resolveFor === CONVERSION && parametersResolution === 'schema') {
if (!schema.hasOwnProperty('format')) {
schema.default = '<' + schema.type + '>';
Expand Down Expand Up @@ -722,6 +722,13 @@ let QUERYPARAM = 'query',
);
}
});

let { parametersResolution } = context.computedOptions;
if (resolveFor === CONVERSION && parametersResolution === 'schema') {
if (schema.hasOwnProperty('type')) {
schema.default = '<' + schema.type + '>';
}
}
}

// Keep track of readOnly and writeOnly properties to resolve request and responses accordingly later.
Expand Down
71 changes: 71 additions & 0 deletions test/data/valid_openapi/issue#817-enum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
openapi: 3.0.3
info:
version: 4.1.0
title: 817-Enum
paths:
/crm/contacts:
post:
operationId: contactsCreate
summary: Post contacts
description: Post contacts
parameters:
- $ref: '#/components/parameters/contactsSort'
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Website'
responses:
'200': {}
components:
parameters:
contactsSort:
name: sort
in: query
description: Apply sorting
style: deepObject
explode: true
schema:
$ref: '#/components/schemas/ContactsSort'
schemas:
ContactsSort:
type: object
example:
by: created_at
properties:
by:
type: string
description: The field on which to sort the Contacts
enum:
- created_at
- updated_at
- name
- first_name
- last_name
- email
example: created_at
required:
- by
Website:
type: object
required:
- url
properties:
id:
type: string
example: '12345'
nullable: true
url:
type: string
example: 'http://example.com'
minLength: 1
category:
type: string
enum:
- primary
- secondary
- work
- personal
- other
example: work
74 changes: 72 additions & 2 deletions test/unit/convertV2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@
path.join(__dirname, VALID_OPENAPI_PATH, '/readOnlyOneOf.json'),
readOnlyNestedSpec =
path.join(__dirname, VALID_OPENAPI_PATH, '/readOnlyNested.json'),
issue817 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#817-enum.yaml'),
issue795 = path.join(__dirname, VALID_OPENAPI_PATH, '/form-binary-file.json');


describe('The convert v2 Function', function() {

it('Should explicitly set auth when specified on a request ' +
Expand Down Expand Up @@ -321,7 +321,7 @@
});

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

Check warning on line 324 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 324 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 324 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 @@ -333,7 +333,7 @@
done();
});
});
it.skip('Should collapse child and parent folder when parent has only one child' +

Check warning on line 336 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 336 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 336 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 @@ -347,7 +347,7 @@
done();
});
});
it.skip('Should generate collection without creating folders and having only one request' +

Check warning on line 350 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 350 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 350 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 @@ -2097,7 +2097,8 @@
};

Converter.convertV2(input, {
optimizeConversion: false
optimizeConversion: false,
parametersResolution: 'Example'
}, (err, result) => {
const expectedResponseBody = JSON.parse(result.output[0].data.item[0].item[0].response[0].body);
expect(err).to.be.null;
Expand Down Expand Up @@ -2950,4 +2951,73 @@
done();
});
});

it('[Github #817] Should convert using Example parameter resolution', function (done) {
var openapi = fs.readFileSync(issue817, 'utf8'),
reqQuery,
reqBody;
Converter.convertV2({ type: 'string', data: openapi }, {
requestNameSource: 'Fallback',
indentCharacter: 'Space',
collapseFolders: true,
optimizeConversion: true,
parametersResolution: 'Example'
}, (err, conversionResult) => {

reqQuery = conversionResult.output[0].data.item[0].item[0].item[0].request.url.query[0];
reqBody = JSON.parse(conversionResult.output[0].data.item[0].item[0].item[0].request.body.raw);

expect(err).to.be.null;
expect(conversionResult.result).to.equal(true);
expect(reqQuery.value).to.equal('created_at');
expect(reqBody.category).to.equal('work');
done();
});
});

it('[Github #817] Should convert using Schema parameter resolution', function (done) {
var openapi = fs.readFileSync(issue817, 'utf8'),
reqQuery,
reqBody;
Converter.convertV2({ type: 'string', data: openapi }, {
requestNameSource: 'Fallback',
indentCharacter: 'Space',
collapseFolders: true,
optimizeConversion: true,
parametersResolution: 'Schema'
}, (err, conversionResult) => {

reqQuery = conversionResult.output[0].data.item[0].item[0].item[0].request.url.query[0];
reqBody = JSON.parse(conversionResult.output[0].data.item[0].item[0].item[0].request.body.raw);

expect(err).to.be.null;
expect(conversionResult.result).to.equal(true);
expect(reqQuery.value).to.equal('<string>');
expect(reqBody.category).to.equal('<string>');
done();
});
});

it('[Github #817] Should fallback to default Schema parameter resolution', function (done) {
var openapi = fs.readFileSync(issue817, 'utf8'),
reqQuery,
reqBody;
Converter.convertV2({ type: 'string', data: openapi }, {
requestNameSource: 'Fallback',
indentCharacter: 'Space',
collapseFolders: true,
optimizeConversion: true
}, (err, conversionResult) => {

reqQuery = conversionResult.output[0].data.item[0].item[0].item[0].request.url.query[0];
reqBody = JSON.parse(conversionResult.output[0].data.item[0].item[0].item[0].request.body.raw);

expect(err).to.be.null;
expect(conversionResult.result).to.equal(true);
expect(reqQuery.value).to.equal('<string>');
expect(reqBody.category).to.equal('<string>');
done();
});
});

});
158 changes: 158 additions & 0 deletions test/unit/schemaUtilsV2.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {
resolveSchema,
resolveRequestBodyForPostmanRequest
} = require('../../libV2/schemaUtils.js'),
concreteUtils = require('../../lib/30XUtils/schemaUtils30X'),
Expand Down Expand Up @@ -182,3 +183,160 @@ describe('resolveRequestBodyForPostmanRequest function', function () {
});

});

describe('resolveSchema function', function () {

it('should return schema value "string" for simple property', function () {
const contextTest = {
concreteUtils,
schemaCache: {},
schemaFakerCache: {},
computedOptions: {
parametersResolution: 'schema',
stackLimit: 10,
includeDeprecated: false
}
},
schema = {
'type': 'string',
'example': 'abc'
},
result = resolveSchema(contextTest, schema, 0, 'conversion');

expect(result.default).to.equal('<string>');
});

it('should return example value for simple property', function () {
const contextTest = {
concreteUtils,
schemaCache: {},
schemaFakerCache: {},
computedOptions: {
parametersResolution: 'example',
stackLimit: 10,
includeDeprecated: false
}
},
schema = {
'type': 'string',
'example': 'abc'
},
result = resolveSchema(contextTest, schema, 0, 'conversion');

expect(result.example).to.equal('abc');
});

it('should return schema value "string" for nested property', function () {
const contextTest = {
concreteUtils,
schemaCache: {},
schemaFakerCache: {},
computedOptions: {
parametersResolution: 'schema',
stackLimit: 10,
includeDeprecated: false
}
},
schema = {
'type': 'object',
'properties': {
'foo': {
'type': 'string',
'example': 'abc'
}
}
},
result = resolveSchema(contextTest, schema, 0, 'conversion');

expect(result.properties.foo.default).to.equal('<string>');
});

it('should return example value for nested property', function () {
const contextTest = {
concreteUtils,
schemaCache: {},
schemaFakerCache: {},
computedOptions: {
parametersResolution: 'example',
stackLimit: 10,
includeDeprecated: false
}
},
schema = {
'type': 'object',
'properties': {
'foo': {
'type': 'string',
'example': 'abc'
}
}
},
result = resolveSchema(contextTest, schema, 0, 'conversion');

expect(result.properties.foo.example).to.equal('abc');
});

it('should return schema value "string" for ENUM property', function () {
const contextTest = {
concreteUtils,
schemaCache: {},
schemaFakerCache: {},
computedOptions: {
parametersResolution: 'schema',
stackLimit: 10,
includeDeprecated: false
}
},
schema = {
'type': 'object',
'properties': {
'foo': {
'type': 'string',
'enum': [
'primary',
'secondary',
'work',
'personal',
'other'
],
'example': 'primary'
}
}
},
result = resolveSchema(contextTest, schema, 0, 'conversion');

expect(result.properties.foo.default).to.equal('<string>');
});

it('should return example value for ENUM property', function () {
const contextTest = {
concreteUtils,
schemaCache: {},
schemaFakerCache: {},
computedOptions: {
parametersResolution: 'example',
stackLimit: 10,
includeDeprecated: false
}
},
schema = {
'type': 'object',
'properties': {
'foo': {
'type': 'string',
'enum': [
'primary',
'secondary',
'work',
'personal',
'other'
],
'example': 'work'
}
}
},
result = resolveSchema(contextTest, schema, 0, 'conversion');

expect(result.properties.foo.example).to.equal('work');
});
});
Loading