-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Generate enums with integer values assigned #2690
Comments
@kenjitayama For Java, it will be taken care of by #2508, which adds better support for enum (number, string, special characters) in PHP, C# and Java. For Swift, we want to do it as part of #2508 as well. Currently, we're adding better enum support for JS in that PR. |
Thanks for the quick response! I will be watching the issue 👍 |
@kenjitayama #2508 has been merged into master so it should cover at least Java. Please pull the latest master and give it a try. |
@wing328 I wasn't sure of the supported yaml syntax and I tried the following. definitions:
weather1:
type: object
required:
- type
properties:
type:
type: integer
enum:
- Sunny
- Cloudy
- Rainy
weather2:
type: object
required:
- type
properties:
type:
type: integer
enum:
- 1
- 2
- 3
weather3:
type: object
required:
- type
properties:
type:
type: integer
format: int32
enum:
- 1
- 2
- 3
weather4:
type: object
required:
- type
properties:
type:
type: integer
format: int32
enum:
- Sunny
- Cloudy
- Rainy weather3 produced Java code : public enum TypeEnum {
NUMBER_1(1),
NUMBER_2(2),
NUMBER_3(3);
private Integer value;
TypeEnum(Integer value) {
this.value = value;
}
@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}
} Is there any syntax for having custom names for each enum items? weather3 caused exception for Swift (I guess not supported yet) :
|
For weather1 and weather4, I don't think it's valid as the enum is string but the type is integer. For enum naming convention, is there any reference? The most common one I saw is adding the "Enum" suffix (e.g. StatusEnum). |
I agree. Tried them in case I could find out something.
Looks like I couldn't express my question correctly.
Instead of
? |
Don't think we support that at the moment. Should be mapping be done in the server side, which means the client will send the string "Sunny" and the server will map it to 1 (in database, etc)? |
This was what I was looking for…, but thanks for sharing.
In my case, the client needs to send and retrieve |
@kenjitayama technically we can support it via vendor extension with code changes in how to handle the enum mapping. |
I see that the Swagger specs itself doesn't support this use case.
I saw swagger-codegen supporting a vendor extension before (x-swagger-router-controller) , so I recognize that it's position is not closed for supporting vendor extensions. |
Feel free to suggest here: https://github.com/OAI/OpenAPI-Specification/issues |
2.2.1 has been released with some improvements in enum. Please give it a try. |
I'm currently not using swagger(nor any API description language), but I'm listening to updates :) I'd love to try if this feature gets supported. |
I think for better support (i.e. define name + value together) we need to wait for OpenAPI 3.0. OAI/OpenAPI-Specification#681 seems to have some ideas on how this could be defined. If we have that, implementing it in Codegen seems quite straightforward. |
I have noticed that Enums are not properly generated in the Angular2 TypeScript Client. I will look at the code of that specific generator to see if it is a bug there. |
Did you guys noticed that the json value generated is type string instead of type integer, which is wrong by swagger spec? |
@paranoid945 could you please open a new issue about this? I fear it gets lost in this discussion, which is about supporting names for enum values. |
Is there also a way to have Enums with additional values I have something like this in Java: public enum MimeType {
} |
I've implemented the x-enumNames approach suggested by @kenjitayama that passes the value of the x-enumNames list to the template: x-enumNames Considering that even Version 3 of the Swagger OpenAPI Spec does not support something similar in the near future, I think it may be an option to add this "vendor extension" to swagger-codegen? |
Help! I think my problem is related to this. Here is my enum:
Any I use c.DescribeAllEnumsAsStrings(); But the generated code start with enum 1, instead of 0? Why? C# default start with 0 instead of 1. Is there any way I can specify the start number?? Thanks! |
Is there any plan to support this for Golang? |
It's been a while since this was proposed. Any updates or plans for supporting this? |
We are also facing the same issue for C#. @xiangyucao, did you find any solution for this? |
No, I didn't find anySent from my T-Mobile 4G LTE Device
-------- 原始信息 --------发件人: Naresh Sirvi <[email protected]> 日期: 2020/2/11 上午1:56 (GMT-08:00) 收件人: swagger-api/swagger-codegen <[email protected]> 抄送: Shawn Cao <[email protected]>, Mention <[email protected]> 主题: Re: [swagger-api/swagger-codegen] Generate enums with integer values
assigned (#2690) We are also facing the same issue for C# @xiangyucao have you find any solution for this?
—You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub, or unsubscribe.
[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "#2690?email_source=notifications\u0026email_token=ABYENNK7676TC6USIGOYIELRCJY4TA5CNFSM4CBYZFNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELLZVFY#issuecomment-584555159",
"url": "#2690?email_source=notifications\u0026email_token=ABYENNK7676TC6USIGOYIELRCJY4TA5CNFSM4CBYZFNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELLZVFY#issuecomment-584555159",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]
|
Hi, what about |
Hello, Was it implemented in any update? I am also running into same issue. Want to define custom value for specific enum names. Any plan to support this ? Thanks, |
Same issue in C#. Good feature to have in Swagger-CodeGen too |
I want to generate enum with integer values assigned.
Swift:
Java:
But I couldn't figure out how to do this.
In the Swagger 2.0 specs, it references JSON Schema specs for
enum
.It says any type can be used, but it seems like
type
needs to bestring
for generating enums. Like this:If I change
type
tointeger
, no enum is generated, and it will be a plain integer property.Firstly, I couldn't find how to assign integers to enum items using Swagger specs.
Are there any plans or workarounds to support this kind of enum?
The text was updated successfully, but these errors were encountered: