Skip to content

Commit 806032d

Browse files
authored
fix(build-request): create proper accept header (#1981)
Set accept header according to the the produces content-types defined in the API. This fix uses the content-type defined in the operation and sets the accept header, to contain all of these content-types. If the option responseContentType is provided, only this value will be set as accept header. Closes #1116
1 parent b6d3751 commit 806032d

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

src/execute/oas3/build-request.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// `src/execute/index.js#buildRequest`
33
import assign from 'lodash/assign';
44
import get from 'lodash/get';
5+
import isPlainObject from 'lodash/isPlainObject';
56
import btoa from 'btoa';
67

78
export default function buildRequest(options, req) {
@@ -37,6 +38,17 @@ export default function buildRequest(options, req) {
3738
} else if (requestContentType && isExplicitContentTypeValid) {
3839
req.headers['Content-Type'] = requestContentType;
3940
}
41+
if (!options.responseContentType && operation.responses) {
42+
const mediaTypes = Object.entries(operation.responses)
43+
.filter(([key, value]) => {
44+
const code = parseInt(key, 10);
45+
return code >= 200 && code < 300 && isPlainObject(value.content);
46+
})
47+
.reduce((acc, [, value]) => acc.concat(Object.keys(value.content)), []);
48+
if (mediaTypes.length > 0) {
49+
req.headers.accept = mediaTypes.join(', ');
50+
}
51+
}
4052

4153
// for OAS3: add requestBody to request
4254
if (requestBody) {

src/execute/swagger2/build-request.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default function buildRequest(options, req) {
99
operation,
1010
securities,
1111
requestContentType,
12+
responseContentType,
1213
attachContentTypeForEmptyPayload,
1314
} = options;
1415
// Add securities, which are applicable
@@ -47,6 +48,9 @@ export default function buildRequest(options, req) {
4748
req.headers['Content-Type'] = requestContentType;
4849
}
4950
}
51+
if (!responseContentType && Array.isArray(operation.produces) && operation.produces.length > 0) {
52+
req.headers.accept = operation.produces.join(', ');
53+
}
5054

5155
return req;
5256
}

test/execute/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ describe('execute', () => {
880880
method: 'PUT',
881881
credentials: 'same-origin',
882882
headers: {
883+
accept: 'mime/silent-french-type',
883884
head: 'justTheHead',
884885
},
885886
body: {

test/oas3/execute/build-request.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// https://github.com/swagger-api/swagger-js/issues/1116
2+
import { buildRequest } from '../../../src/execute';
3+
4+
describe('buildRequest - OAS 3.0.x', () => {
5+
describe('test accept header', () => {
6+
const spec = {
7+
openapi: '3.0.0',
8+
servers: [
9+
{
10+
url: 'https://test.com/v1',
11+
},
12+
],
13+
paths: {
14+
'/getMultiple': {
15+
get: {
16+
operationId: 'getMultiple',
17+
responses: {
18+
200: {
19+
content: {
20+
'application/xml': {
21+
schema: {
22+
type: 'array',
23+
items: 'string',
24+
},
25+
},
26+
'application/json': {
27+
schema: {
28+
type: 'array',
29+
items: 'string',
30+
},
31+
},
32+
},
33+
},
34+
default: {
35+
description: 'Unexpected error',
36+
content: {
37+
'application/json': {
38+
schema: {
39+
type: 'array',
40+
items: 'string',
41+
},
42+
},
43+
},
44+
},
45+
},
46+
},
47+
},
48+
},
49+
};
50+
51+
test('should generate accept header according to defined responses', () => {
52+
const req = buildRequest({
53+
spec,
54+
operationId: 'getMultiple',
55+
});
56+
57+
expect(req).toEqual({
58+
url: 'https://test.com/v1/getMultiple',
59+
method: 'GET',
60+
credentials: 'same-origin',
61+
headers: {
62+
accept: 'application/xml, application/json',
63+
},
64+
});
65+
});
66+
67+
test('should allow to override accept header according to defined responses', () => {
68+
const req = buildRequest({
69+
spec,
70+
operationId: 'getMultiple',
71+
responseContentType: 'application/xml',
72+
});
73+
74+
expect(req).toEqual({
75+
url: 'https://test.com/v1/getMultiple',
76+
method: 'GET',
77+
credentials: 'same-origin',
78+
headers: {
79+
accept: 'application/xml',
80+
},
81+
});
82+
});
83+
});
84+
});

test/swagger2/execute/build-request.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,61 @@ describe('buildRequest - swagger 2.0', () => {
284284
});
285285
});
286286
});
287+
288+
describe('test accept header', () => {
289+
const spec = {
290+
host: 'test.com',
291+
basePath: '/v1',
292+
schemes: ['https'],
293+
paths: {
294+
'/getMultiple': {
295+
get: {
296+
operationId: 'getMultiple',
297+
produces: ['application/xml', 'application/json'],
298+
responses: {
299+
200: {
300+
schema: {
301+
type: 'array',
302+
items: 'string',
303+
},
304+
},
305+
},
306+
},
307+
},
308+
},
309+
};
310+
311+
test('should generate accept header according to defined responses', () => {
312+
const req = buildRequest({
313+
spec,
314+
operationId: 'getMultiple',
315+
});
316+
317+
expect(req).toEqual({
318+
url: 'https://test.com/v1/getMultiple',
319+
method: 'GET',
320+
credentials: 'same-origin',
321+
headers: {
322+
accept: 'application/xml, application/json',
323+
},
324+
});
325+
});
326+
327+
test('should allow to override accept header according to defined responses', () => {
328+
const req = buildRequest({
329+
spec,
330+
operationId: 'getMultiple',
331+
responseContentType: 'application/xml',
332+
});
333+
334+
expect(req).toEqual({
335+
url: 'https://test.com/v1/getMultiple',
336+
method: 'GET',
337+
credentials: 'same-origin',
338+
headers: {
339+
accept: 'application/xml',
340+
},
341+
});
342+
});
343+
});
287344
});

0 commit comments

Comments
 (0)