Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dist/gen/js/genOperations.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions dist/spec/operations.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/gen/js/genOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,15 @@ function renderOperationInfo(
if (hasBody && op.contentTypes.length) {
lines.push(`${SP}contentTypes: ['${op.contentTypes.join("','")}'],`);
}
// Only emit `accepts` when it carries real signal (e.g. XML); JSON is the
// gateway default, so omitting it keeps JSON clients byte-identical.
if (
op.accepts &&
op.accepts.length &&
!(op.accepts.length === 1 && op.accepts[0] === 'application/json')
) {
lines.push(`${SP}accepts: ['${op.accepts.join("','")}'],`);
}
lines.push(`${SP}method: '${op.method}'${op.security ? ',' : ''}`);
if (op.security && op.security.length) {
const secLines = renderSecurityInfo(op.security);
Expand Down
26 changes: 26 additions & 0 deletions src/spec/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ function getPathOperation(
description: op.requestBody.description || '',
});
}
// OpenAPI 3.0 encodes the request media type(s) under requestBody.content
// (Swagger 2.0 used `consumes`). Propagate them so non-JSON bodies (e.g. XML)
// are not defaulted to application/json downstream.
const requestContentTypes = Object.keys(content || {});
if (requestContentTypes.length) op.contentTypes = requestContentTypes;
delete op.requestBody;
}

Expand All @@ -92,6 +97,14 @@ function getPathOperation(
delete operation.consumes;
delete operation.produces;

// OpenAPI 3.0 encodes response media types under responses[code].content
// (Swagger 2.0 used `produces`). Derive the Accept header from the success
// responses so non-JSON responses (e.g. XML) are not defaulted to JSON.
if (!op.accepts || !op.accepts.length) {
const responseContentTypes = getResponseContentTypes(op.responses);
if (responseContentTypes.length) op.accepts = responseContentTypes;
}

if (!op.contentTypes || !op.contentTypes.length)
op.contentTypes = spec.contentTypes.slice();
if (!op.accepts || !op.accepts.length) op.accepts = spec.accepts.slice();
Expand Down Expand Up @@ -121,6 +134,19 @@ function getOperationResponses(op: any): ApiOperationResponse[] {
});
}

function getResponseContentTypes(responses: any[]): string[] {
const types: string[] = [];
for (const res of responses || []) {
if (!res || !res.content) continue;
// Only success responses inform the Accept header the client sends.
if (!/^2/.test(String(res.code))) continue;
for (const type of Object.keys(res.content)) {
if (!types.includes(type)) types.push(type);
}
}
return types;
}

function getOperationSecurity(op: any, spec: any): ApiOperationSecurity[] {
let security;

Expand Down
59 changes: 59 additions & 0 deletions test/openapi3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Content Type Test
paths:
/xml:
post:
operationId: xmlEcho
tags:
- ct
requestBody:
required: true
content:
application/xml:
schema:
type: string
responses:
'200':
description: ok
content:
application/xml:
schema:
type: string
/json:
post:
operationId: jsonCreate
tags:
- ct
requestBody:
required: true
content:
application/json:
schema:
type: object
responses:
'201':
description: created
content:
application/json:
schema:
type: object
/mixed:
post:
operationId: jsonReqXmlResp
tags:
- ct
requestBody:
required: true
content:
application/json:
schema:
type: object
responses:
'200':
description: ok
content:
application/xml:
schema:
type: string
30 changes: 30 additions & 0 deletions test/spec/operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,34 @@ describe('operations', () => {
const resDefault = listPets.responses.find((res) => res.code === 'default');
expect(resDefault).not.toBeFalsy();
});

describe('OpenAPI 3.0 content types', () => {
let operations: any[];
beforeAll(async () => {
const spec = await resolveSpec(`${__dirname}/../openapi3.yml`);
operations = getOperations(spec);
});

it('derives request contentTypes from requestBody.content (XML)', () => {
const op = operations.find((o) => o.id === 'xmlEcho');
expect(op.contentTypes).toEqual(['application/xml']);
});

it('derives accepts from the success response content (XML)', () => {
const op = operations.find((o) => o.id === 'xmlEcho');
expect(op.accepts).toEqual(['application/xml']);
});

it('keeps JSON request/response as application/json', () => {
const op = operations.find((o) => o.id === 'jsonCreate');
expect(op.contentTypes).toEqual(['application/json']);
expect(op.accepts).toEqual(['application/json']);
});

it('derives request and response content types independently', () => {
const op = operations.find((o) => o.id === 'jsonReqXmlResp');
expect(op.contentTypes).toEqual(['application/json']);
expect(op.accepts).toEqual(['application/xml']);
});
});
});