Skip to content

Commit

Permalink
fix(oas3): support externalValue in example objects (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored May 17, 2022
1 parent 6a63d96 commit 1d91d4f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 6 deletions.
36 changes: 34 additions & 2 deletions src/oas/__tests__/__snapshots__/operation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions src/oas/__tests__/fixtures/oas3-kitchen-sink.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Object {
"summary": "example summary",
"value": "hey",
},
Object {
"externalValue": "https://stoplight.io/b",
"id": Any<String>,
"key": "b",
"summary": "example summary",
},
Object {
"id": Any<String>,
"key": "__default",
Expand Down
7 changes: 7 additions & 0 deletions src/oas3/transformers/__tests__/content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ describe('translateHeaderObject', () => {
summary: 'example summary',
value: 'hey',
},
b: {
summary: 'example summary',
externalValue: 'https://stoplight.io/b',
},
},
example: {},
content: {},
Expand All @@ -474,6 +478,9 @@ describe('translateHeaderObject', () => {
{
id: expect.any(String),
},
{
id: expect.any(String),
},
],
schema: {
'x-stoplight': {
Expand Down
8 changes: 8 additions & 0 deletions src/oas3/transformers/__tests__/responses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ describe('translateToOas3Responses', () => {
'my-example': {
$ref: '#/components/examples/Joe',
},
Bear: {
externalValue: 'https://stoplight.io/bear',
},
},
},
},
Expand Down Expand Up @@ -219,6 +222,11 @@ describe('translateToOas3Responses', () => {
id: 1,
},
},
{
id: expect.any(String),
key: 'Bear',
externalValue: 'https://stoplight.io/bear',
},
],
mediaType: 'application/json',
schema: {
Expand Down
3 changes: 2 additions & 1 deletion src/oas3/transformers/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
IHttpHeaderParam,
IMediaTypeContent,
INodeExample,
INodeExternalExample,
Optional,
} from '@stoplight/types';
import type { JSONSchema7 } from 'json-schema';
Expand Down Expand Up @@ -124,7 +125,7 @@ export const translateHeaderObject = withContext<
),
};

const examples: INodeExample[] = [];
const examples: (INodeExample | INodeExternalExample)[] = [];
const encodings: IHttpEncoding[] = [];

if (isPlainObject(contentValue)) {
Expand Down
12 changes: 9 additions & 3 deletions src/oas3/transformers/examples.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,7 +9,10 @@ import type { ArrayCallbackParameters } from '../../types';
import type { Oas3TranslateFunction } from '../types';

export const translateToExample = withContext<
Oas3TranslateFunction<ArrayCallbackParameters<[key: string, example: unknown]>, Optional<INodeExample>>
Oas3TranslateFunction<
ArrayCallbackParameters<[key: string, example: unknown]>,
Optional<INodeExample | INodeExternalExample>
>
>(function ([key, example]) {
const resolvedExample = this.maybeResolveLocalRef(example);

Expand All @@ -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,
Expand Down

0 comments on commit 1d91d4f

Please sign in to comment.