Coder Social home page Coder Social logo

Unknown type: UnionType about tsoa HOT 6 CLOSED

lukeautry avatar lukeautry commented on April 28, 2024
Unknown type: UnionType

from tsoa.

Comments (6)

lukeautry avatar lukeautry commented on April 28, 2024

Good find, should be able to look at this today.
On Jul 23, 2016 9:59 PM, "Dave Leaver" [email protected] wrote:

Make a controller with a return result that includes a string union

interface Result {
value: 'success' | 'failure';
}

Then try generate routes with tsoa.
You get the error: Unknown type: UnionType


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#5, or mute the thread
https://github.com/notifications/unsubscribe-auth/AIXUAOybc6gxQs-AYL5Mu8PX-h_ZHcWxks5qYsbpgaJpZM4JTe-X
.

from tsoa.

lukeautry avatar lukeautry commented on April 28, 2024

Started looking into the implementation for this, but I think there's a challenging limitation of Swagger/OpenAPI we'll have to figure out.

Swagger spec deliberately disallows multiple response types ("union types")

So, the question becomes: what should happen if a referenced model in your API code has a union type property, or the return type of an endpoint itself is a union type?

We can:

  • Throw an error during generation (this is what happens now, and is obviously not great)
  • We could somehow try to find a merged/common type between union types, or merge the type info into a combined type (this seems super confusing to me)
  • We can find some way to essentially represent a union type as dynamic, i.e. no generated swagger definition (not super useful to a consumer, but at least you'd know that that an endpoint returning type "Result" has some value by key 'value'.

I think from an API design standpoint, an enum makes a lot more sense here than the union type string values. That said, there's no support in this library for enums at the moment, although I plan to implement that soon.

from tsoa.

lukeautry avatar lukeautry commented on April 28, 2024

@danzel After further reflection I've decided on option 3 above - see 305dd32

TLDR: union types will be set to type 'object' in swagger generation, which you can think of as 'any' or 'dynamic' type since Swagger/OpenAPI doesn't offer a way to represent this.

Let me know if this doesn't quite meet your expectations or if you have some additional thoughts to share.

from tsoa.

danzel avatar danzel commented on April 28, 2024

You are right that I wanted to use an enum, hadn't actually tried an enum type though.

I'll make an issue for adding that, thanks!

from tsoa.

danzel avatar danzel commented on April 28, 2024

Oh you beat me to it :)

from tsoa.

ThomasKruegl avatar ThomasKruegl commented on April 28, 2024

Actually swagger has a way of expressing "any". It is omitting the "type" property.
So in a schema instead of

"values": {
          "type": "object",
           "properties": {}
          "description": "descr"
        }

you can have just:

"values": {
          "description": "descr"
        }

Having object as type has another meaning, since it says the type will be an object - and not a string, number, boolean..
I suggest removing the "type" property completely for any, and also for union types. Though this definitely requires more testing, i am not sure if omitting the type is allowed in all cases.

regards,
Thomas

from tsoa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.