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

Commit b0887ad

Browse files
authored
fix: if an OAS has an empty examples object, don't parse it (#600)
* fix: if an OAS has an empty examples object, don't parse it * style: fixing a typo in a test
1 parent 418afe1 commit b0887ad

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

packages/api-explorer/__tests__/__fixtures__/example-results/oas.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,21 @@
180180
"summary": "Update Password"
181181
}
182182
},
183+
"/emptyexample": {
184+
"get": {
185+
"description": "",
186+
"responses": {
187+
"200": {
188+
"description": "OK",
189+
"content": {
190+
"application/json": {
191+
"examples": {}
192+
}
193+
}
194+
}
195+
}
196+
}
197+
},
183198
"/nolang": {
184199
"get": {
185200
"description": ""

packages/api-explorer/__tests__/lib/create-code-shower.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ describe('createCodeShower', () => {
137137
expect(createCodeShower(operation, oas)).toStrictEqual([]);
138138
});
139139

140+
it('should return early if there is an empty example', () => {
141+
const operation = oas.operation('/emptyexample', 'get');
142+
expect(createCodeShower(operation, oas)).toStrictEqual([]);
143+
});
144+
140145
it('should return early if there is no response', () => {
141146
const operation = oas.operation('/nolang', 'get');
142147
expect(createCodeShower(operation, oas)).toStrictEqual([]);

packages/api-explorer/src/lib/create-code-shower.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ function getExample(response, lang) {
3131

3232
let example = examples[0];
3333
example = response.content[lang].examples[example];
34-
if ('value' in example) {
35-
return example.value;
34+
if (example !== null && typeof example === 'object') {
35+
if ('value' in example) {
36+
return example.value;
37+
}
3638
}
3739

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

4446
const { examples } = response.content[lang];
45-
return Object.keys(examples).map(key => {
47+
const multipleExamples = Object.keys(examples).map(key => {
4648
let example = examples[key];
47-
if (typeof example === 'object') {
49+
if (example !== null && typeof example === 'object') {
4850
if ('value' in example) {
4951
example = example.value;
5052
}
@@ -57,15 +59,19 @@ function getMultipleExamples(response, lang) {
5759
code: example,
5860
};
5961
});
62+
63+
return multipleExamples.length > 0 ? multipleExamples : false;
6064
}
6165

6266
function constructLanguage(language, response, example) {
6367
const multipleExamples = getMultipleExamples(response, language);
64-
if (!example && !multipleExamples) return false;
68+
if (!example && !multipleExamples) {
69+
return false;
70+
}
6571

6672
return {
6773
language,
68-
code: typeof example === 'object' ? JSON.stringify(example, undefined, 2) : example,
74+
code: example !== null && typeof example === 'object' ? JSON.stringify(example, undefined, 2) : example,
6975
multipleExamples: !example ? multipleExamples : false,
7076
};
7177
}

0 commit comments

Comments
 (0)