From 1d91d4faca93b17fd88280773c5a15c3fb7a8ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Tue, 17 May 2022 15:47:24 +0200 Subject: [PATCH] fix(oas3): support externalValue in example objects (#190) --- .../__snapshots__/operation.test.ts.snap | 36 +++++++++++++++++-- .../__tests__/fixtures/oas3-kitchen-sink.json | 13 +++++++ .../__snapshots__/content.test.ts.snap | 6 ++++ .../transformers/__tests__/content.test.ts | 7 ++++ .../transformers/__tests__/responses.test.ts | 8 +++++ src/oas3/transformers/content.ts | 3 +- src/oas3/transformers/examples.ts | 12 +++++-- 7 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/oas/__tests__/__snapshots__/operation.test.ts.snap b/src/oas/__tests__/__snapshots__/operation.test.ts.snap index 46f19a95..9dcc4f4c 100644 --- a/src/oas/__tests__/__snapshots__/operation.test.ts.snap +++ b/src/oas/__tests__/__snapshots__/operation.test.ts.snap @@ -680,7 +680,23 @@ Array [ "contents": Array [ Object { "encodings": Array [], - "examples": Array [], + "examples": Array [ + Object { + "id": "97acd8b5018d8", + "key": "Dog", + "summary": "A dog example", + "value": Object { + "name": "Leo", + "photoUrls": Array [], + }, + }, + Object { + "externalValue": "https://stoplight.io/pets/cat", + "id": "97acd8b5018d8", + "key": "Cat", + "summary": "A cat example", + }, + ], "id": "954d8d8652606", "mediaType": "application/json", "schema": Object { @@ -941,7 +957,23 @@ Array [ "contents": Array [ Object { "encodings": Array [], - "examples": Array [], + "examples": Array [ + Object { + "id": "97acd8b5018d8", + "key": "Dog", + "summary": "A dog example", + "value": Object { + "name": "Leo", + "photoUrls": Array [], + }, + }, + Object { + "externalValue": "https://stoplight.io/pets/cat", + "id": "97acd8b5018d8", + "key": "Cat", + "summary": "A cat example", + }, + ], "id": "954d8d8652606", "mediaType": "application/json", "schema": Object { diff --git a/src/oas/__tests__/fixtures/oas3-kitchen-sink.json b/src/oas/__tests__/fixtures/oas3-kitchen-sink.json index d8903026..3bb65896 100644 --- a/src/oas/__tests__/fixtures/oas3-kitchen-sink.json +++ b/src/oas/__tests__/fixtures/oas3-kitchen-sink.json @@ -189,6 +189,19 @@ "application/json": { "schema": { "$ref": "#/components/schemas/Pet" + }, + "examples": { + "Dog": { + "summary": "A dog example", + "value": { + "name": "Leo", + "photoUrls": [] + } + }, + "Cat": { + "summary": "A cat example", + "externalValue": "https://stoplight.io/pets/cat" + } } }, "application/xml": { diff --git a/src/oas3/transformers/__tests__/__snapshots__/content.test.ts.snap b/src/oas3/transformers/__tests__/__snapshots__/content.test.ts.snap index 29675d0d..88330251 100644 --- a/src/oas3/transformers/__tests__/__snapshots__/content.test.ts.snap +++ b/src/oas3/transformers/__tests__/__snapshots__/content.test.ts.snap @@ -15,6 +15,12 @@ Object { "summary": "example summary", "value": "hey", }, + Object { + "externalValue": "https://stoplight.io/b", + "id": Any, + "key": "b", + "summary": "example summary", + }, Object { "id": Any, "key": "__default", diff --git a/src/oas3/transformers/__tests__/content.test.ts b/src/oas3/transformers/__tests__/content.test.ts index 58d00e6c..958bbb29 100644 --- a/src/oas3/transformers/__tests__/content.test.ts +++ b/src/oas3/transformers/__tests__/content.test.ts @@ -459,6 +459,10 @@ describe('translateHeaderObject', () => { summary: 'example summary', value: 'hey', }, + b: { + summary: 'example summary', + externalValue: 'https://stoplight.io/b', + }, }, example: {}, content: {}, @@ -474,6 +478,9 @@ describe('translateHeaderObject', () => { { id: expect.any(String), }, + { + id: expect.any(String), + }, ], schema: { 'x-stoplight': { diff --git a/src/oas3/transformers/__tests__/responses.test.ts b/src/oas3/transformers/__tests__/responses.test.ts index 0c7f0ba2..707152a7 100644 --- a/src/oas3/transformers/__tests__/responses.test.ts +++ b/src/oas3/transformers/__tests__/responses.test.ts @@ -173,6 +173,9 @@ describe('translateToOas3Responses', () => { 'my-example': { $ref: '#/components/examples/Joe', }, + Bear: { + externalValue: 'https://stoplight.io/bear', + }, }, }, }, @@ -219,6 +222,11 @@ describe('translateToOas3Responses', () => { id: 1, }, }, + { + id: expect.any(String), + key: 'Bear', + externalValue: 'https://stoplight.io/bear', + }, ], mediaType: 'application/json', schema: { diff --git a/src/oas3/transformers/content.ts b/src/oas3/transformers/content.ts index da16f366..9a342e60 100644 --- a/src/oas3/transformers/content.ts +++ b/src/oas3/transformers/content.ts @@ -5,6 +5,7 @@ import { IHttpHeaderParam, IMediaTypeContent, INodeExample, + INodeExternalExample, Optional, } from '@stoplight/types'; import type { JSONSchema7 } from 'json-schema'; @@ -124,7 +125,7 @@ export const translateHeaderObject = withContext< ), }; - const examples: INodeExample[] = []; + const examples: (INodeExample | INodeExternalExample)[] = []; const encodings: IHttpEncoding[] = []; if (isPlainObject(contentValue)) { diff --git a/src/oas3/transformers/examples.ts b/src/oas3/transformers/examples.ts index 5bfb8032..4dd543f4 100644 --- a/src/oas3/transformers/examples.ts +++ b/src/oas3/transformers/examples.ts @@ -1,5 +1,5 @@ import { isPlainObject } from '@stoplight/json'; -import { INodeExample, Optional } from '@stoplight/types'; +import { INodeExample, INodeExternalExample, Optional } from '@stoplight/types'; import pickBy = require('lodash.pickby'); import { withContext } from '../../context'; @@ -9,7 +9,10 @@ import type { ArrayCallbackParameters } from '../../types'; import type { Oas3TranslateFunction } from '../types'; export const translateToExample = withContext< - Oas3TranslateFunction, Optional> + Oas3TranslateFunction< + ArrayCallbackParameters<[key: string, example: unknown]>, + Optional + > >(function ([key, example]) { const resolvedExample = this.maybeResolveLocalRef(example); @@ -19,9 +22,12 @@ export const translateToExample = withContext< return { id: this.generateId(`example-${this.parentId}-${actualKey}`), - value: resolvedExample.value, key, + ...(typeof resolvedExample.externalValue === 'string' + ? { externalValue: resolvedExample.externalValue } + : { value: resolvedExample.value }), + ...pickBy( { summary: resolvedExample.summary,