Skip to content

Commit a8de5ff

Browse files
committed
AB-289-refactored-code
1 parent c4a9591 commit a8de5ff

File tree

2 files changed

+31
-40
lines changed

2 files changed

+31
-40
lines changed

libV2/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ module.exports = {
9393
let request = {},
9494
collectionVariables = [],
9595
requestObject = {},
96-
typesObject = {};
96+
requestTypesObject = {};
9797

9898
try {
99-
({ request, collectionVariables, typesObject } = resolvePostmanRequest(context,
99+
({ request, collectionVariables, requestTypesObject } = resolvePostmanRequest(context,
100100
context.openapi.paths[node.meta.path],
101101
node.meta.path,
102102
node.meta.method
103103
));
104104

105105
requestObject = generateRequestItemObject(request);
106-
extractedTypesObject = Object.assign({}, extractedTypesObject, typesObject);
106+
extractedTypesObject = Object.assign({}, extractedTypesObject, requestTypesObject);
107107

108108
}
109109
catch (error) {

libV2/schemaUtils.js

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ let QUERYPARAM = 'query',
743743
* @param {Set<string>} [parentRequired=new Set()] - A set of parent-required property keys to inherit.
744744
* @returns {Object} The processed schema details.
745745
*/
746-
processSchema = (resolvedSchema, parentRequired = new Set()) => {
746+
processSchema = (resolvedSchema) => {
747747
if (resolvedSchema.type === 'object' && resolvedSchema.properties) {
748748
const schemaDetails = {
749749
type: resolvedSchema.type || 'unknown',
@@ -767,7 +767,7 @@ let QUERYPARAM = 'query',
767767
format: prop.format || undefined
768768
};
769769

770-
if (requiredProperties.has(key) || parentRequired.has(key)) {
770+
if (requiredProperties.has(key)) {
771771
schemaDetails.required.push(key);
772772
}
773773
if (prop.properties) {
@@ -778,7 +778,7 @@ let QUERYPARAM = 'query',
778778
}
779779
}
780780
else if (prop.type === 'array' && prop.items) {
781-
propertyDetails.items = processSchema(prop.items, parentRequired = requiredProperties);
781+
propertyDetails.items = processSchema(prop.items);
782782
}
783783

784784
schemaDetails.properties[key] = propertyDetails;
@@ -2119,6 +2119,7 @@ let QUERYPARAM = 'query',
21192119
keyName = name;
21202120
properties = createProperties(schema, param);
21212121
}
2122+
21222123
pathParamTypeInfo = { keyName, properties };
21232124
if (keyName && param.schema && param.schema.type) {
21242125
pathParamTypes.push(pathParamTypeInfo);
@@ -2136,6 +2137,7 @@ let QUERYPARAM = 'query',
21362137

21372138
pmParams.push(...deserialisedParams);
21382139
});
2140+
21392141
return { pathParamTypes, pathParams: pmParams };
21402142
},
21412143

@@ -2199,16 +2201,14 @@ let QUERYPARAM = 'query',
21992201
keyName,
22002202
paramValue = resolveValueOfParameter(context, param);
22012203

2202-
if (param && param.schema) {
2204+
if (param && param.name && param.schema && param.schema.type) {
22032205
const { name, schema } = param;
22042206
keyName = name;
22052207
properties = createProperties(schema, param);
22062208
}
22072209
headerTypeInfo = { keyName, properties };
22082210

2209-
if (keyName && param.schema && param.schema.type) {
2210-
headerTypes.push(headerTypeInfo);
2211-
}
2211+
headerTypes.push(headerTypeInfo);
22122212

22132213
if (typeof paramValue === 'number' || typeof paramValue === 'boolean') {
22142214
// the SDK will keep the number-ness,
@@ -2222,6 +2222,7 @@ let QUERYPARAM = 'query',
22222222

22232223
pmParams.push(...deserialisedParams);
22242224
});
2225+
22252226
return { headerTypes, headers: pmParams };
22262227
},
22272228

@@ -2267,9 +2268,7 @@ let QUERYPARAM = 'query',
22672268
resolvedResponseBodyResult = resolveBodyData(
22682269
context, responseContent[bodyType], bodyType, true, code, requestBodyExamples);
22692270
allBodyData = resolvedResponseBodyResult.generatedBody;
2270-
resolvedResponseBodyTypes = resolvedResponseBodyResult ?
2271-
resolvedResponseBodyResult.resolvedSchemaType :
2272-
undefined;
2271+
resolvedResponseBodyTypes = resolvedResponseBodyResult.resolvedSchemaType;
22732272

22742273
return _.map(allBodyData, (bodyData) => {
22752274
let requestBodyData = bodyData.request,
@@ -2350,11 +2349,11 @@ let QUERYPARAM = 'query',
23502349

23512350
headers.push(...serialisedHeader);
23522351

2353-
if (headerData && headerData.schema) {
2352+
if (headerData && headerData.name && headerData.schema && headerData.schema.type) {
23542353
const { name, schema } = headerData;
23552354
keyName = name;
23562355
properties = {
2357-
type: schema.type || 'unknown',
2356+
type: schema.type,
23582357
format: schema.format || undefined,
23592358
default: schema.default !== undefined ? schema.default : undefined,
23602359
required: schema.required || false,
@@ -2370,9 +2369,7 @@ let QUERYPARAM = 'query',
23702369

23712370
}
23722371
headerTypeInfo = { keyName, properties };
2373-
if (keyName && headerData.schema && headerData.schema.type) {
2374-
headerTypes.push(headerTypeInfo);
2375-
}
2372+
headerTypes.push(headerTypeInfo);
23762373
});
23772374

23782375
return { resolvedHeaderTypes: headerTypes, headers };
@@ -2468,7 +2465,7 @@ let QUERYPARAM = 'query',
24682465
headerFamily,
24692466
isBodyTypeXML,
24702467
resolvedExamplesObject = {},
2471-
responseBlock = {};
2468+
responseTypes = {};
24722469

24732470
// store all request examples which will be used for creation of examples with correct request and response matching
24742471
if (typeof requestBody === 'object') {
@@ -2515,20 +2512,18 @@ let QUERYPARAM = 'query',
25152512
resolveSchema(context, responseObj, { isResponseSchema: true })) : responseObj,
25162513
{ includeAuthInfoInExample } = context.computedOptions,
25172514
auth = request.auth,
2518-
resolvedExamples = resolveResponseBody(context, responseSchema, requestBodyExamples, code) || {};
2515+
resolvedExamples = resolveResponseBody(context, responseSchema, requestBodyExamples, code) || {},
2516+
{ resolvedHeaderTypes, headers } = resolveResponseHeaders(context, responseSchema.headers),
2517+
responseBodyHeaderObj;
25192518
resolvedExamplesObject = resolvedExamples[0] && resolvedExamples[0].resolvedResponseBodyTypes;
25202519
// eslint-disable-next-line one-var
2521-
let { resolvedHeaderTypes, headers } = resolveResponseHeaders(context, responseSchema.headers),
2522-
responseBodyHeaderObj,
2523-
responseTypes;
2524-
responseBodyHeaderObj = resolvedExamplesObject ?
2520+
responseBodyHeaderObj =
25252521
{
25262522
body: JSON.stringify(resolvedExamplesObject, null, 2),
25272523
headers: JSON.stringify(resolvedHeaderTypes, null, 2)
2528-
} : {};
2524+
};
25292525

2530-
responseTypes = { [code]: responseBodyHeaderObj };
2531-
Object.assign(responseBlock, responseTypes);
2526+
Object.assign(responseTypes, { [code]: responseBodyHeaderObj });
25322527

25332528
_.forOwn(resolvedExamples, (resolvedExample = {}) => {
25342529
let { body, contentHeader = [], bodyType, acceptHeader, name } = resolvedExample,
@@ -2593,7 +2588,7 @@ let QUERYPARAM = 'query',
25932588
return {
25942589
responses,
25952590
acceptHeader: requestAcceptHeader,
2596-
unifiedResponseTypes: responseBlock
2591+
responseTypes: responseTypes
25972592
};
25982593
};
25992594

@@ -2614,17 +2609,13 @@ module.exports = {
26142609
{ pathParamTypes, pathParams } = resolvePathParamsForPostmanRequest(context, operationItem, method),
26152610
{ pathVariables, collectionVariables } = filterCollectionAndPathVariables(url, pathParams),
26162611
requestBody = resolveRequestBodyForPostmanRequest(context, operationItem[method]),
2617-
bodyTypes = requestBody && requestBody.resolvedSchemaTypeObject,
2612+
requestBodyTypes = requestBody && requestBody.resolvedSchemaTypeObject,
26182613
request,
26192614
securitySchema = _.get(operationItem, [method, 'security']),
26202615
authHelper = generateAuthForCollectionFromOpenAPI(context.openapi, securitySchema),
26212616
{ alwaysInheritAuthentication } = context.computedOptions,
26222617
requestIdentifier,
2623-
requestBlock,
2624-
typesObject = {};
2625-
if (requestBody && requestBody.resolvedSchemaTypeObject) {
2626-
delete requestBody.resolvedSchemaTypeObject;
2627-
}
2618+
requestTypesObject = {};
26282619
headers.push(..._.get(requestBody, 'headers', []));
26292620
pathVariables.push(...baseUrlData.pathVariables);
26302621
collectionVariables.push(...baseUrlData.collectionVariables);
@@ -2645,8 +2636,8 @@ module.exports = {
26452636
auth: alwaysInheritAuthentication ? undefined : authHelper
26462637
};
26472638

2648-
const unifiedRequestTypes = {
2649-
body: JSON.stringify(bodyTypes, null, 2),
2639+
const requestTypes = {
2640+
body: JSON.stringify(requestBodyTypes, null, 2),
26502641
headers: JSON.stringify(headerTypes, null, 2),
26512642
pathParam: JSON.stringify(pathParamTypes, null, 2),
26522643
queryParam: JSON.stringify(queryParamTypes, null, 2)
@@ -2655,12 +2646,12 @@ module.exports = {
26552646
{
26562647
responses,
26572648
acceptHeader,
2658-
unifiedResponseTypes
2649+
responseTypes
26592650
} = resolveResponseForPostmanRequest(context, operationItem[method], request);
26602651

26612652
requestIdentifier = method + path;
2662-
requestBlock = { request: unifiedRequestTypes, response: unifiedResponseTypes };
2663-
Object.assign(typesObject, { [requestIdentifier]: requestBlock });
2653+
Object.assign(requestTypesObject,
2654+
{ [requestIdentifier]: { request: requestTypes, response: responseTypes } });
26642655

26652656
// add accept header if found and not present already
26662657
if (!_.isEmpty(acceptHeader)) {
@@ -2675,7 +2666,7 @@ module.exports = {
26752666
})
26762667
},
26772668
collectionVariables,
2678-
typesObject
2669+
requestTypesObject
26792670
};
26802671
},
26812672

0 commit comments

Comments
 (0)