Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support for multiple response content-types see #436 #439

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
62 changes: 40 additions & 22 deletions lib/openapi3.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,40 +427,58 @@ function getResponses(data) {
for (let r in data.operation.responses) {
if (!r.startsWith('x-')) {
let response = data.operation.responses[r];
let entry = {};
entry.status = r;
entry.meaning = (r === 'default' ? data.translations.responseDefault : data.translations.responseUnknown);

let meta = {};
meta.meaning = (r === 'default' ? data.translations.responseDefault : data.translations.responseUnknown);
var url = '';
for (var s in common.statusCodes) {
if (common.statusCodes[s].code === r) {
entry.meaning = common.statusCodes[s].phrase;
meta.meaning = common.statusCodes[s].phrase;
url = common.statusCodes[s].spec_href;
break;
}
}
if (url) entry.meaning = '[' + entry.meaning + '](' + url + ')';
entry.description = (typeof response.description === 'string' ? response.description.trim() : undefined);
entry.schema = data.translations.schemaNone;
if (url) meta.meaning = '[' + meta.meaning + '](' + url + ')';
meta.description = (typeof response.description === 'string' ? response.description.trim() : undefined);

for (let ct in response.content) {
let entry = {...meta};
entry.status = r;
entry.schema = data.translations.schemaNone;

let contentType = response.content[ct];
if (contentType.schema) {
entry.type = contentType.schema.type;
entry.schema = data.translations.schemaInline;
}
if (contentType.schema && contentType.schema["x-widdershins-oldRef"] && contentType.schema["x-widdershins-oldRef"].startsWith('#/components/')) {
let schemaName = contentType.schema["x-widdershins-oldRef"].replace('#/components/schemas/', '');
entry.schema = '[' + schemaName + '](#schema' + schemaName.toLowerCase() + ')';
entry.$ref = true;
}
else {
if (contentType.schema && contentType.schema.type && (contentType.schema.type !== 'object') && (contentType.schema.type !== 'array')) {
entry.schema = contentType.schema.type;
entry.contentType = ct;

let ctSchema = contentType.schema;
if (ctSchema) {
entry.type = ctSchema.type;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous.

I guess is nice having format as well but seeing we're using ctSchema.type below this point and entry.originalType is being set, there is no need to assign entry.type.

entry.format = ctSchema.format;

entry.originalType = ctSchema.type;
entry.safeType = ctSchema.type || common.inferType(ctSchema);
if (ctSchema.format) {
entry.safeType = entry.safeType + '(' + ctSchema.format + ')';
}
if ((entry.safeType === 'array') && (ctSchema.items)) {
let itemsType = ctSchema.items.type;
if (!itemsType) {
itemsType = common.inferType(ctSchema.items);
}
entry.safeType = 'array[' + itemsType + ']';
}
if (ctSchema["x-widdershins-oldRef"]) {
let schemaName = ctSchema["x-widdershins-oldRef"].replace('#/components/schemas/', '');
entry.safeType = '[' + schemaName + '](#schema' + schemaName.toLowerCase() + ')';
entry.$ref = true;
}
} else {
entry.safeType = data.translations.schemaInline;
}

entry.content = response.content;
entry.links = response.links;
responses.push(entry);
}
entry.content = response.content;
entry.links = response.links;
responses.push(entry);
}
}
return responses;
Expand Down
6 changes: 3 additions & 3 deletions templates/openapi3/responses.def
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
{{= data.tags.section }}
<h3 id="{{=data.operationUniqueSlug}}-responses">Responses</h3>

|Status|Meaning|Description|Schema|
|---|---|---|---|
{{~ data.responses :r}}|{{=r.status}}|{{=r.meaning}}|{{=r.description || 'none'}}|{{=r.schema}}|
|Content-Type|Status|Meaning|Description|Schema|
|---|---|---|---|---|
{{~ data.responses :r}}|{{=r.contentType}}|{{=r.status}}|{{=r.meaning}}|{{=r.description || 'none'}}|{{=r.safeType}}|
{{~}}

{{ data.responseSchemas = false; }}
Expand Down