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

Commit

Permalink
fix: if an OAS has an empty examples object, don't parse it (#600)
Browse files Browse the repository at this point in the history
* fix: if an OAS has an empty examples object, don't parse it

* style: fixing a typo in a test
  • Loading branch information
erunion authored Apr 14, 2020
1 parent 418afe1 commit b0887ad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@
"summary": "Update Password"
}
},
"/emptyexample": {
"get": {
"description": "",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"examples": {}
}
}
}
}
}
},
"/nolang": {
"get": {
"description": ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ describe('createCodeShower', () => {
expect(createCodeShower(operation, oas)).toStrictEqual([]);
});

it('should return early if there is an empty example', () => {
const operation = oas.operation('/emptyexample', 'get');
expect(createCodeShower(operation, oas)).toStrictEqual([]);
});

it('should return early if there is no response', () => {
const operation = oas.operation('/nolang', 'get');
expect(createCodeShower(operation, oas)).toStrictEqual([]);
Expand Down
18 changes: 12 additions & 6 deletions packages/api-explorer/src/lib/create-code-shower.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ function getExample(response, lang) {

let example = examples[0];
example = response.content[lang].examples[example];
if ('value' in example) {
return example.value;
if (example !== null && typeof example === 'object') {
if ('value' in example) {
return example.value;
}
}

return example;
Expand All @@ -42,9 +44,9 @@ function getMultipleExamples(response, lang) {
if (!response.content[lang].examples || response.content[lang].examples.response) return false;

const { examples } = response.content[lang];
return Object.keys(examples).map(key => {
const multipleExamples = Object.keys(examples).map(key => {
let example = examples[key];
if (typeof example === 'object') {
if (example !== null && typeof example === 'object') {
if ('value' in example) {
example = example.value;
}
Expand All @@ -57,15 +59,19 @@ function getMultipleExamples(response, lang) {
code: example,
};
});

return multipleExamples.length > 0 ? multipleExamples : false;
}

function constructLanguage(language, response, example) {
const multipleExamples = getMultipleExamples(response, language);
if (!example && !multipleExamples) return false;
if (!example && !multipleExamples) {
return false;
}

return {
language,
code: typeof example === 'object' ? JSON.stringify(example, undefined, 2) : example,
code: example !== null && typeof example === 'object' ? JSON.stringify(example, undefined, 2) : example,
multipleExamples: !example ? multipleExamples : false,
};
}
Expand Down

0 comments on commit b0887ad

Please sign in to comment.