diff --git a/libV2/schemaUtils.js b/libV2/schemaUtils.js index 1aae098a..45b6ec1d 100644 --- a/libV2/schemaUtils.js +++ b/libV2/schemaUtils.js @@ -1682,14 +1682,12 @@ let QUERYPARAM = 'query', resolvedBodyResult = resolveBodyData(context, requestBodyContent.schema); resolvedBody = + resolvedBodyResult && + Array.isArray(resolvedBodyResult.generatedBody) && + resolvedBodyResult.generatedBody[0]; - resolvedBodyResult && Array.isArray(resolvedBodyResult.generatedBody) && - resolvedBodyResult.generatedBody[0] ? - resolvedBodyResult.generatedBody[0] : - undefined; + resolvedSchemaTypeObject = resolvedBodyResult && resolvedBodyResult.resolvedSchemaType; - resolvedSchemaTypeObject = resolvedBodyResult && - resolvedBodyResult.resolvedSchemaType ? resolvedBodyResult.resolvedSchemaType : undefined; resolvedBody && (bodyData = resolvedBody.request); const encoding = requestBodyContent.encoding || {}; @@ -1747,15 +1745,11 @@ let QUERYPARAM = 'query', resolvedBodyResult = resolveBodyData(context, requestBodyContent.schema); resolvedBody = - resolvedBodyResult && Array.isArray(resolvedBodyResult.generatedBody) && - resolvedBodyResult.generatedBody[0] ? - resolvedBodyResult.generatedBody[0] : - undefined; + resolvedBodyResult && + Array.isArray(resolvedBodyResult.generatedBody) && + resolvedBodyResult.generatedBody[0]; - resolvedSchemaTypeObject = - resolvedBodyResult && resolvedBodyResult.resolvedSchemaType ? - result.resolvedSchemaType : - undefined; + resolvedSchemaTypeObject = resolvedBodyResult && resolvedBodyResult.resolvedSchemaType; resolvedBody && (bodyData = resolvedBody.request); @@ -1875,15 +1869,11 @@ let QUERYPARAM = 'query', else { resolvedBodyResult = resolveBodyData(context, requestContent[bodyType], bodyType); resolvedBody = - resolvedBodyResult && Array.isArray(resolvedBodyResult.generatedBody) && - resolvedBodyResult.generatedBody[0] ? - resolvedBodyResult.generatedBody[0] : - undefined; + resolvedBodyResult && + Array.isArray(resolvedBodyResult.generatedBody) && + resolvedBodyResult.generatedBody[0]; - resolvedSchemaTypeObject = - resolvedBodyResult && resolvedBodyResult.resolvedSchemaType ? - resolvedBodyResult.resolvedSchemaType : - undefined; + resolvedSchemaTypeObject = resolvedBodyResult && resolvedBodyResult.resolvedSchemaType; resolvedBody && (bodyData = resolvedBody.request); @@ -2074,18 +2064,14 @@ let QUERYPARAM = 'query', let queryParamTypeInfo = {}, properties = {}, - keyName, paramValue = resolveValueOfParameter(context, param); if (param && param.name && param.schema && param.schema.type) { - keyName = param.name; properties = createProperties(param); + queryParamTypeInfo = { keyName: param.name, properties }; + queryParamTypes.push(queryParamTypeInfo); } - queryParamTypeInfo = { keyName, properties }; - - queryParamTypes.push(queryParamTypeInfo); - if (typeof paramValue === 'number' || typeof paramValue === 'boolean') { // the SDK will keep the number-ness, // which will be rejected by the collection v2 schema @@ -2105,8 +2091,8 @@ let QUERYPARAM = 'query', resolvePathParamsForPostmanRequest = (context, operationItem, method) => { const params = resolvePathItemParams(context, operationItem[method].parameters, operationItem.parameters), - pmParams = []; - let pathParamTypes = []; + pmParams = [], + pathParamTypes = []; _.forEach(params, (param) => { if (!_.isObject(param)) { @@ -2127,17 +2113,14 @@ let QUERYPARAM = 'query', let pathParamTypeInfo = {}, properties = {}, - keyName, paramValue = resolveValueOfParameter(context, param); if (param && param.name && param.schema && param.schema.type) { - keyName = param.name; properties = createProperties(param); + pathParamTypeInfo = { keyName: param.name, properties }; + pathParamTypes.push(pathParamTypeInfo); } - pathParamTypeInfo = { keyName, properties }; - pathParamTypes.push(pathParamTypeInfo); - if (typeof paramValue === 'number' || typeof paramValue === 'boolean') { // the SDK will keep the number-ness, // which will be rejected by the collection v2 schema @@ -2211,18 +2194,14 @@ let QUERYPARAM = 'query', let headerTypeInfo = {}, properties = {}, - keyName, paramValue = resolveValueOfParameter(context, param); if (param && param.name && param.schema && param.schema.type) { - keyName = param.name; properties = createProperties(param); + headerTypeInfo = { keyName: param.name, properties }; + headerTypes.push(headerTypeInfo); } - headerTypeInfo = { keyName, properties }; - - headerTypes.push(headerTypeInfo); - if (typeof paramValue === 'number' || typeof paramValue === 'boolean') { // the SDK will keep the number-ness, // which will be rejected by the collection v2 schema @@ -2328,8 +2307,8 @@ let QUERYPARAM = 'query', resolveResponseHeaders = (context, responseHeaders) => { const headers = [], - { includeDeprecated } = context.computedOptions; - let headerTypes = []; + { includeDeprecated } = context.computedOptions, + headerTypes = []; if (_.has(responseHeaders, '$ref')) { responseHeaders = resolveSchema(context, responseHeaders, { isResponseSchema: true }); @@ -2346,8 +2325,7 @@ let QUERYPARAM = 'query', let headerValue = resolveValueOfParameter(context, value, { isResponseSchema: true }), headerTypeInfo = {}, - properties = {}, - keyName; + properties = {}; if (typeof headerValue === 'number' || typeof headerValue === 'boolean') { // the SDK will keep the number-ness, @@ -2363,8 +2341,7 @@ let QUERYPARAM = 'query', headers.push(...serialisedHeader); if (headerData && headerData.name && headerData.schema && headerData.schema.type) { - const { name, schema } = headerData; - keyName = name; + const { schema } = headerData; properties = { type: schema.type, format: schema.format, @@ -2379,10 +2356,9 @@ let QUERYPARAM = 'query', pattern: schema.pattern, example: schema.example }; - + headerTypeInfo = { keyName: headerData.name, properties }; + headerTypes.push(headerTypeInfo); } - headerTypeInfo = { keyName, properties }; - headerTypes.push(headerTypeInfo); }); return { resolvedHeaderTypes: headerTypes, headers }; @@ -2521,20 +2497,30 @@ let QUERYPARAM = 'query', } _.forOwn(operationItem.responses, (responseObj, code) => { - let responseSchema = _.has(responseObj, '$ref') ? ( - resolveSchema(context, responseObj, { isResponseSchema: true })) : responseObj, + let responseSchema = _.has(responseObj, '$ref') ? + resolveSchema(context, responseObj, { isResponseSchema: true }) : responseObj, { includeAuthInfoInExample } = context.computedOptions, auth = request.auth, resolvedExamples = resolveResponseBody(context, responseSchema, requestBodyExamples, code) || {}, { resolvedHeaderTypes, headers } = resolveResponseHeaders(context, responseSchema.headers), responseBodyHeaderObj; + + /* since resolvedExamples is a list of objects, we are picking the head element everytime + as the types are generated per example and even if we have response having same status code, + we resolve them all together */ + resolvedExamplesObject = resolvedExamples[0] && resolvedExamples[0].resolvedResponseBodyTypes; + responseBodyHeaderObj = { body: JSON.stringify(resolvedExamplesObject, null, 2), headers: JSON.stringify(resolvedHeaderTypes, null, 2) }; + // replace 'X' char in code with '0' | E.g. 5xx -> 500 + code = code.replace(/X|x/g, '0'); + code = code === 'default' ? 500 : _.toSafeInteger(code); + Object.assign(responseTypes, { [code]: responseBodyHeaderObj }); _.forOwn(resolvedExamples, (resolvedExample = {}) => { diff --git a/libV2/utils.js b/libV2/utils.js index 9d4df9a8..33375b77 100644 --- a/libV2/utils.js +++ b/libV2/utils.js @@ -28,10 +28,6 @@ const _ = require('lodash'), originalRequest.header = _.get(response, 'originalRequest.headers', []); originalRequest.body = requestItem.request.body; - // replace 'X' char with '0' - response.code = response.code.replace(/X|x/g, '0'); - response.code = response.code === 'default' ? 500 : _.toSafeInteger(response.code); - let sdkResponse = new Response({ name: response.name, code: response.code, diff --git a/test/unit/convertV2WithTypes.test.js b/test/unit/convertV2WithTypes.test.js index f4f629b3..a95c946a 100644 --- a/test/unit/convertV2WithTypes.test.js +++ b/test/unit/convertV2WithTypes.test.js @@ -2,7 +2,9 @@ // Disabling max Length for better visibility of the expectedExtractedTypes /* eslint-disable one-var */ -// Disabling as we want the checks to run in order of their declaration +/* Disabling as we want the checks to run in order of their declaration as declaring everything as once + even though initial declarations fails with test won't do any good */ + const expect = require('chai').expect, Converter = require('../../index.js'), @@ -49,7 +51,7 @@ const expect = require('chai').expect, }; -describe('convertV2WithTypes should generate collection confirming to collection schema', function() { +describe('convertV2WithTypes should generate collection conforming to collection schema', function() { it('Should generate collection conforming to schema for and fail if not valid ' + testSpec, function(done) { @@ -135,7 +137,7 @@ describe('convertV2WithTypes', function() { ); }); - it('should validate the schema' + testSpec1, function() { + it('should validate the generated type object' + testSpec1, function() { const example = { code: 200, message: 'Success' @@ -189,7 +191,6 @@ describe('convertV2WithTypes', function() { const element = Object.values(conversionResult.extractedTypes)[0]; const { response } = element; - // Get the schema from the response const [key, value] = Object.entries(response)[0]; expect(value).to.have.property('body').that.is.a('string'); @@ -216,7 +217,7 @@ describe('convertV2WithTypes', function() { 'body': '{\n "type": "array",\n "items": {\n "type": "object",\n "properties": {\n "id": {\n "type": "integer",\n "format": "int64"\n },\n "name": {\n "type": "string"\n },\n "tag": {\n "type": "string"\n }\n },\n "required": [\n "id",\n "name"\n ]\n }\n}', 'headers': '[\n {\n "keyName": "x-next",\n "properties": {\n "type": "string",\n "default": "",\n "required": false,\n "deprecated": false\n }\n }\n]' }, - 'default': { + '500': { 'body': '{\n "type": "object",\n "properties": {\n "code": {\n "type": "integer"\n },\n "message": {\n "type": "string"\n }\n },\n "required": [\n "code",\n "message"\n ]\n}', 'headers': '[]' } @@ -232,9 +233,9 @@ describe('convertV2WithTypes', function() { '201': { 'headers': '[]' }, - 'default': { + '500': { 'body': '{\n "type": "object",\n "properties": {\n "code": {\n "type": "integer"\n },\n "message": {\n "type": "string"\n }\n },\n "required": [\n "code",\n "message"\n ]\n}', - 'headers': '[\n {\n "properties": {}\n }\n]' + 'headers': '[]' } } }, @@ -249,7 +250,7 @@ describe('convertV2WithTypes', function() { 'body': '{\n "type": "array",\n "items": {\n "type": "object",\n "properties": {\n "id": {\n "type": "integer",\n "format": "int64"\n },\n "name": {\n "type": "string"\n },\n "tag": {\n "type": "string"\n }\n },\n "required": [\n "id",\n "name"\n ]\n }\n}', 'headers': '[]' }, - 'default': { + '500': { 'body': '{\n "type": "object",\n "properties": {\n "code": {\n "type": "integer"\n },\n "message": {\n "type": "string"\n }\n },\n "required": [\n "code",\n "message"\n ]\n}', 'headers': '[]' } @@ -266,7 +267,7 @@ describe('convertV2WithTypes', function() { 'body': '{\n "type": "array",\n "items": {\n "type": "object",\n "properties": {\n "id": {\n "type": "integer",\n "format": "int64"\n },\n "name": {\n "type": "string"\n },\n "tag": {\n "type": "string"\n }\n },\n "required": [\n "id",\n "name"\n ]\n }\n}', 'headers': '[]' }, - 'default': { + '500': { 'body': '{\n "type": "object",\n "properties": {\n "code": {\n "type": "integer"\n },\n "message": {\n "type": "string"\n }\n },\n "required": [\n "code",\n "message"\n ]\n}', 'headers': '[]' }