Skip to content

Commit 0bda160

Browse files
feat: support for dictionaries (#218)
* 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>
1 parent 34a6a82 commit 0bda160

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

test/transforms/components/index.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,4 +865,48 @@ describe('parseComponents method', () => {
865865
const result = parseComponents({}, parsedJSDocs);
866866
expect(result).toEqual(expected);
867867
});
868+
869+
it('Should parse jsdoc component spec dictionary', () => {
870+
const jsodInput = [`
871+
/**
872+
* Profile
873+
* @typedef {object} Profile
874+
*
875+
* @property {string} email
876+
*/
877+
`,
878+
`
879+
/**
880+
* Profiles dict
881+
* @typedef {Dictionary<Profile>} Profiles
882+
*/
883+
`];
884+
const expected = {
885+
components: {
886+
schemas: {
887+
Profile: {
888+
type: 'object',
889+
description: 'Profile',
890+
properties: {
891+
email: {
892+
type: 'string',
893+
description: '',
894+
},
895+
},
896+
},
897+
Profiles: {
898+
type: 'object',
899+
description: 'Profiles dict',
900+
properties: {},
901+
additionalProperties: {
902+
$ref: '#/components/schemas/Profile',
903+
},
904+
},
905+
},
906+
},
907+
};
908+
const parsedJSDocs = jsdocInfo()(jsodInput);
909+
const result = parseComponents({}, parsedJSDocs);
910+
expect(result).toEqual(expected);
911+
});
868912
});

transforms/components/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ const getRequiredProperties = properties => (
6464

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

67+
const addDictionaryAdditionalProperties = typedef => {
68+
if (!typedef.type.expression || typedef.type.expression.name !== 'Dictionary') {
69+
return {};
70+
}
71+
72+
return {
73+
additionalProperties: {
74+
$ref: `#/components/schemas/${typedef.type.applications[0].name}`,
75+
},
76+
};
77+
};
78+
6779
const parseSchema = (schema, options = {}) => {
6880
const typedef = getTagInfo(schema.tags, 'typedef');
6981
const propertyValues = getTagsInfo(schema.tags, 'property');
@@ -88,6 +100,7 @@ const parseSchema = (schema, options = {}) => {
88100
} : {}),
89101
...(format ? { format } : {}),
90102
...addEnumValues(enumValues),
103+
...addDictionaryAdditionalProperties(typedef),
91104
...(jsonOptions || {}),
92105
},
93106
};

0 commit comments

Comments
 (0)