Skip to content

Commit

Permalink
fix: handle invalid schema data (#106)
Browse files Browse the repository at this point in the history
* fix: handle invalid schema data

* fix: lint
  • Loading branch information
lottamus committed Jul 11, 2020
1 parent 171ccc8 commit 6307aee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/oas2/transformers/__tests__/getExamplesFromSchema.test.ts
Original file line number Diff line number Diff line change
@@ -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',
});
});
});
2 changes: 2 additions & 0 deletions src/oas2/transformers/getExamplesFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { isObject } from 'lodash';
import { Schema } from 'swagger-schema-official';

export function getExamplesFromSchema(data: Schema & { 'x-examples'?: Dictionary<unknown> }): Dictionary<unknown> {
if (!isObject(data)) return {};

return {
...('x-examples' in data && isObject(data['x-examples']) && { ...data['x-examples'] }),
...('example' in data && { default: data.example }),
Expand Down

0 comments on commit 6307aee

Please sign in to comment.