Skip to content

Commit

Permalink
Allow combining schemas on typedef (#78)
Browse files Browse the repository at this point in the history
* feat: add combineSchema to parseSchema

* test: add test for combineSchema in parseSchema

Co-authored-by: Heungyeon Oh <heungyeon.oh@bagelcode.com>
  • Loading branch information
hoonga and Heungyeon Oh authored Jul 31, 2020
1 parent 86e7d11 commit b63057a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
79 changes: 79 additions & 0 deletions test/transforms/components/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,83 @@ describe('parseComponents method', () => {
const result = parseComponents({}, parsedJSDocs);
expect(result).toEqual(expected);
});

it('Should parse a SingleAlbum schema with allOf reference of Song.', () => {
const jsodInput = [`
/**
* SingleAlbum
* @typedef {allOf|Song} SingleAlbum
* @property {array<Song>} Songs
*/
`];
const expected = {
components: {
schemas: {
SingleAlbum: {
allOf: [
{
$ref: '#/components/schemas/Song',
},
],
type: 'object',
required: [],
description: 'SingleAlbum',
properties: {
Songs: {
type: 'array',
description: '',
items: {
$ref: '#/components/schemas/Song',
},
},
},
},
},
},
};
const parsedJSDocs = jsdocInfo()(jsodInput);
const result = parseComponents({}, parsedJSDocs);
expect(result).toEqual(expected);
});

it('Should parse a SongOrAlbum schema with oneOf reference of Song and Album.', () => {
const jsodInput = [`
/**
* SongOrAlbum
* @typedef {oneOf|Song|Album} SongOrAlbum
* @property {array<Song>} Songs
*/
`];
const expected = {
components: {
schemas: {
SongOrAlbum: {
oneOf: [
{
$ref: '#/components/schemas/Song',
},
{
$ref: '#/components/schemas/Album',
},
],
type: 'object',
required: [],
description: 'SongOrAlbum',
properties: {
Songs: {
type: 'array',
description: '',
items: {
$ref: '#/components/schemas/Song',
},
},
},
},
},
},
};
const parsedJSDocs = jsdocInfo()(jsodInput);
const result = parseComponents({}, parsedJSDocs);
expect(result).toEqual(expected);
});
});
4 changes: 4 additions & 0 deletions transforms/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ const parseSchema = schema => {
const typedef = getTagInfo(schema.tags, 'typedef');
const propertyValues = getTagsInfo(schema.tags, 'property');
if (!typedef || !typedef.name) return {};
const {
elements,
} = typedef.type;
return {
[typedef.name]: {
...combineSchema(elements),
description: schema.description,
required: isPropertyRequired(propertyValues),
type: 'object',
Expand Down

0 comments on commit b63057a

Please sign in to comment.