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

Extended resolveType to better handle TypeLiteral and String TypeReference typeNodes #172

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/metadataGeneration/resolveType.ts
Expand Up @@ -57,6 +57,10 @@ export function resolveType(typeNode: ts.TypeNode, parentNode?: ts.Node, extract
return { dataType: 'any' } as Tsoa.Type;
}

if (typeNode.kind === ts.SyntaxKind.TypeLiteral) {
return { dataType: 'any' } as Tsoa.Type;
}

if (typeNode.kind !== ts.SyntaxKind.TypeReference) {
throw new GenerateMetadataError(`Unknown type: ${ts.SyntaxKind[typeNode.kind]}`);
}
Expand All @@ -81,6 +85,10 @@ export function resolveType(typeNode: ts.TypeNode, parentNode?: ts.Node, extract
if (typeReference.typeName.text === 'Promise' && typeReference.typeArguments && typeReference.typeArguments.length === 1) {
return resolveType(typeReference.typeArguments[0]);
}

if (typeReference.typeName.text === 'String') {
return { dataType: 'string' } as Tsoa.Type;
}
}

if (!extractEnum) {
Expand Down
27 changes: 24 additions & 3 deletions tests/fixtures/custom/routes.ts
Expand Up @@ -114,7 +114,9 @@ const models: TsoaRoute.Models={
"defaultValue2": { "dataType": "string", "default": "Default Value 2" },
"publicStringProperty": { "dataType": "string", "required": true, "validators": { "minLength": { "value": 3 }, "maxLength": { "value": 20 }, "pattern": { "value": "^[a-zA-Z]+$" } } },
"optionalPublicStringProperty": { "dataType": "string", "validators": { "minLength": { "value": 0 }, "maxLength": { "value": 10 } } },
"emailPattern": { "dataType": "string", "validators": { "pattern": { "value": "^[a-zA-Z0-9_.+-]+" } } },
"stringProperty": { "dataType": "string", "required": true },
"typeLiterals": { "dataType": "any", "default": { "booleanTypeLiteral": {}, "numberTypeLiteral": {}, "stringTypeLiteral": {} } },
"publicConstructorVar": { "dataType": "string", "required": true },
"optionalPublicConstructorVar": { "dataType": "string" },
"id": { "dataType": "double", "required": true },
Expand Down Expand Up @@ -1263,6 +1265,25 @@ export function RegisterRoutes(app: any) {
const promise=controller.queryAnyType.apply(controller, validatedArgs);
promiseHandler(controller, promise, response, next);
});
app.post('/v1/ParameterTest/ParamaterQueyArray',
function(request: any, response: any, next: any) {
const args={
name: { "in": "query", "name": "name", "required": true, "dataType": "array", "array": { "dataType": "string" } },
};

let validatedArgs: any[]=[];
try {
validatedArgs=getValidatedArgs(args, request);
} catch (err) {
return next(err);
}

const controller=new ParameterController();


const promise=controller.queyArray.apply(controller, validatedArgs);
promiseHandler(controller, promise, response, next);
});
app.post('/v1/ParameterTest/ParamaterBodyAnyType',
function(request: any, response: any, next: any) {
const args={
Expand All @@ -1282,10 +1303,10 @@ export function RegisterRoutes(app: any) {
const promise=controller.bodyAnyType.apply(controller, validatedArgs);
promiseHandler(controller, promise, response, next);
});
app.post('/v1/ParameterTest/ParamaterQueyArray',
app.post('/v1/ParameterTest/ParamaterBodyArrayType',
function(request: any, response: any, next: any) {
const args={
name: { "in": "query", "name": "name", "required": true, "dataType": "array", "array": { "dataType": "string" } },
body: { "in": "body", "name": "body", "required": true, "dataType": "array", "array": { "ref": "ParameterTestModel" } },
};

let validatedArgs: any[]=[];
Expand All @@ -1298,7 +1319,7 @@ export function RegisterRoutes(app: any) {
const controller=new ParameterController();


const promise=controller.queyArray.apply(controller, validatedArgs);
const promise=controller.bodyArrayType.apply(controller, validatedArgs);
promiseHandler(controller, promise, response, next);
});
app.get('/v1/ParameterTest/ParamaterImplicitString',
Expand Down
27 changes: 24 additions & 3 deletions tests/fixtures/express/routes.ts
Expand Up @@ -116,7 +116,9 @@ const models: TsoaRoute.Models={
"defaultValue2": { "dataType": "string", "default": "Default Value 2" },
"publicStringProperty": { "dataType": "string", "required": true, "validators": { "minLength": { "value": 3 }, "maxLength": { "value": 20 }, "pattern": { "value": "^[a-zA-Z]+$" } } },
"optionalPublicStringProperty": { "dataType": "string", "validators": { "minLength": { "value": 0 }, "maxLength": { "value": 10 } } },
"emailPattern": { "dataType": "string", "validators": { "pattern": { "value": "^[a-zA-Z0-9_.+-]+" } } },
"stringProperty": { "dataType": "string", "required": true },
"typeLiterals": { "dataType": "any", "default": { "booleanTypeLiteral": {}, "numberTypeLiteral": {}, "stringTypeLiteral": {} } },
"publicConstructorVar": { "dataType": "string", "required": true },
"optionalPublicConstructorVar": { "dataType": "string" },
"id": { "dataType": "double", "required": true },
Expand Down Expand Up @@ -1309,6 +1311,25 @@ export function RegisterRoutes(app: any) {
const promise=controller.queryAnyType.apply(controller, validatedArgs);
promiseHandler(controller, promise, response, next);
});
app.post('/v1/ParameterTest/ParamaterQueyArray',
function(request: any, response: any, next: any) {
const args={
name: { "in": "query", "name": "name", "required": true, "dataType": "array", "array": { "dataType": "string" } },
};

let validatedArgs: any[]=[];
try {
validatedArgs=getValidatedArgs(args, request);
} catch (err) {
return next(err);
}

const controller=new ParameterController();


const promise=controller.queyArray.apply(controller, validatedArgs);
promiseHandler(controller, promise, response, next);
});
app.post('/v1/ParameterTest/ParamaterBodyAnyType',
function(request: any, response: any, next: any) {
const args={
Expand All @@ -1328,10 +1349,10 @@ export function RegisterRoutes(app: any) {
const promise=controller.bodyAnyType.apply(controller, validatedArgs);
promiseHandler(controller, promise, response, next);
});
app.post('/v1/ParameterTest/ParamaterQueyArray',
app.post('/v1/ParameterTest/ParamaterBodyArrayType',
function(request: any, response: any, next: any) {
const args={
name: { "in": "query", "name": "name", "required": true, "dataType": "array", "array": { "dataType": "string" } },
body: { "in": "body", "name": "body", "required": true, "dataType": "array", "array": { "ref": "ParameterTestModel" } },
};

let validatedArgs: any[]=[];
Expand All @@ -1344,7 +1365,7 @@ export function RegisterRoutes(app: any) {
const controller=new ParameterController();


const promise=controller.queyArray.apply(controller, validatedArgs);
const promise=controller.bodyArrayType.apply(controller, validatedArgs);
promiseHandler(controller, promise, response, next);
});
app.get('/v1/ParameterTest/ParamaterImplicitString',
Expand Down