Skip to content
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 @@ const expect = require('chai').expect,
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 @@ -2097,7 +2097,8 @@ describe('The convert v2 Function', function() {
};

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 @@ describe('The convert v2 Function', function() {
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');
});
});