diff --git a/libV2/schemaUtils.js b/libV2/schemaUtils.js index 77ba2f546..b5edb3f44 100644 --- a/libV2/schemaUtils.js +++ b/libV2/schemaUtils.js @@ -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 + '>'; @@ -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. diff --git a/test/data/valid_openapi/issue#817-enum.yaml b/test/data/valid_openapi/issue#817-enum.yaml new file mode 100644 index 000000000..052d49be3 --- /dev/null +++ b/test/data/valid_openapi/issue#817-enum.yaml @@ -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 diff --git a/test/unit/convertV2.test.js b/test/unit/convertV2.test.js index 985e3499d..2c5f53bd7 100644 --- a/test/unit/convertV2.test.js +++ b/test/unit/convertV2.test.js @@ -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 ' + @@ -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; @@ -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(''); + expect(reqBody.category).to.equal(''); + 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(''); + expect(reqBody.category).to.equal(''); + done(); + }); + }); + }); diff --git a/test/unit/schemaUtilsV2.test.js b/test/unit/schemaUtilsV2.test.js index 547925532..20e8c3dcf 100644 --- a/test/unit/schemaUtilsV2.test.js +++ b/test/unit/schemaUtilsV2.test.js @@ -1,4 +1,5 @@ const { + resolveSchema, resolveRequestBodyForPostmanRequest } = require('../../libV2/schemaUtils.js'), concreteUtils = require('../../lib/30XUtils/schemaUtils30X'), @@ -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(''); + }); + + 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(''); + }); + + 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(''); + }); + + 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'); + }); +});