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

Undefined type being passed to resolveTypeArguments in certain cases #122

Open
tnrich opened this issue Jun 16, 2020 · 1 comment
Open

Comments

@tnrich
Copy link
Collaborator

tnrich commented Jun 16, 2020

Hey @thiagobustamante and whoever else maintains this! Thanks again for the awesome tool!

So.. I was generating swagger json using swaggerGen:

Here are some of the types:

import { Errors } from "typescript-rest";

export interface ErrorWithDetails<T> {
  details?: T,
}
export class ConflictErrorWithDetails<T> extends Errors.ConflictError implements ErrorWithDetails<T> {
  details?: T;
  constructor(message?: string, details?: T) {
    super(message);
    this.details = details
  }
}

I then use that here in a @Response tag:

@Response<ConflictErrorWithDetails<DuplicatesFoundResult>>(409, "Duplicate sequences detected", {
  name: "ConflictError",
  message: "Duplicate sequences detected",
  details: {
    ...etc...
  },
  statusCode: 409
})

I was getting a difficult to debug error:


There was a problem resolving type of 'ConflictErrorWithDetailsDuplicatesFoundResult'.
/Users/tnrich/Sites/lims/tg-cli-api/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:235
        throw err;
        ^

TypeError: Cannot read property 'typeParameters' of undefined
    at buildGenericTypeMap (/Users/tnrich/Sites/lims/tg-cli-api/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:581:14)
    at resolveTypeArguments (/Users/tnrich/Sites/lims/tg-cli-api/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:590:18)
    at /Users/tnrich/Sites/lims/tg-cli-api/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:516:34
    at Array.forEach (<anonymous>)
    at /Users/tnrich/Sites/lims/tg-cli-api/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:509:22
    at Array.forEach (<anonymous>)
    at getInheritedProperties (/Users/tnrich/Sites/lims/tg-cli-api/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:505:21)
    at getReferenceType (/Users/tnrich/Sites/lims/tg-cli-api/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:228:34)
    at Object.resolveType (/Users/tnrich/Sites/lims/tg-cli-api/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:76:25)
    at /Users/tnrich/Sites/lims/tg-cli-api/node_modules/typescript-rest-swagger/dist/metadata/endpointGenerator.js:117:37
    at Array.map (<anonymous>)

I dug deeper by debugging in node inspector:

image

As you can see in the above image, the actual issue is that type===undefined we should at the very least return a better error message when type===undefined. I'll put a PR in for that right now.

After understanding the issue better, I was able to fix it by explicitly importing:
import { ConflictError, HttpError } from "typescript-rest/dist/server/model/errors";
instead of
import { Errors } from "typescript-rest";

That can work for right now but seems like an imperfect solution (because ConflictError, HttpError aren't exposed as top level exports in typescript-rest).

It seems like the best solution would be improve the typescript-rest-swagger MetadataGenerator to properly find all the included types coming from a top-level import.

Thanks!

@tnrich
Copy link
Collaborator Author

tnrich commented Jun 17, 2020

@ngraef any thoughts on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant