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

Support for discriminated unions #240

Closed
AmazingTurtle opened this issue Jun 22, 2018 · 8 comments
Closed

Support for discriminated unions #240

AmazingTurtle opened this issue Jun 22, 2018 · 8 comments

Comments

@AmazingTurtle
Copy link
Contributor

AmazingTurtle commented Jun 22, 2018

This is an absolute must have feature!

interface Square {
    kind: "square";
    size: number;
}
interface Rectangle {
    kind: "rectangle";
    width: number;
    height: number;
}
interface Circle {
    kind: "circle";
    radius: number;
}

/**
 * @tsoaModel
 */
interface TsoaModel = Square | Rectangle | Circle;

You can't use TsoaModel as @RequestBody or response type.

There was a problem resolving type of 'TsoaModel'.
Generate swagger error.
 TypeError: Cannot read property 'text' of undefined
    at ...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:253:79
    at Array.map (<anonymous>)
    at getLiteralType (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:253:27)
    at resolveType (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:79:23)
    at resolveType (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:26:26)
    at ...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:512:20
    at Array.map (<anonymous>)
    at getModelProperties (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:502:10)
    at getReferenceType (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:275:26)
    at resolveType (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:89:25)
    at ...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:512:20
    at Array.map (<anonymous>)
    at getModelProperties (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:502:10)
    at getReferenceType (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:275:26)
    at resolveType (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:89:25)
    at Object.resolveType (...\node_modules\tsoa\dist\metadataGeneration\resolveType.js:70:20)

http://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions

@strongpauly
Copy link

I've run into this issue today. Is there any planned fix?

@AmazingTurtle
Copy link
Contributor Author

@lukeautry may I ask you for kind support so that I can implement it my own? I'm not familiar with reflection in typescript

@endor
Copy link
Collaborator

endor commented Aug 3, 2018

I'm not really familiar with reflection either nor with the enum implementation in tsoa and why it uses purely strings.

It seems to me though, like a clean implementation would involve extending getLiteralType to check whether the unionNode has a literal property or a typeName property, which seems to contain an object describing a part of the union:

IdentifierObject {
 pos: 459,
 end: 469,
 flags: 0,
 parent: [Circular],
 escapedText: 'Rectangle',
 flowNode: [Object],
 transformFlags: 536870912 }

Then, if there is a typeName property, we should probably introduce a Tsoa.UnionType or Tsoa.DiscriminatedUnionType.

That would then also involve adding a validateUnion method in templateHelpers and getSwaggerTypeForUnion in specGenerator, etc.

Are you up for that @AmazingTurtle? I can also try to help.

@AmazingTurtle
Copy link
Contributor Author

@endor I'm up for it. I would give it a shot then. I'm wondering if it is compliant to OpenAPI specs as well?

@endor
Copy link
Collaborator

endor commented Oct 1, 2018

At least in 3.0.0 it seems to be supported: https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/. Might be a good idea to go forward with the 3.0.0 implementation efforts (:

@adrianplavka
Copy link

@AmazingTurtle @endor Hey, is this still being worked on?

@dgreene1
Copy link
Collaborator

Union types are being tracked under #393 so we’ll close this and track the feature there.

@dgreene1
Copy link
Collaborator

@adrianplavka, @strongpauly, @AmazingTurtle, I'm happy to report that the we added our first draft of unions and intersections in v2.4.11. Please consider upgrading to it and let us know how it works for you. We would appreciate the feedback. :) Please note: you must have config.swagger.specVersion set to 3 for this to work since oneOf and allOf are not supported in Swagger (but they are in OpenAPI 3.0).

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

No branches or pull requests

5 participants