From ef18f54ae69b2f4d5fc2e06a7175e57b298bce69 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 13 Apr 2020 13:41:06 -0700 Subject: [PATCH] fix: bug where responses with multiple examples weren't rendered properly (#599) --- .../__tests__/lib/create-code-shower.test.js | 14 ++++---------- .../api-explorer/src/lib/create-code-shower.js | 11 ++++++++++- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/api-explorer/__tests__/lib/create-code-shower.test.js b/packages/api-explorer/__tests__/lib/create-code-shower.test.js index 8ecb3511d..db990f9e5 100644 --- a/packages/api-explorer/__tests__/lib/create-code-shower.test.js +++ b/packages/api-explorer/__tests__/lib/create-code-shower.test.js @@ -103,21 +103,15 @@ describe('createCodeShower', () => { { label: 'cat', code: encodeJsonExample({ - summary: 'An example of a cat', - value: { - name: 'Fluffy', - petType: 'Cat', - }, + name: 'Fluffy', + petType: 'Cat', }), }, { label: 'dog', code: encodeJsonExample({ - summary: "An example of a dog with a cat's name", - value: { - name: 'Puma', - petType: 'Dog', - }, + name: 'Puma', + petType: 'Dog', }), }, ], diff --git a/packages/api-explorer/src/lib/create-code-shower.js b/packages/api-explorer/src/lib/create-code-shower.js index 89686fa05..ce5a811b9 100644 --- a/packages/api-explorer/src/lib/create-code-shower.js +++ b/packages/api-explorer/src/lib/create-code-shower.js @@ -43,9 +43,18 @@ function getMultipleExamples(response, lang) { const { examples } = response.content[lang]; return Object.keys(examples).map(key => { + let example = examples[key]; + if (typeof example === 'object') { + if ('value' in example) { + example = example.value; + } + + example = JSON.stringify(example, undefined, 2); + } + return { label: key, - code: typeof examples[key] === 'object' ? JSON.stringify(examples[key], undefined, 2) : examples[key], + code: example, }; }); }