Skip to content

Commit

Permalink
AB-289-refactored-code
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushShri committed Jan 8, 2025
1 parent 6f30294 commit dc4a48a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 65 deletions.
90 changes: 38 additions & 52 deletions libV2/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 });
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 };
Expand Down Expand Up @@ -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 = {}) => {
Expand Down
4 changes: 0 additions & 4 deletions libV2/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 10 additions & 9 deletions test/unit/convertV2WithTypes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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');

Expand All @@ -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": "<string>",\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': '[]'
}
Expand All @@ -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': '[]'
}
}
},
Expand All @@ -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': '[]'
}
Expand All @@ -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': '[]'
}
Expand Down

0 comments on commit dc4a48a

Please sign in to comment.