From d2066aef2b2c108bafb29c45fe95b8553916d9f4 Mon Sep 17 00:00:00 2001 From: Mr-Yellow Date: Wed, 14 Apr 2021 12:48:22 +1000 Subject: [PATCH 1/4] fix: Support for multiple response content-types see #436 --- lib/openapi3.js | 37 +++++++++++++++++--------------- templates/openapi3/responses.def | 6 +++--- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/openapi3.js b/lib/openapi3.js index e159ce4f..8f1853a9 100644 --- a/lib/openapi3.js +++ b/lib/openapi3.js @@ -427,22 +427,24 @@ 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); - var url = ''; - for (var s in common.statusCodes) { - if (common.statusCodes[s].code === r) { - entry.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; for (let ct in response.content) { + let entry = {}; + entry.status = r; + entry.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; + 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; + let contentType = response.content[ct]; + entry.contentType = ct; if (contentType.schema) { entry.type = contentType.schema.type; entry.schema = data.translations.schemaInline; @@ -457,10 +459,11 @@ function getResponses(data) { entry.schema = contentType.schema.type; } } + + entry.content = response.content; + entry.links = response.links; + responses.push(entry); } - entry.content = response.content; - entry.links = response.links; - responses.push(entry); } } return responses; diff --git a/templates/openapi3/responses.def b/templates/openapi3/responses.def index ac9679ef..99abd655 100644 --- a/templates/openapi3/responses.def +++ b/templates/openapi3/responses.def @@ -13,9 +13,9 @@ {{= data.tags.section }}

Responses

-|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.schema}}| {{~}} {{ data.responseSchemas = false; }} From 99e191810fed1651213337f540f820e9bba729a5 Mon Sep 17 00:00:00 2001 From: Mr-Yellow Date: Wed, 14 Apr 2021 12:57:34 +1000 Subject: [PATCH 2/4] refactor: Initialise response meta-data before iterating types --- lib/openapi3.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/openapi3.js b/lib/openapi3.js index 8f1853a9..6beee6ab 100644 --- a/lib/openapi3.js +++ b/lib/openapi3.js @@ -427,20 +427,23 @@ function getResponses(data) { for (let r in data.operation.responses) { if (!r.startsWith('x-')) { let response = data.operation.responses[r]; + + 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) { + meta.meaning = common.statusCodes[s].phrase; + url = common.statusCodes[s].spec_href; + break; + } + } + 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 = {}; + let entry = {...meta}; entry.status = r; - entry.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; - 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; let contentType = response.content[ct]; From b08d63c6f9856bd3902b9cbc20301b298485cf07 Mon Sep 17 00:00:00 2001 From: Mr-Yellow Date: Wed, 14 Apr 2021 13:30:41 +1000 Subject: [PATCH 3/4] fix: Include format in type with similar pattern to parameters --- lib/openapi3.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/openapi3.js b/lib/openapi3.js index 6beee6ab..226ac9e2 100644 --- a/lib/openapi3.js +++ b/lib/openapi3.js @@ -448,19 +448,31 @@ function getResponses(data) { let contentType = response.content[ct]; entry.contentType = 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; + + let ctSchema = contentType.schema; + if (ctSchema) { + entry.type = ctSchema.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; From 1cfbf3145bc3717128cabfd55cfdafd23be89e58 Mon Sep 17 00:00:00 2001 From: Mr-Yellow Date: Wed, 14 Apr 2021 13:33:12 +1000 Subject: [PATCH 4/4] fix: Utilising safeType for schema description --- templates/openapi3/responses.def | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/openapi3/responses.def b/templates/openapi3/responses.def index 99abd655..28c2c5db 100644 --- a/templates/openapi3/responses.def +++ b/templates/openapi3/responses.def @@ -15,7 +15,7 @@ |Content-Type|Status|Meaning|Description|Schema| |---|---|---|---|---| -{{~ data.responses :r}}|{{=r.contentType}}|{{=r.status}}|{{=r.meaning}}|{{=r.description || 'none'}}|{{=r.schema}}| +{{~ data.responses :r}}|{{=r.contentType}}|{{=r.status}}|{{=r.meaning}}|{{=r.description || 'none'}}|{{=r.safeType}}| {{~}} {{ data.responseSchemas = false; }}