Skip to content

Commit

Permalink
feat: add support for dictionary primitives (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstaroselsky authored Jul 27, 2022
1 parent 0a5af46 commit faaab1e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
26 changes: 26 additions & 0 deletions test/transforms/components/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,30 @@ describe('parseComponents method', () => {
const result = parseComponents({}, parsedJSDocs);
expect(result).toEqual(expected);
});

it('Should parse jsdoc component spec record type', () => {
const jsodInput = [`
/**
* Records dict
* @typedef {Dictionary<string>} Records map
*/
`];
const expected = {
components: {
schemas: {
Records: {
type: 'object',
description: 'Records dict',
properties: {},
additionalProperties: {
type: 'string',
},
},
},
},
};
const parsedJSDocs = jsdocInfo()(jsodInput);
const result = parseComponents({}, parsedJSDocs);
expect(result).toEqual(expected);
});
});
21 changes: 14 additions & 7 deletions transforms/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { refSchema, formatRefSchema } = require('../utils/refSchema');
const addEnumValues = require('../utils/enumValues');
const formatDescription = require('../utils/formatDescription');
const combineSchema = require('../utils/combineSchema');
const validateTypes = require('../utils/validateTypes');

const REQUIRED = 'required';

Expand Down Expand Up @@ -65,15 +66,21 @@ const getRequiredProperties = properties => (
const formatRequiredProperties = requiredProperties => requiredProperties.map(getPropertyName);

const addDictionaryAdditionalProperties = typedef => {
if (!typedef.type.expression || typedef.type.expression.name !== 'Dictionary') {
return {};
if (
typedef.type.expression
&& typedef.type.expression.name === 'Dictionary'
) {
const typeName = typedef.type.applications[0].name;
const isPrimitive = validateTypes(typeName);

return {
additionalProperties: {
...(isPrimitive ? { type: typeName } : { $ref: `#/components/schemas/${typeName}` }),
},
};
}

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

const parseSchema = (schema, options = {}) => {
Expand Down

0 comments on commit faaab1e

Please sign in to comment.