Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
First pass at outputting example response bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Oct 19, 2017
1 parent b049885 commit 259d050
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"openapi": "3.0.0-rc2",
"servers": [
{
"url": "http://example.com"
}
],
"info": {
"title": "example results",
"version": "1.0"
},
"paths": {
"/results": {
"get": {
"description": "",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"examples": {
"response": {
"value": {
"user": {
"email": "test@example.com",
"name": "Test user name"
}
}
}
}
}
}
},
"400": {
"description": "Validation failed",
"content": {
"application/json": {
"examples": {
"response": {
"value": {
"email": "invalid email",
"name": "name already taken"
}
}
}
}
}
}
},
"summary": "Update Password"
}
}
},
"components": {
"schemas": {},
"responses": {},
"parameters": {},
"examples": {},
"requestBodies": {},
"securitySchemes": {},
"headers": {}
},
"x-explorer-enabled": true,
"x-samples-enabled": true,
"x-samples-languages": ["curl", "node", "ruby", "javascript", "python"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"openapi": "3.0.0-rc2",
"servers": [
{
"url": "http://example.com"
}
],
"info": {
"title": "example results",
"version": "1.0"
},
"paths": {
"/results": {
"get": {
"description": "",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"examples": {
"response": {
"value": {
"user": {
"email": "test@example.com",
"name": "Test user name"
}
}
}
}
}
}
},
"400": {
"description": "Validation failed",
"content": {
"application/json": {
"examples": {
"response": {
"value": {
"email": "invalid email",
"name": "name already taken"
}
}
}
}
}
}
},
"summary": "Update Password"
}
}
},
"components": {
"schemas": {},
"responses": {},
"parameters": {},
"examples": {},
"requestBodies": {},
"securitySchemes": {},
"headers": {}
},
"x-explorer-enabled": true,
"x-samples-enabled": true,
"x-samples-languages": ["curl", "node", "ruby", "javascript", "python"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"swagger": "2.0",
"info": {
"title": "example results",
"version": "1.0"
},
"host": "example.com",
"paths": {
"/results": {
"get": {
"description": "",
"responses": {
"200": {
"description": "OK",
"examples": {
"application/json": {
"user": {
"email": "test@example.com",
"name": "Test user name"
}
}
}
},
"400": {
"description": "Validation failed",
"examples": {
"application/json": {
"email": "invalid email",
"name": "name already taken"
}
}
}
},
"summary": "Update Password",
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1069,5 +1069,8 @@
}
},
"headers": {}
}
},
"x-explorer-enabled": true,
"x-samples-enabled": true,
"x-samples-languages": ["curl", "node", "ruby", "javascript", "python"]
}
60 changes: 32 additions & 28 deletions packages/api-explorer-ui/src/CodeSampleResponse.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class CodeSampleResponse extends React.Component {
allSecurities = operation.prepareSecurity();
} catch (e) {} // eslint-disable-line no-empty

console.log(showCodeResults(operation));

return (
<div
className={classNames('hub-reference-right hub-reference-results tabber-parent', {
Expand Down Expand Up @@ -180,37 +182,39 @@ class CodeSampleResponse extends React.Component {
</div>

<div className="hub-reference-results-examples code-sample">
{showCodeResults(oas, operation).length ? (
{showCodeResults(operation).length ? (
<span>
{/* <ul className="code-samples-tabs hub-reference-results-header">
{showCodeResults(oas, operation).forEach((ele, index) => {
const status = statusCodes(result.status);
const title = result.name ? result.name : status[1];
<ul className="code-samples-tabs hub-reference-results-header">
{showCodeResults(operation).map((result) => {
console.log(result);
return <span dangerouslySetInnerHTML={{__html: result.code}}></span>
// const status = statusCodes(result.status);
// const title = result.name ? result.name : status[1];

<a
className={
index === 0 ? (
'hub-reference-result-header-item tabber-tab selected'
) : (
'hub-reference-result-header-item tabber-tab '
)
}
href="#"
data-tab={index}
>
{result.status ? (
<span className={status[2] === 'success' ? 'httpsuccess' : 'httperror'}>
<i className="fa fa-circle" />
<em>
&nbsp;`${status[0]}`&nbsp;`${title}`
</em>
</span>
) : (
<span>{generateCodeSnippets.getLangName(result.language)}</span>
)}
</a>;
// <a
// className={
// index === 0 ? (
// 'hub-reference-result-header-item tabber-tab selected'
// ) : (
// 'hub-reference-result-header-item tabber-tab '
// )
// }
// href="#"
// data-tab={index}
// >
// {result.status ? (
// <span className={status[2] === 'success' ? 'httpsuccess' : 'httperror'}>
// <i className="fa fa-circle" />
// <em>
// &nbsp;`${status[0]}`&nbsp;`${title}`
// </em>
// </span>
// ) : (
// <span>{generateCodeSnippets.getLangName(result.language)}</span>
// )}
// </a>;
})}
</ul> */}
</ul>
<div className="code-sample-body">
{showCodeResults(oas, operation).forEach((ele, index) => {
// <pre
Expand Down
16 changes: 8 additions & 8 deletions packages/api-explorer-ui/src/lib/create-code-shower.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ module.exports = type => {
// Only examples so far...
Object.keys(pathOperation.responses || {}).forEach(status => {
const response = pathOperation.responses[status];
const lang = Object.keys(response.content)[0];
const example = response.content[lang].examples.response.value;

if (response.examples) {
// const lang = Object.keys(response.examples)[0];
// const example = response.examples[lang];
// codes.push({
// code: _.isObject(example) ? JSON.stringify(example, undefined, 2) : example,
// language: lang,
// status,
// });
if (example) {
codes.push({
code: lang === 'application/json' ? JSON.stringify(example, undefined, 2) : example,
language: lang,
status,
});
}
});
}
Expand Down

0 comments on commit 259d050

Please sign in to comment.