Skip to content

Commit

Permalink
feat: support for dictionaries (#218)
Browse files Browse the repository at this point in the history
* feat: support for dictionaries

* fix: node 10 compatibility

* feat: implemented dictionaries with dictionary type name

Co-authored-by: Kevin Julián Martínez Escobar <kevinccbsg@gmail.com>
  • Loading branch information
joelabrahamsson and kevinccbsg authored Jun 22, 2022
1 parent 34a6a82 commit 0bda160
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
44 changes: 44 additions & 0 deletions test/transforms/components/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,4 +865,48 @@ describe('parseComponents method', () => {
const result = parseComponents({}, parsedJSDocs);
expect(result).toEqual(expected);
});

it('Should parse jsdoc component spec dictionary', () => {
const jsodInput = [`
/**
* Profile
* @typedef {object} Profile
*
* @property {string} email
*/
`,
`
/**
* Profiles dict
* @typedef {Dictionary<Profile>} Profiles
*/
`];
const expected = {
components: {
schemas: {
Profile: {
type: 'object',
description: 'Profile',
properties: {
email: {
type: 'string',
description: '',
},
},
},
Profiles: {
type: 'object',
description: 'Profiles dict',
properties: {},
additionalProperties: {
$ref: '#/components/schemas/Profile',
},
},
},
},
};
const parsedJSDocs = jsdocInfo()(jsodInput);
const result = parseComponents({}, parsedJSDocs);
expect(result).toEqual(expected);
});
});
13 changes: 13 additions & 0 deletions transforms/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ const getRequiredProperties = properties => (

const formatRequiredProperties = requiredProperties => requiredProperties.map(getPropertyName);

const addDictionaryAdditionalProperties = typedef => {
if (!typedef.type.expression || typedef.type.expression.name !== 'Dictionary') {
return {};
}

return {
additionalProperties: {
$ref: `#/components/schemas/${typedef.type.applications[0].name}`,
},
};
};

const parseSchema = (schema, options = {}) => {
const typedef = getTagInfo(schema.tags, 'typedef');
const propertyValues = getTagsInfo(schema.tags, 'property');
Expand All @@ -88,6 +100,7 @@ const parseSchema = (schema, options = {}) => {
} : {}),
...(format ? { format } : {}),
...addEnumValues(enumValues),
...addDictionaryAdditionalProperties(typedef),
...(jsonOptions || {}),
},
};
Expand Down

0 comments on commit 0bda160

Please sign in to comment.