Skip to content

Commit 13d694d

Browse files
author
svolkov
committed
fix: problems with using generated swagger schemas from tsoa 3.x
1 parent 13ecfbd commit 13d694d

File tree

10 files changed

+2586
-8
lines changed

10 files changed

+2586
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# 1.2.5
22
Features: better naming of routes without `operationId`
33
![route naming](./assets/changelog_assets/1.2.5_route_naming.jpg)
4+
Changes: rename `@security true` -> `@secure`, `@duplicate true` -> `@duplicate`
5+
Fixes: Support tsoa 3.x usage with complex types
46

57
# 1.2.4
68
Features: add .d.ts file into npm package

src/modelNames.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
const checkAndRenameModelName = name => {
3+
4+
if (!/^([A-Za-z$_]{1,})$/g.test(name)) {
5+
// specific replaces for TSOA 3.x
6+
7+
if (name.includes('.'))
8+
name = name
9+
.replace(/Exclude_keyof[A-Za-z]{1,}/g, match => 'ExcludeKeys')
10+
.replace(/%22\~AND\~%22/g, 'And')
11+
.replace(/%22\~OR\~%22/g, 'Or')
12+
.replace(/(\.?%22)|\./g, '_')
13+
.replace(/__+$/, '');
14+
}
15+
16+
return name;
17+
}
18+
19+
module.exports = {
20+
checkAndRenameModelName
21+
}

src/modelTypes.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const _ = require('lodash');
22
const { formatters } = require("./typeFormatters");
3+
const { checkAndRenameModelName } = require("./modelNames");
34

45
const CONTENT_KEYWORD = '__CONTENT__';
56

@@ -10,12 +11,13 @@ const contentWrapersByTypeIdentifier = {
1011
}
1112

1213
// { typeIdentifier, name, content, type }
13-
const getModelType = ({ typeIdentifier, name, content, type, description }) => {
14+
const getModelType = ({ typeIdentifier, name: originalName, content, type, description }) => {
1415
if (!contentWrapersByTypeIdentifier[typeIdentifier]) {
1516
throw new Error(`${typeIdentifier} - type identifier is unknown for this utility`)
1617
}
1718

1819
const resultContent = formatters[type] ? formatters[type](content) : content;
20+
const name = checkAndRenameModelName(originalName);
1921

2022
return {
2123
typeIdentifier,
@@ -26,7 +28,7 @@ const getModelType = ({ typeIdentifier, name, content, type, description }) => {
2628
}
2729
}
2830

29-
3031
module.exports = {
3132
getModelType,
32-
}
33+
checkAndRenameModelName,
34+
}

src/routes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const _ = require("lodash");
22
const { parseSchema } = require("./schema");
3+
const { checkAndRenameModelName } = require("./modelNames");
34
const { inlineExtraFormatters } = require("./typeFormatters");
45

56
const methodAliases = {
@@ -18,7 +19,8 @@ const getTypeFromRequestInfo = (requestInfo, parsedSchemas, contentType) => {
1819
const extractedSchema = _.get(schema, 'additionalProperties', schema);
1920
const { content } = parseSchema(extractedSchema, 'none', inlineExtraFormatters);
2021
const foundSchema = _.find(parsedSchemas, parsedSchema => _.isEqual(parsedSchema.content, content))
21-
return foundSchema ? foundSchema.name : content;
22+
23+
return foundSchema ? foundSchema.name : checkAndRenameModelName(content);
2224
}
2325

2426
return 'any';

src/schema.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const findSchemaType = schema => {
1515
const typeAliases = {
1616
"integer": "number",
1717
}
18-
const getTypeAlias = type => typeAliases[type] || type || 'any'
18+
const getPrimitiveType = type => typeAliases[type] || type || 'any'
1919

2020
const specificObjectTypes = {
2121
'array': ({ items }) => {
@@ -25,7 +25,7 @@ const specificObjectTypes = {
2525
}
2626
const getRefType = (ref) => _.last(_.split(ref, '/'))
2727
const getType = (property) => {
28-
const func = specificObjectTypes[property.type] || (() => getTypeAlias(property.type))
28+
const func = specificObjectTypes[property.type] || (() => getPrimitiveType(property.type))
2929
return property["$ref"] ? getRefType(property["$ref"]) : func(property)
3030
}
3131
const getObjectTypeContent = (properties) => {
@@ -73,7 +73,7 @@ const getComplexType = (schema) => {
7373

7474
const schemaParsers = {
7575
'enum': (schema, typeName) => {
76-
const type = getTypeAlias(schema.type);
76+
const type = getPrimitiveType(schema.type);
7777
const isIntegerEnum = type === "number";
7878
return {
7979
type: isIntegerEnum ? "intEnum" : 'enum',
@@ -136,4 +136,5 @@ const parseSchema = (schema, typeName, formattersMap) => {
136136
module.exports = {
137137
parseSchema,
138138
getType,
139+
getPrimitiveType,
139140
}

src/typeFormatters.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _ = require('lodash');
2+
const { checkAndRenameModelName } = require("./modelNames");
23

34
const formatters = {
45
'enum': content => _.map(content, ({ key, value }) => ` ${key} = ${value}`).join(',\n'),
@@ -15,7 +16,19 @@ const formatters = {
1516
}
1617

1718
return result;
18-
}).join('')
19+
}).join(''),
20+
'type': content => {
21+
if (content.includes(' & ')) {
22+
return content.split(' & ').map(checkAndRenameModelName).join(' & ')
23+
}
24+
25+
if (content.includes(' | ')) {
26+
return content.split(' | ').map(checkAndRenameModelName).join(' | ')
27+
}
28+
29+
return content
30+
},
31+
'primitive': content =>checkAndRenameModelName(content),
1932
}
2033

2134
const inlineExtraFormatters = {

0 commit comments

Comments
 (0)