Coder Social home page Coder Social logo

Support enums about tsoa HOT 21 CLOSED

lukeautry avatar lukeautry commented on April 28, 2024
Support enums

from tsoa.

Comments (21)

lukeautry avatar lukeautry commented on April 28, 2024 2

I think the string representation that you've proposed is ideal. The number
representation is basically meaningless from a human readability standpoint.
On Jul 24, 2016 9:14 PM, "Dave Leaver" [email protected] wrote:

For my uses I'd like to declare an enum and have the field be the string
values

enum ResultType {
Success,
FailureNoPermission
}

Should result in swagger like:

"type": "string","enum": [ "Success", "FailureNoPermission" ],

Not sure if other people would like the numeric values.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#6 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AIXUACHjNUQlYCqctcorqtKXO6XZKti9ks5qZA3sgaJpZM4JTt7o
.

from tsoa.

egandro avatar egandro commented on April 28, 2024 2

You can try using this:

export type CaseEnum = "Unchanged" | "Capitalized" | "FirstLetterUppercase" | "CamelCase" | "PascalCase" | "Lower" | "Upper";

export const CaseEnum = {
Unchanged: "Unchanged" as CaseEnum,
Capitalized: "Capitalized" as CaseEnum,
FirstLetterUppercase: "FirstLetterUppercase" as CaseEnum,
CamelCase: "CamelCase" as CaseEnum,
PascalCase: "PascalCase" as CaseEnum,
Lower: "Lower" as CaseEnum,
Upper: "Upper" as CaseEnum
};

It feels like an enum - but it's actually a class/string

from tsoa.

danzel avatar danzel commented on April 28, 2024

For my uses I'd like to declare an enum and have the field be the string values

enum ResultType {
    Success,
    FailureNoPermission
}

Should result in swagger like:

"type": "string",
"enum": [ "Success", "FailureNoPermission" ],

Not sure if other people would like the numeric values.

from tsoa.

lukeautry avatar lukeautry commented on April 28, 2024

@danzel Just wanted to provide an update here; enum support hasn't been straightforward, at least for the routing side of things. Generating the correct swagger spec is a straightforward, but wiring up the autogenerated routes to 1) convert strings to their enum values and 2) validate enum values takes a bit more work. Hope to have something in the next week or so.

from tsoa.

danzel avatar danzel commented on April 28, 2024

Thanks, no hurry the string work around is fine :)

from tsoa.

maxcan avatar maxcan commented on April 28, 2024

@lukeautry would it be easier to support string literal types instead of enums?

for example:

type Auth = "Facebook" | "Google" 

from tsoa.

lukeautry avatar lukeautry commented on April 28, 2024

@maxcan Yeah, I have a branch going to implement that exact thing; the issue you opened #39 is caused by the inability to parse a type alias/string literal type.

Feel free to take a stab at it though, it'll probably be next week sometime before it can be completed.

from tsoa.

maxcan avatar maxcan commented on April 28, 2024

from tsoa.

lukeautry avatar lukeautry commented on April 28, 2024

from tsoa.

maxcan avatar maxcan commented on April 28, 2024

@lukeautry is it worth using it as a starting point? If not, I can start off master, just tell me where to look in the code

from tsoa.

adhamhf avatar adhamhf commented on April 28, 2024

hey @lukeautry I'm only interested in swagger support... any chance you could share the code that gets enums to work with swagger? I could then use it in a fork.

from tsoa.

alexey-pelykh avatar alexey-pelykh commented on April 28, 2024

@maxcan do you plan to add support for using string literal types as @path arguments?

from tsoa.

maxcan avatar maxcan commented on April 28, 2024

@alexey-pelykh I'm actually not using tsoa at the moment - jumped over to the graphql dark side so I won't be making any more changes. Happy to give some tips if you want to take it up though.

Max

from tsoa.

alexey-pelykh avatar alexey-pelykh commented on April 28, 2024

@maxcan yeah, I have no choice then :) I've looked through the code more or less, string literal as "pathable" type - just something that was missed out or there was a reason for not doing that?

from tsoa.

maxcan avatar maxcan commented on April 28, 2024

what do you mean by "pathable"?

from tsoa.

alexey-pelykh avatar alexey-pelykh commented on April 28, 2024

union (string literals) fails over here https://github.com/lukeautry/tsoa/blob/master/src/metadataGeneration/parameterGenerator.ts#L128
So it's impossible to use type test = 'a' | 'b'; in Header, Query or Path parameters. I do hope to address that soon

from tsoa.

maxcan avatar maxcan commented on April 28, 2024

ah ok, I see what you mean. no, I never added support for that. I imagine i shouldn't be too difficult though, just not on my radar. maybe use the PR above as a guide?

from tsoa.

alexey-pelykh avatar alexey-pelykh commented on April 28, 2024

@lukeautry I'd love to submit a PR regarding enums-as-union-types and alias types, yet it bases on #77 and #75 so I'd like to ask to take a look on those

from tsoa.

j-ibarra avatar j-ibarra commented on April 28, 2024

Hello, any update for this support, I'm actually trying to add type and enumeration to the response body, but it fails in TypeResolver, the enumeration name (Merchant Status) is not the same as SafeArray

export enum MerchantStatus {
  active = 'active',
  pending = 'pending',
}

export class User {
  uid: string;
  merchantStatus: MerchantStatus;
}

Unhandled rejection Error: No matching model found for referenced type MerchantStatus. If MerchantStatus comes from a dependency, please create an interface in your own code that has the same structure. Tsoa can not utilize interfaces from external dependencies. Read more athttps://github.com/lukeautry/tsoa/blob/master/docs/ExternalInterfacesExplanation.MD

image

return (modelTypeDeclaration.name as ts.Identifier).text === typeName;

in this block filter its empty because

the typename no is SafeArray

image

from tsoa.

WoH avatar WoH commented on April 28, 2024

Could you tell me if this issue still applies to the v3 beta releases?

from tsoa.

j-ibarra avatar j-ibarra commented on April 28, 2024

Could you tell me if this issue still applies to the v3 beta releases?

I just upgraded to version 3.0.4, and the problem has been solved thanks very much

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.