-
Notifications
You must be signed in to change notification settings - Fork 507
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
Support generic type parameter and return type #27
Comments
I think this can be supported and it's a great idea. I'll look into it soon. |
I see that support for this was merged, but if I try to use $ ./node_modules/.bin/tsoa swagger
There was a problem resolving type of 'T'.
There was a problem resolving type of 'ResultListResource'.
Error: No matching model found for referenced type T with interface ResultList<T> {
data: T[],
} version: |
@nenadalm +1 same here |
Just something i came across while fiddling with the above The following doesn't, gives the above error that @nenadalm mentioned: |
+1 on this! I can't make something like
And use as result |
+1, i have the same issue |
@lukeautry Hello, anytning on this? Its been sitting there dormant for a while |
+1, same issue here. This seems a particularly important feature for code maintenance. |
Hello, still have a kind a similar issue : There was a problem resolving type of 'T'.
There was a problem resolving type of 'JsonApiResourceT'.
There was a problem resolving type of 'JsonApiWrapperUserAttributes'.
Generate routes error.
Error: No matching model found for referenced type T.
at new GenerateMetadataError (/Users/thibaut/Documents/poc/jsonapi-serializer/node_modules/tsoa/dist/metadataGeneration/exceptions.js:17:28)
at getModelTypeDeclaration (/Users/thibaut/Documents/poc/jsonapi-serializer/node_modules/tsoa/dist/metadataGeneration/resolveType.js:419:15)
at getReferenceType (/Users/thibaut/Documents/poc/jsonapi-serializer/node_modules/tsoa/dist/metadataGeneration/resolveType.js:280:25)
at resolveType (/Users/thibaut/Documents/poc/jsonapi-serializer/node_modules/tsoa/dist/metadataGeneration/resolveType.js:95:25)
at /Users/thibaut/Documents/poc/jsonapi-serializer/node_modules/tsoa/dist/metadataGeneration/resolveType.js:496:23
at Array.map (<anonymous>)
at getModelProperties (/Users/thibaut/Documents/poc/jsonapi-serializer/node_modules/tsoa/dist/metadataGeneration/resolveType.js:462:14)
at getReferenceType (/Users/thibaut/Documents/poc/jsonapi-serializer/node_modules/tsoa/dist/metadataGeneration/resolveType.js:281:26)
at resolveType (/Users/thibaut/Documents/poc/jsonapi-serializer/node_modules/tsoa/dist/metadataGeneration/resolveType.js:92:25)
at resolveType (/Users/thibaut/Documents/poc/jsonapi-serializer/node_modules/tsoa/dist/metadataGeneration/resolveType.js:69:30) I use tsoa in version 2.1.4. Here is my controller : @Route("users")
export class UsersController extends Controller {
@Get("{id}")
public async getUser(
@Path("id") id: number,
@Query() name?: string
): Promise<JsonApiWrapper<UserAttributes>> {
const user = await UserService.get(id);
return SerializerService.serialize(user);
}
@SuccessResponse("201", "Created")
@Post()
public async createUser(
@Body() requestBody: UserCreationRequest
): Promise<JsonApiWrapper<UserAttributes>> {
const user = await UserService.create(requestBody);
this.setStatus(201); // set return status 201
return SerializerService.serialize(user);
}
} and my Models : User.tsexport interface User extends UserAttributes {
apiId: number;
}
export interface UserAttributes {
email: string;
name: Name;
status?: Status;
phoneNumbers: string[];
}
export type Status = "Happy" | "Sad";
export interface Name {
first: string;
last?: string;
}
export interface UserCreationRequest {
email: string;
name: Name;
phoneNumbers: string[];
} JsonApiWrapper.tsexport interface JsonApiWrapper<T> {
data?:
| JsonApiResource<T>
| Array<JsonApiResource<T>>
| JsonApiResourceIdentifier
| JsonApiResourceIdentifier[]
| null;
error?: JsonApiError[];
meta?: JsonApiMeta;
jsonapi?: any;
links?: JsonApiWrapperLinks;
included?: Array<JsonApiResource<T>>;
} JsonApiError.tsexport interface JsonApiError {
id?: string;
links?: JsonApiErrorLinks;
status?: string;
code?: string;
title?: string;
detail?: string;
source?: JsonApiErrorSource;
}
export interface JsonApiErrorSource {
pointer: string;
parameter: string;
meta: JsonApiMeta;
} JsonApiMeta.tsexport interface JsonApiMeta {
[key: string]: any;
} JsonApiResource.tsexport interface JsonApiResource<T> {
type: string;
id?: string;
attributes?: T;
relationships?: JsonApiRelationships;
links?: JsonApiResourceLinks;
meta?: JsonApiMeta;
}
export interface JsonApiResourceIdentifier {
type: string;
id: string;
meta?: any;
} JsonApiRelationships.tsexport interface JsonApiRelationships {
[key: string]: JsonApiRelationship;
}
export interface JsonApiRelationship {
links: JsonApiLinks;
data: JsonApiResourceIdentifier | JsonApiResourceIdentifier[] | null;
} JsonApiLinks.tsexport interface JsonApiLinks {
self?: string | JsonApiLinkObject;
related?: string | JsonApiLinkObject;
[key: string]: string | JsonApiLinkObject;
}
export interface JsonApiLinkObject {
href: string;
meta: any;
}
export interface JsonApiResourceLinks extends JsonApiLinks {
self: string | JsonApiLinkObject;
related: string | JsonApiLinkObject;
}
export interface JsonApiErrorLinks extends JsonApiLinks {
about: string | JsonApiLinkObject;
}
export interface JsonApiWrapperLinks extends JsonApiLinks {
first?: string | JsonApiLinkObject;
last?: string | JsonApiLinkObject;
prev?: string | JsonApiLinkObject;
next?: string | JsonApiLinkObject;
} any update on this issue ? |
So I tried to replace some generic parts of my interface and the following interface replacing JsonApiWrapper seems to work : JsonApiUser.tsexport interface JsonApiUser {
data?:
| JsonApiResource<UserAttributes>
| Array<JsonApiResource<UserAttributes>>
| JsonApiResourceIdentifier
| JsonApiResourceIdentifier[]
| null;
error?: JsonApiError[];
meta?: JsonApiMeta;
jsonapi?: any;
links?: JsonApiWrapperLinks;
included?: Array<JsonApiResource<UserAttributes>>;
} I think it's the passing on of the generic type to another generic interface that bring on the issue. |
Bump. Any plans to fix this? |
This is definitely a desirable feature but it's some work to implement it handle all of the cases. |
+1 |
Full support for generics was added in v2.4.10. |
@dgreene1 I think this should be re-opened as I'm still seeing the behavior described above. It can be replicated by replacing tsoa/tests/fixtures/testModel.ts Lines 360 to 363 in ac71d13
with export interface GenericRequest<T> {
name: string;
value: T;
nested?: GenericNested<T>;
}
export interface GenericNested<T> {
foo: T;
} and re-running the tests:
|
@ryankeener I know you already confirmed that this works, but I wanted to let you know that this fix/feature was just released in v2.5.1. Can you let us know how it worked out for you? Side note: we're looking for companies that want to be featured in tsoa's readme: #464 |
That's absolutely awesome. I love it! But still have similar issue adding
And I got There was a problem resolving type of 'T'. |
@fhewitt would you be so kind as to make a separate issue for that scenario since the non-extended generic scenarios seem to be resolved? |
Can do that! > #467 |
Hi @WoH @dgreene1, sorry for digging this but I think this issue isn't fully resolved (or maybe I'm doing something incorrectly). import { Get, Route } from "tsoa";
interface ResponseObject<T> {
status: number;
data: T | any;
}
interface PingEntity {
message: string;
}
@Route("ping")
export default class PingController {
@Get()
async getMessage() {
const data: PingEntity = { message: "pong" };
let result: ResponseObject<PingEntity> = { status: 1, data };
return result;
}
} When I try to generate the swagger json it throws this error:
But when I specified the type directly to interface ResponseObject {
status: number;
data: PingEntity;
} Here is the example repo: https://github.com/mrgoonie/express-swagger-typescript Temporary workaround solution: import { Get, Route } from "tsoa";
// no generic type
interface ResponseObject {
status: number;
data: any;
}
interface PingEntity {
message: string;
}
@Route("ping")
export default class PingController {
@Get()
async getMessage() {
const data: PingEntity = { message: "pong" };
// use type combination instead of generic type
let result: ResponseObject & { data: PingEntity } = { status: 1, data };
return result;
}
} Cheers, |
@mrgoonie I had the exact same issue today, the problem is that the return type of @Get()
async getMessage() {
const data: PingEntity = { message: "pong" };
let result: ResponseObject<PingEntity> = { status: 1, data };
return result;
} Should be changed to: @Get()
async getMessage(): Promise<ResponseObject<PingEntity>> {
const data: PingEntity = { message: "pong" };
let result: ResponseObject<PingEntity> = { status: 1, data };
return result;
} |
I don't think it's fully resolved, using a generic that extends a type results in the generic type itself erroring out with |
I spent far too long chasing this similar problem updating the tsoa typeResolver.js helped me track it down: try {
(0, flowUtils_1.throwUnless)(modelTypes.length, new exceptions_1.GenerateMetadataError(`No matching model found for referenced type ${typeName}.`));
} catch(e) {
console.log(type.getSourceFile().fileName)
console.log(type.pos);
throw e
} The filename and offending method really should be logged by TSOA Turns out it was an issue with the Sequelize toJSON method and its generic return type and I had to do something like this: override toJSON(): InferAttributes<TModelClass, TInferAttrsOptions> {
const values = Object.assign( {}, this.get() )
return values
} However it still does not like my generic TModelClass type so I have to explicitly set it to the model class (which I can't as this is base class for my models - or I have to set it on all my models) OR I just set it to This makes me miss working with FastAPI |
Return type:
Current only support return
Promise<XXX>
, when returnPromise<Response<XXX>>
orResponse<XXX>
, cli throw error "No matching model found for referenced type T".Paramter type:
use generic type in parameter can generate definition file, but swagger show parameter's schema is undefined.
Can you support it? thank you.
The text was updated successfully, but these errors were encountered: