-
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
Csharp Client not generating valid enums #5352
Comments
@chidionuekwusi thanks for reporting the issue. What's the "correct" code look like? I could find a test case here to cover integer enum in C#: https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test/Model/EnumClassTests.cs#L66 |
@wing328 It looks to me like the issue is more about how swagger definitions define enums. For example, defining a gender enum like this in definition:
Results in this enum:
Technically, this is correct and is the only way I know of to provide semantics to the enum's numbers. However, the enum is annotated with If you define the enum as numbers with data type
You get the following enum (on master):
Here you get numbers serialized to the API, but no semantics about what those numbers mean. If you attempt to add semantics with numerical values:
You end up with a broken enum:
Interestingly, the above doesn't cause a parsing issue (type is number, value is non-number). Sounds like the issue report expects the generator to accept the above definition and have this result in:
The problem is: if you look at the OpenAPI spec, it points to: https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.1 for the spec on enum definition and enum validation.
So, if we accept none,male,female as values in an enum and transform those to 0, 1, 2... not only would this break validation as defined in 5.5.1.2 above, but it would be unintuitive whether the API accepts I did a little more investigation and this seems to be a common issue with OpenAPI specification, and is in progress. You can follow the discussion here: OAI/OpenAPI-Specification#681. We've had this reported on other generators (see #2690), but it's unfortunately a limitation of the specification. @wing328 should we consider supporting this via vendor extension? That seems a bit hacky to me, but it would work considering the semantics (none, male, female) are only metadata at a code level. |
Sorry for late reply on this. Previously I did consider using vendor extension to support this but the one who reported the issue previously was not interested in using vendor extension as a solution. If there are more demand for a solution addressing this issue, we'll guide the community on how to fix it to begin with. |
I have a very similar issue as this one. Therefore, I allow myself to post it in this issue instead of creating an extra issue for something so similar. Issue
Looking forward to hear from you and thank you very much. |
Description
CSharp client generation does not generate valid c# for enums with int values. eg
enum Sex {
None=0,
Male=1,
Female=2
}
After running generate command with the below config
The resultant code:
[JsonConverter(typeof(StringEnumConverter))]
public enum Sex
{
Swagger-codegen version
2.2.3
Swagger declaration file content or url
Command line used for generation
java -jar swagger-codegen-cli.jar generate -i %JSON_CONFIG% -l csharp -c "%APP_FOLDER%code-gen-config.json" -o "%APP_FOLDER%API"
Steps to reproduce
Related issues
Suggest a Fix
The text was updated successfully, but these errors were encountered: