Skip to content

Commit 0593b6b

Browse files
committed
prevent use of */* to end in an empty response
1 parent 851645d commit 0593b6b

File tree

3 files changed

+159
-19
lines changed

3 files changed

+159
-19
lines changed

lib/schemaUtils.js

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,8 @@ module.exports = {
13331333
convertToPmResponseBody: function(contentObj, components, options, schemaCache) {
13341334
options = _.merge({}, defaultOptions, options);
13351335

1336-
var responseBody, cTypeHeader, hasComputedType, cTypes;
1336+
var responseBody, cTypeHeader, hasComputedType, cTypes,
1337+
isJsonLike = false;
13371338
if (!contentObj) {
13381339
return {
13391340
contentTypeHeader: null,
@@ -1374,12 +1375,18 @@ module.exports = {
13741375
responseBody = JSON.stringify(responseBody, null, options.indentCharacter);
13751376
}
13761377
else if (typeof responseBody !== 'string') {
1377-
// since the collection v2 schema only supports body being a string
1378-
responseBody = '';
1378+
if (!_.isObject(responseBody) && _.isFunction(_.get(responseBody, 'toString'))) {
1379+
responseBody = responseBody.toString();
1380+
}
1381+
else {
1382+
responseBody = JSON.stringify(responseBody, null, options.indentCharacter);
1383+
isJsonLike = true;
1384+
}
13791385
}
13801386
return {
13811387
contentTypeHeader: cTypeHeader,
1382-
responseBody: responseBody
1388+
responseBody: responseBody,
1389+
isJsonLike
13831390
};
13841391
},
13851392

@@ -2108,7 +2115,27 @@ module.exports = {
21082115
});
21092116

21102117
responseBodyWrapper = this.convertToPmResponseBody(response.content, components, options, schemaCache);
2118+
previewLanguage = this.resolveResponsePreviewLanguageAndResponseHeader(responseBodyWrapper,
2119+
responseHeaders, response);
2120+
// replace 'X' char with '0'
2121+
code = code.replace(/X/g, '0');
2122+
code = code === 'default' ? 500 : _.toSafeInteger(code);
2123+
2124+
sdkResponse = new sdk.Response({
2125+
name: response.description,
2126+
code: code || 500,
2127+
header: responseHeaders,
2128+
body: responseBodyWrapper.responseBody,
2129+
originalRequest: originalRequest
2130+
});
2131+
sdkResponse._postman_previewlanguage = previewLanguage;
2132+
2133+
return sdkResponse;
2134+
},
21112135

2136+
resolveResponsePreviewLanguageAndResponseHeader: function (responseBodyWrapper,
2137+
responseHeaders, response) {
2138+
let previewLanguage = 'text';
21122139
if (responseBodyWrapper.contentTypeHeader) {
21132140
// we could infer the content-type header from the body
21142141
responseHeaders.push({ key: 'Content-Type', value: responseBodyWrapper.contentTypeHeader });
@@ -2118,6 +2145,9 @@ module.exports = {
21182145
else if (this.getHeaderFamily(responseBodyWrapper.contentTypeHeader) === HEADER_TYPE.XML) {
21192146
previewLanguage = PREVIEW_LANGUAGE.XML;
21202147
}
2148+
else if (responseBodyWrapper.isJsonLike) {
2149+
previewLanguage = PREVIEW_LANGUAGE.JSON;
2150+
}
21212151
}
21222152
else if (response.content && Object.keys(response.content).length > 0) {
21232153
responseHeaders.push({ key: 'Content-Type', value: Object.keys(response.content)[0] });
@@ -2127,24 +2157,14 @@ module.exports = {
21272157
else if (this.getHeaderFamily(Object.keys(response.content)[0]) === HEADER_TYPE.XML) {
21282158
previewLanguage = PREVIEW_LANGUAGE.XML;
21292159
}
2160+
else if (responseBodyWrapper.isJsonLike) {
2161+
previewLanguage = PREVIEW_LANGUAGE.JSON;
2162+
}
21302163
}
21312164
else {
21322165
responseHeaders.push({ key: 'Content-Type', value: TEXT_PLAIN });
21332166
}
2134-
// replace 'X' char with '0'
2135-
code = code.replace(/X/g, '0');
2136-
code = code === 'default' ? 500 : _.toSafeInteger(code);
2137-
2138-
sdkResponse = new sdk.Response({
2139-
name: response.description,
2140-
code: code || 500,
2141-
header: responseHeaders,
2142-
body: responseBodyWrapper.responseBody,
2143-
originalRequest: originalRequest
2144-
});
2145-
sdkResponse._postman_previewlanguage = previewLanguage;
2146-
2147-
return sdkResponse;
2167+
return previewLanguage;
21482168
},
21492169

21502170
/**
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"description": "This API provides access to signature platform \n\nWorkflow:\n\nDepending on the needs of the client, the order and use of the calls to the API may differ.<br>\nHere is an example of a very basic workflow:\n1) Create Request - Provides the information that will be used for the signer's certificate.\n2) Create Signature Request - Provides the information for the creation of the visible signature. Also provides the document(s) to be signed.\n3) Launch Signature Request - Launches the signature process based on the two previous requests.\n4) Get Signature Request Status - Returns the current status of the signature request and, if the status is \"SIGNED\", provides the signed document.\nA list of all requests is provided below, however the order is not necessarily correct for the signature process.\n\nWe suggest that the client begins with this basic workflow and expands based on their specific needs.",
5+
"version": "1.11.2",
6+
"title": "Signature api"
7+
},
8+
"host": "sign-sandbox.certeurope.fr",
9+
"basePath": "/",
10+
"tags": [{
11+
"name": "Signature",
12+
"description": "Centralize Signing API"
13+
}],
14+
"paths": {
15+
"/centralize/admin/orders/{raProfileId}": {
16+
"post": {
17+
"tags": ["Signature"],
18+
"summary": "createAdminCentralizedOrder",
19+
"operationId": "createAdminCentralizedOrderUsingPOST",
20+
"consumes": ["application/json"],
21+
"produces": ["*/*"],
22+
"parameters": [{
23+
"name": "raProfileId",
24+
"in": "path",
25+
"description": "raProfileId",
26+
"required": true,
27+
"type": "integer",
28+
"format": "int64"
29+
}, {
30+
"in": "body",
31+
"name": "request",
32+
"description": "request",
33+
"required": true,
34+
"schema": {
35+
"$ref": "#/definitions/CentralizedOrderRequestDTO"
36+
}
37+
}],
38+
"responses": {
39+
"200": {
40+
"description": "OK",
41+
"schema": {
42+
"$ref": "#/definitions/AliveOKDTO"
43+
}
44+
}
45+
}
46+
}
47+
}
48+
},
49+
"definitions": {
50+
"AliveOKDTO": {
51+
"type": "object",
52+
"properties": {
53+
"archiver": {
54+
"type": "boolean"
55+
},
56+
"kms": {
57+
"type": "boolean"
58+
},
59+
"ok": {
60+
"type": "boolean"
61+
},
62+
"ts": {
63+
"type": "boolean"
64+
}
65+
},
66+
"title": "AliveOKDTO"
67+
},
68+
"CentralizedOrderRequestDTO": {
69+
"type": "object",
70+
"properties": {
71+
"aeFolderId": {
72+
"type": "string"
73+
},
74+
"clientIdentifier": {
75+
"type": "string"
76+
},
77+
"enableEmail": {
78+
"type": "boolean"
79+
},
80+
"enableOtp": {
81+
"type": "boolean"
82+
},
83+
"enablePin": {
84+
"type": "boolean"
85+
},
86+
"enableSharing": {
87+
"type": "boolean"
88+
},
89+
"externalOrderRequestId": {
90+
"type": "string"
91+
},
92+
"otpContact": {
93+
"type": "string"
94+
},
95+
"signatureMode": {
96+
"type": "string",
97+
"enum": ["SOFTWARE", "HARDWARE"]
98+
}
99+
},
100+
"title": "CentralizedOrderRequestDTO"
101+
}
102+
}
103+
}

test/unit/x20schemapack.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('getMetaData method', function() {
4747
});
4848

4949
describe('Convert method', function() {
50-
it('Should convert an example file from: ', function(done) {
50+
it('Should convert an example file from: sampleswagger.json', function(done) {
5151
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'sampleswagger.json'),
5252
fileData = fs.readFileSync(fileSource, 'utf8'),
5353
input = {
@@ -62,6 +62,23 @@ describe('Convert method', function() {
6262
});
6363
done();
6464
});
65+
66+
it('Should convert an example file from: rangeMediaType.json', function(done) {
67+
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'rangeMediaType.json'),
68+
fileData = fs.readFileSync(fileSource, 'utf8'),
69+
input = {
70+
type: 'string',
71+
data: fileData
72+
},
73+
schemapack = new SchemaPack(input);
74+
75+
schemapack.convert((error, result) => {
76+
expect(error).to.be.null;
77+
expect(result.output[0].data.item[0].response[0]._postman_previewlanguage).to.equal('json');
78+
expect(result.output[0].data.item[0].response[0].body).to.not.be.empty;
79+
});
80+
done();
81+
});
6582
});
6683

6784
describe('Swagger 2.0 schemapack mergeAndValidate method', function() {

0 commit comments

Comments
 (0)