diff --git a/src/oas2/transformers/__tests__/getExamplesFromSchema.test.ts b/src/oas2/transformers/__tests__/getExamplesFromSchema.test.ts new file mode 100644 index 00000000..ceee05df --- /dev/null +++ b/src/oas2/transformers/__tests__/getExamplesFromSchema.test.ts @@ -0,0 +1,30 @@ +import { getExamplesFromSchema } from '../getExamplesFromSchema'; + +describe('getExamplesFromSchema', () => { + it('should ignore invalid data', () => { + // @ts-ignore + expect(getExamplesFromSchema(null)).toEqual({}); + }); + + it('should work with x-examples', () => { + expect( + getExamplesFromSchema({ + 'x-examples': { + 'my-example': {}, + }, + }), + ).toEqual({ + 'my-example': {}, + }); + }); + + it('should work with example', () => { + expect( + getExamplesFromSchema({ + example: 'my-example', + }), + ).toEqual({ + default: 'my-example', + }); + }); +}); diff --git a/src/oas2/transformers/getExamplesFromSchema.ts b/src/oas2/transformers/getExamplesFromSchema.ts index 0bad96b9..b5018f05 100644 --- a/src/oas2/transformers/getExamplesFromSchema.ts +++ b/src/oas2/transformers/getExamplesFromSchema.ts @@ -3,6 +3,8 @@ import { isObject } from 'lodash'; import { Schema } from 'swagger-schema-official'; export function getExamplesFromSchema(data: Schema & { 'x-examples'?: Dictionary }): Dictionary { + if (!isObject(data)) return {}; + return { ...('x-examples' in data && isObject(data['x-examples']) && { ...data['x-examples'] }), ...('example' in data && { default: data.example }),