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
I've run into this issue today. Is there any planned fix?
@lukeautry may I ask you for kind support so that I can implement it my own? I'm not familiar with reflection in typescript
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.
@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?
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 (:
@AmazingTurtle @endor Hey, is this still being worked on?
Union types are being tracked under https://github.com/lukeautry/tsoa/issues/393 so we鈥檒l close this and track the feature there.
@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).