From 968b8a2eebae028f2d221f5b2afe9917d29576ff Mon Sep 17 00:00:00 2001 From: Thim Date: Fri, 26 Jul 2024 13:08:43 +0200 Subject: [PATCH 1/6] parametersResolution: Added tests --- test/data/valid_openapi/issue#817-enum.yaml | 43 ++++++++++++++ test/unit/convertV2.test.js | 63 ++++++++++++++++++++- 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 test/data/valid_openapi/issue#817-enum.yaml 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..9f14074b7 --- /dev/null +++ b/test/data/valid_openapi/issue#817-enum.yaml @@ -0,0 +1,43 @@ +openapi: 3.0.3 +info: + version: 4.1.0 + title: 817-Enum +paths: + /crm/contacts: + get: + operationId: contactsAll + summary: List contacts + description: List contacts + parameters: + - $ref: '#/components/parameters/contactsSort' + 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 diff --git a/test/unit/convertV2.test.js b/test/unit/convertV2.test.js index 1fe5e0062..e77b97655 100644 --- a/test/unit/convertV2.test.js +++ b/test/unit/convertV2.test.js @@ -111,7 +111,8 @@ const expect = require('chai').expect, path.join(__dirname, VALID_OPENAPI_PATH, '/multiExampleResponseCodeMatching.json'), duplicateCollectionVars = path.join(__dirname, VALID_OPENAPI_PATH, '/duplicateCollectionVars.json'), - issue795 = path.join(__dirname, VALID_OPENAPI_PATH, '/form-binary-file.json'); + issue795 = path.join(__dirname, VALID_OPENAPI_PATH, '/form-binary-file.json'), + issue817 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#817-enum.yaml'); describe('The convert v2 Function', function() { @@ -2841,4 +2842,64 @@ 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; + 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]; + + expect(err).to.be.null; + expect(conversionResult.result).to.equal(true); + expect(reqQuery.value).to.equal('created_at'); + done(); + }); + }); + + it('[Github #817] Should convert using Schema parameter resolution', function (done) { + var openapi = fs.readFileSync(issue817, 'utf8'), + reqQuery; + 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]; + + expect(err).to.be.null; + expect(conversionResult.result).to.equal(true); + expect(reqQuery.value).to.equal(''); + done(); + }); + }); + + it('[Github #817] Should fallback to default Schema parameter resolution', function (done) { + var openapi = fs.readFileSync(issue817, 'utf8'), + reqQuery; + 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]; + + expect(err).to.be.null; + expect(conversionResult.result).to.equal(true); + expect(reqQuery.value).to.equal(''); + done(); + }); + }); + }); From 2d89a7d3d09ae700d027a97a0d5614ec9b92a365 Mon Sep 17 00:00:00 2001 From: Thim Date: Sat, 27 Jul 2024 13:51:12 +0200 Subject: [PATCH 2/6] Added tests for resolveSchema --- test/unit/schemaUtilsV2.test.js | 80 +++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/test/unit/schemaUtilsV2.test.js b/test/unit/schemaUtilsV2.test.js index 547925532..79a8ead63 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,82 @@ describe('resolveRequestBodyForPostmanRequest function', function () { }); }); + +describe('resolveSchema function', function () { + + it('should return 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 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 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(''); + }); +}); From eb578526e0a239eb11016562d31c0bc7582d1d49 Mon Sep 17 00:00:00 2001 From: Thim Date: Sat, 27 Jul 2024 13:51:43 +0200 Subject: [PATCH 3/6] parametersResolution: Handle ENUM properties --- libV2/schemaUtils.js | 10 +++- test/data/valid_openapi/issue#817-enum.yaml | 62 +++++++++++++++------ test/unit/convertV2.test.js | 18 ++++-- 3 files changed, 68 insertions(+), 22 deletions(-) diff --git a/libV2/schemaUtils.js b/libV2/schemaUtils.js index ddc635917..a44fc795b 100644 --- a/libV2/schemaUtils.js +++ b/libV2/schemaUtils.js @@ -606,7 +606,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 + '>'; @@ -644,6 +644,13 @@ let QUERYPARAM = 'query', ); } }); + + let { parametersResolution } = context.computedOptions; + if (resolveFor === CONVERSION && parametersResolution === 'schema') { + if (schema.hasOwnProperty('type')) { + schema.default = '<' + schema.type + '>'; + } + } } return schema; @@ -2264,6 +2271,7 @@ module.exports = { resolveResponseForPostmanRequest, resolveRequestBodyForPostmanRequest, + resolveBodyData, resolveRefFromSchema, resolveSchema }; diff --git a/test/data/valid_openapi/issue#817-enum.yaml b/test/data/valid_openapi/issue#817-enum.yaml index 9f14074b7..052d49be3 100644 --- a/test/data/valid_openapi/issue#817-enum.yaml +++ b/test/data/valid_openapi/issue#817-enum.yaml @@ -4,12 +4,18 @@ info: title: 817-Enum paths: /crm/contacts: - get: - operationId: contactsAll - summary: List contacts - description: List 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: @@ -24,20 +30,42 @@ components: $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 - example: - by: created_at + required: + - url properties: - by: + id: + type: string + example: '12345' + nullable: true + url: + type: string + example: 'http://example.com' + minLength: 1 + category: 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 + - primary + - secondary + - work + - personal + - other + example: work diff --git a/test/unit/convertV2.test.js b/test/unit/convertV2.test.js index e77b97655..f65f1e422 100644 --- a/test/unit/convertV2.test.js +++ b/test/unit/convertV2.test.js @@ -2088,7 +2088,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; @@ -2845,7 +2846,8 @@ describe('The convert v2 Function', function() { it('[Github #817] Should convert using Example parameter resolution', function (done) { var openapi = fs.readFileSync(issue817, 'utf8'), - reqQuery; + reqQuery, + reqBody; Converter.convertV2({ type: 'string', data: openapi }, { requestNameSource: 'Fallback', indentCharacter: 'Space', @@ -2855,17 +2857,20 @@ describe('The convert v2 Function', function() { }, (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; + reqQuery, + reqBody; Converter.convertV2({ type: 'string', data: openapi }, { requestNameSource: 'Fallback', indentCharacter: 'Space', @@ -2875,17 +2880,20 @@ describe('The convert v2 Function', function() { }, (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; + reqQuery, + reqBody; Converter.convertV2({ type: 'string', data: openapi }, { requestNameSource: 'Fallback', indentCharacter: 'Space', @@ -2894,10 +2902,12 @@ describe('The convert v2 Function', function() { }, (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(); }); }); From afc039c4d5123be6b42b0afb4711c954b558b76c Mon Sep 17 00:00:00 2001 From: Thim Date: Sat, 27 Jul 2024 13:55:14 +0200 Subject: [PATCH 4/6] parametersResolution: Handle ENUM properties --- libV2/schemaUtils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/libV2/schemaUtils.js b/libV2/schemaUtils.js index a44fc795b..4ced24f4e 100644 --- a/libV2/schemaUtils.js +++ b/libV2/schemaUtils.js @@ -2271,7 +2271,6 @@ module.exports = { resolveResponseForPostmanRequest, resolveRequestBodyForPostmanRequest, - resolveBodyData, resolveRefFromSchema, resolveSchema }; From 1cfe4fc3114bbc32c3c6a5c2a23dfe86cb6c4c42 Mon Sep 17 00:00:00 2001 From: Thim Date: Sat, 27 Jul 2024 14:00:50 +0200 Subject: [PATCH 5/6] Added tests for resolveSchema --- test/unit/schemaUtilsV2.test.js | 84 +++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/test/unit/schemaUtilsV2.test.js b/test/unit/schemaUtilsV2.test.js index 79a8ead63..20e8c3dcf 100644 --- a/test/unit/schemaUtilsV2.test.js +++ b/test/unit/schemaUtilsV2.test.js @@ -186,7 +186,7 @@ describe('resolveRequestBodyForPostmanRequest function', function () { describe('resolveSchema function', function () { - it('should return string for simple property', function () { + it('should return schema value "string" for simple property', function () { const contextTest = { concreteUtils, schemaCache: {}, @@ -205,7 +205,28 @@ describe('resolveSchema function', function () { expect(result.default).to.equal(''); }); - it('should return string for nested property', function () { + + 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: {}, @@ -230,7 +251,32 @@ describe('resolveSchema function', function () { expect(result.properties.foo.default).to.equal(''); }); - it('should return string for ENUM property', function () { + 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: {}, @@ -261,4 +307,36 @@ describe('resolveSchema function', function () { 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'); + }); }); From 385c3209f16a44ab28e73391eb0fac69f8e2ac74 Mon Sep 17 00:00:00 2001 From: Thim Date: Wed, 11 Sep 2024 09:31:36 +0200 Subject: [PATCH 6/6] Merge branch 'develop' into 817-enum --- test/unit/convertV2.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/convertV2.test.js b/test/unit/convertV2.test.js index 6427ff3cc..2c5f53bd7 100644 --- a/test/unit/convertV2.test.js +++ b/test/unit/convertV2.test.js @@ -111,8 +111,6 @@ const expect = require('chai').expect, path.join(__dirname, VALID_OPENAPI_PATH, '/multiExampleResponseCodeMatching.json'), duplicateCollectionVars = path.join(__dirname, VALID_OPENAPI_PATH, '/duplicateCollectionVars.json'), - issue795 = path.join(__dirname, VALID_OPENAPI_PATH, '/form-binary-file.json'), - issue817 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#817-enum.yaml'); readOnlySpec = path.join(__dirname, VALID_OPENAPI_PATH, '/readOnly.json'), readOnlyRefSpec = @@ -123,6 +121,7 @@ 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() {