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

192 multiple request examples #530

Open
wants to merge 3 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
52 changes: 52 additions & 0 deletions defs/issue192.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"openapi": "3.0.1",
"info": {
"title": "API",
"version": "1.0.0"
},
"paths": {
"/test": {
"post": {
"responses": {
"200": {
"description": "OK"
}
},
"requestBody": {
"description": "Overall description",
"content": {
"application/json": {
"examples": {
"MyFirstExample": {
"value": {
"test": 123
},
"description": "Test description one"
},
"MySecondExample": {
"value": {
"test": 456
},
"description": "second test description"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Cat": {
"type": "object"
},
"Dog": {
"type": "object"
},
"Hamster": {
"type": "object"
}
}
}
}
92 changes: 61 additions & 31 deletions lib/openapi3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const util = require('util');
const up = require('url');

const yaml = require('yaml');
const jsonTrunc = require('./jsonTrunc.js');
const uri = require('urijs');
const URITemplate = require('urijs/src/URITemplate');
const dot = require('dot');
dot.templateSettings.strip = false;
Expand Down Expand Up @@ -330,7 +328,7 @@ function getParameters(data) {
}

function getBodyParameterExamples(data) {
let obj = data.bodyParameter.exampleValues.object;
let examples = [];
let content = '';
let xmlWrap = false;
if (data.bodyParameter.schema && data.bodyParameter.schema.xml) {
Expand All @@ -339,35 +337,67 @@ function getBodyParameterExamples(data) {
else if (data.bodyParameter.schema && data.bodyParameter.schema["x-widdershins-oldRef"]) {
xmlWrap = data.bodyParameter.schema["x-widdershins-oldRef"].split('/').pop();
}
if (common.doContentType(data.consumes, 'json')) {
content += '```json\n';
content += common.safejson(obj, null, 2) + '\n';
content += '```\n\n';
}
if (common.doContentType(data.consumes, 'yaml')) {
content += '```yaml\n';
content += yaml.stringify(obj) + '\n';
content += '```\n\n';
}
if (common.doContentType(data.consumes, 'text')) {
content += '```\n';
content += yaml.stringify(obj) + '\n';
content += '```\n\n';
}
if (common.doContentType(data.consumes, 'form')) {
content += '```yaml\n';
content += yaml.stringify(obj) + '\n';
content += '```\n\n';

let requestBody = data.operation.requestBody;
for (let reqBodyExample in requestBody.content) {
for (let ct in requestBody.content) {
let contentType = requestBody.content[ct];
let cta = [ct];
// support multiple embedded body examples

if (contentType.examples) {
for (let ctei in contentType.examples) {
let example = contentType.examples[ctei];
examples.push({description:example.description,value: common.clean(example.value), cta: cta});
}
}
else if (contentType.example) {
examples.push({description:reqBodyExample+' '+data.translations.requestBody,value:common.clean(contentType.example.value), cta: cta});
}
else if (contentType.schema) {
let obj = contentType.schema;
let xmlWrap = false;
if (obj && obj.xml && obj.xml.name) {
xmlWrap = obj.xml.name;
}
else if (obj["x-widdershins-oldRef"]) {
xmlWrap = obj["x-widdershins-oldRef"].split('/').pop();
}
examples.push({description:reqBodyExample,value:common.getSample(obj,data.options,{skipWriteOnly:true},data.api), cta: cta, xmlWrap: xmlWrap});
}
}
}
if (common.doContentType(data.consumes, 'xml') && (typeof obj === 'object')) {
if (xmlWrap) {
var newObj = {};
newObj[xmlWrap] = obj;
obj = newObj;
}
content += '```xml\n';
content += xml.getXml(JSON.parse(common.safejson(obj)), '@', '', true, ' ', false) + '\n';
content += '```\n\n';
let lastDesc = '';
for (let example of examples) {
if (example.description && example.description !== lastDesc) {
content += '> ' + example.description + '\n\n';
lastDesc = example.description;
}
if (common.doContentType(example.cta, 'json')) {
content += '```json\n';
content += common.safejson(example.value, null, 2) + '\n';
content += '```\n\n';
}
if (common.doContentType(example.cta, 'yaml')) {
content += '```yaml\n';
content += yaml.stringify(example.value) + '\n';
content += '```\n\n';
}
if (common.doContentType(example.cta, 'text')) {
content += '```\n';
content += JSON.stringify(example.value) + '\n';
content += '```\n\n';
}
let xmlObj = example.value;
if (example.xmlWrap) {
xmlObj = {};
xmlObj[example.xmlWrap] = example.value;
}
if ((typeof xmlObj === 'object') && common.doContentType(example.cta, 'xml')) {
content += '```xml\n';
content += xml.getXml(JSON.parse(common.safejson(xmlObj)), '@', '', true, ' ', false) + '\n';
content += '```\n\n';
}
}
return content;
}
Expand Down