Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for dictionaries #218

Merged
merged 5 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions test/transforms/components/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,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 {Object.<string, Profile>} Profiles
joelabrahamsson marked this conversation as resolved.
Show resolved Hide resolved
*/
`];
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);
});
});
12 changes: 12 additions & 0 deletions transforms/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ const getRequiredProperties = properties => (

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

const addDictionaryAdditionalProperties = typedef => {
if (typedef.type?.applications?.length === 2 && typedef.type.applications[0].name === 'string') {
joelabrahamsson marked this conversation as resolved.
Show resolved Hide resolved
return {
additionalProperties: {
$ref: `#/components/schemas/${typedef.type.applications[1].name}`,
},
};
}
return {};
};

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