Skip to content

Commit 16a2147

Browse files
authored
Merge pull request #867 from postmanlabs/feature/2-way-sync-schema-changes
[AB-1450] schema changes for 2-way-sync
2 parents 8e399c7 + 2dd4b56 commit 16a2147

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

libV2/schemaUtils.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@ let QUERYPARAM = 'query',
792792

793793
if (resolvedSchema.type === 'object' && resolvedSchema.properties) {
794794
const schemaDetails = {
795+
description: resolvedSchema.description,
796+
title: resolvedSchema.title,
795797
type: resolvedSchema.type,
796798
properties: {},
797799
required: []
@@ -1176,16 +1178,24 @@ let QUERYPARAM = 'query',
11761178
* Gets the description of the parameter.
11771179
* If the parameter is required, it prepends a `(Requried)` before the parameter description
11781180
* If the parameter type is enum, it appends the possible enum values
1181+
* @param {object} context - Global context object controlling behavior
11791182
* @param {object} parameter - input param for which description needs to be returned
11801183
* @returns {string} description of the parameters
11811184
*/
1182-
getParameterDescription = (parameter) => {
1185+
getParameterDescription = (context, parameter) => {
11831186
if (!_.isObject(parameter)) {
11841187
return '';
11851188
}
11861189

1187-
return (parameter.required ? '(Required) ' : '') + (parameter.description || '') +
1188-
(parameter.enum ? ' (This can only be one of ' + parameter.enum + ')' : '');
1190+
const requiredPrefix = (context && !context.enableTypeFetching && parameter.required ? '(Required) ' : ''),
1191+
desc = parameter.description || '';
1192+
1193+
let enumDescription = '';
1194+
if (parameter && parameter.schema && parameter.schema.enum && !(context && context.enableTypeFetching)) {
1195+
enumDescription = ' (This can only be one of ' + parameter.schema.enum + ')';
1196+
}
1197+
1198+
return requiredPrefix + desc + enumDescription;
11891199
},
11901200

11911201
/**
@@ -1204,7 +1214,7 @@ let QUERYPARAM = 'query',
12041214
{ enableOptionalParameters } = context.computedOptions;
12051215

12061216
let serialisedValue = '',
1207-
description = getParameterDescription(param),
1217+
description = getParameterDescription(context, param),
12081218
paramName = _.get(param, 'name'),
12091219
disabled = !enableOptionalParameters && _.get(param, 'required') !== true,
12101220
pmParams = [],
@@ -1844,7 +1854,7 @@ let QUERYPARAM = 'query',
18441854
paramSchema.required = _.has(paramSchema, 'required') ?
18451855
paramSchema.required :
18461856
_.indexOf(requestBodySchema.required, key) !== -1;
1847-
description = getParameterDescription(paramSchema);
1857+
description = getParameterDescription(context, paramSchema);
18481858

18491859
if (typeof _.get(encoding, `[${key}].contentType`) === 'string') {
18501860
contentType = encoding[key].contentType;
@@ -2099,8 +2109,8 @@ let QUERYPARAM = 'query',
20992109
type: schema.type,
21002110
format: schema.format,
21012111
default: schema.default,
2102-
required: param.required || false,
2103-
deprecated: param.deprecated || false,
2112+
required: param.required,
2113+
deprecated: param.deprecated,
21042114
enum: schema.enum || undefined,
21052115
minLength: schema.minLength,
21062116
maxLength: schema.maxLength,
@@ -2442,8 +2452,8 @@ let QUERYPARAM = 'query',
24422452
type: schema.type,
24432453
format: schema.format,
24442454
default: schema.default,
2445-
required: schema.required || false,
2446-
deprecated: schema.deprecated || false,
2455+
required: schema.required,
2456+
deprecated: schema.deprecated,
24472457
enum: schema.enum || undefined,
24482458
minLength: schema.minLength,
24492459
maxLength: schema.maxLength,

test/data/valid_openapi/test1.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,10 @@
281281
"in": "query",
282282
"description": "component level query param",
283283
"required": false,
284+
"deprecated": false,
284285
"schema": {
285-
"type": "string"
286+
"type": "string",
287+
"enum": ["medium"]
286288
}
287289
}
288290
},

test/unit/convertV2.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,17 @@ describe('The convert v2 Function', function() {
279279
it('Should generate collection conforming to schema for and fail if not valid ' +
280280
testSpec1, function(done) {
281281
Converter.convertV2({ type: 'file', data: testSpec1 }, { requestNameSource: 'url' }, (err, conversionResult) => {
282+
const firstFolder = conversionResult.output[0].data.item[0];
283+
const listAllPets = firstFolder.item[0];
284+
const queryParams = listAllPets.request.url.query;
285+
const limitDescription = queryParams[0].description.content;
282286
expect(err).to.be.null;
283287
expect(conversionResult.result).to.equal(true);
284288
expect(conversionResult.output.length).to.equal(1);
285289
expect(conversionResult.output[0].type).to.equal('collection');
286290
expect(conversionResult.output[0].data).to.have.property('info');
287291
expect(conversionResult.output[0].data).to.have.property('item');
292+
expect(limitDescription).to.equal('component level query param (This can only be one of medium)');
288293

289294
done();
290295
});

test/unit/convertV2WithTypes.test.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const expect = require('chai').expect,
2929
(acc, [key, value]) => {
3030
acc[key] = {
3131
type: value.type,
32-
deprecated: value.deprecated || false,
32+
deprecated: value.deprecated,
3333
enum: value.enum !== null ? value.enum : undefined,
3434
minLength: value.minLength !== null ? value.minLength : undefined,
3535
maxLength: value.maxLength !== null ? value.maxLength : undefined,
@@ -75,22 +75,28 @@ describe('convertV2WithTypes should generate collection conforming to collection
7575
expect(conversionResult.output).to.be.an('array').that.is.not.empty;
7676

7777
const firstFolder = conversionResult.output[0].data.item[0];
78+
const secondFolder = conversionResult.output[0].data.item[1];
7879
expect(firstFolder).to.have.property('name', 'pets');
7980

8081
const listAllPets = firstFolder.item[0];
8182
expect(listAllPets).to.have.property('name', 'List all pets');
8283
expect(listAllPets.request.method).to.equal('GET');
8384

8485
const createPet = firstFolder.item[1];
86+
const getPetById = secondFolder.item[0];
87+
const idDescription = getPetById.item[0].request.url.variable[0].description.content;
8588
expect(createPet).to.have.property('name', '/pets');
8689
expect(createPet.request.method).to.equal('POST');
90+
expect(idDescription).to.equal('The id of the pet to retrieve');
8791
expect(createPet.request.body.mode).to.equal('raw');
8892
expect(createPet.request.body.raw).to.include('request body comes here');
8993

9094
const queryParams = listAllPets.request.url.query;
9195
expect(queryParams).to.be.an('array').that.has.length(3);
9296
expect(queryParams[0]).to.have.property('key', 'limit');
93-
expect(queryParams[0]).to.have.property('value', '<string>');
97+
expect(queryParams[0]).to.have.property('value', 'medium');
98+
const limitDescription = queryParams[0].description.content;
99+
expect(limitDescription).to.equal('component level query param');
94100

95101
const headers = listAllPets.request.header;
96102
expect(headers).to.be.an('array').that.is.not.empty;
@@ -207,14 +213,14 @@ describe('convertV2WithTypes', function() {
207213
const expectedExtractedTypes = {
208214
'get/pets': {
209215
'request': {
210-
'headers': '[\n {\n "keyName": "variable",\n "properties": {\n "type": "array",\n "required": false,\n "deprecated": false\n }\n }\n]',
216+
'headers': '[\n {\n "keyName": "variable",\n "properties": {\n "type": "array"\n }\n }\n]',
211217
'pathParam': '[]',
212-
'queryParam': '[\n {\n "keyName": "limit",\n "properties": {\n "type": "string",\n "default": "<string>",\n "required": false,\n "deprecated": false\n }\n },\n {\n "keyName": "variable2",\n "properties": {\n "type": "array",\n "required": false,\n "deprecated": false\n }\n },\n {\n "keyName": "variable3",\n "properties": {\n "type": "array",\n "required": false,\n "deprecated": false\n }\n }\n]'
218+
'queryParam': '[\n {\n "keyName": "limit",\n "properties": {\n "type": "string",\n "required": false,\n "deprecated": false,\n "enum": [\n "medium"\n ]\n }\n },\n {\n "keyName": "variable2",\n "properties": {\n "type": "array"\n }\n },\n {\n "keyName": "variable3",\n "properties": {\n "type": "array"\n }\n }\n]'
213219
},
214220
'response': {
215221
'200': {
216222
'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}',
217-
'headers': '[\n {\n "keyName": "x-next",\n "properties": {\n "type": "string",\n "default": "<string>",\n "required": false,\n "deprecated": false\n }\n }\n]'
223+
'headers': '[\n {\n "keyName": "x-next",\n "properties": {\n "type": "string",\n "default": "<string>"\n }\n }\n]'
218224
},
219225
'500': {
220226
'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}',
@@ -226,7 +232,7 @@ describe('convertV2WithTypes', function() {
226232
'request': {
227233
'headers': '[]',
228234
'pathParam': '[]',
229-
'queryParam': '[\n {\n "keyName": "limit",\n "properties": {\n "type": "string",\n "default": "<string>",\n "required": false,\n "deprecated": false\n }\n },\n {\n "keyName": "variable3",\n "properties": {\n "type": "array",\n "required": false,\n "deprecated": false\n }\n }\n]'
235+
'queryParam': '[\n {\n "keyName": "limit",\n "properties": {\n "type": "string",\n "required": false,\n "deprecated": false,\n "enum": [\n "medium"\n ]\n }\n },\n {\n "keyName": "variable3",\n "properties": {\n "type": "array"\n }\n }\n]'
230236
},
231237
'response': {
232238
'201': {
@@ -241,7 +247,7 @@ describe('convertV2WithTypes', function() {
241247
'get/pet/{petId}': {
242248
'request': {
243249
'headers': '[]',
244-
'pathParam': '[\n {\n "keyName": "petId",\n "properties": {\n "type": "string",\n "default": "<string>",\n "required": true,\n "deprecated": false\n }\n }\n]',
250+
'pathParam': '[\n {\n "keyName": "petId",\n "properties": {\n "type": "string",\n "default": "<string>",\n "required": true\n }\n }\n]',
245251
'queryParam': '[]'
246252
},
247253
'response': {
@@ -258,7 +264,7 @@ describe('convertV2WithTypes', function() {
258264
'post/pet/{petId}': {
259265
'request': {
260266
'headers': '[]',
261-
'pathParam': '[\n {\n "keyName": "petId",\n "properties": {\n "type": "string",\n "default": "<string>",\n "required": true,\n "deprecated": false\n }\n }\n]',
267+
'pathParam': '[\n {\n "keyName": "petId",\n "properties": {\n "type": "string",\n "default": "<string>",\n "required": true\n }\n }\n]',
262268
'queryParam': '[]'
263269
},
264270
'response': {

0 commit comments

Comments
 (0)