-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
How to generate enums along with the int -> string mapping #1993
Comments
Swashbuckle is another swagger library and you should only use one to generate a spec... to enable string enums, set add a new serializer settings object in SerializerSettings with a global StringEnumConverter |
Sorry, I don't understand. I have already tried https://github.com/RSuter/NJsonSchema/wiki/JsonSchemaGenerator#integer-vs-string-enumerations What I want is the integer with string enum. The following picture is from https://stackoverflow.com/questions/36452468/swagger-ui-web-api-documentation-present-enums-as-strings I mentioned before. |
@RSuter Could you explain about the global StringEnumConverter you mentioned before? |
See #1234 (comment) (same problem?) |
@RSuter I don't think so.
However, I can not understand it. |
Hi @RSuter , |
Nswag will generate this by default (the x-enumNames property) but it is a custom property and only understood by the nswag client gens - you cannot express this with vanilla swagger/openapi |
@RSuter I am using the NSwag according this document https://github.com/RSuter/NSwag/wiki/OWIN-Middleware
Is this nswag client? If it's not ,then what is it? |
@RSuter Sorry to disturb you again, could you explain the question above? |
UseSwaggerUi3 only serves the Swagger UI, UseSwagger registers the swagger generator and then there are client generators which are usually used via NSwagStudio or CLI... |
@RSuter As you mentioned before,
Then which method I supposed to use to make use of the |
Hi @RicoSuter , |
If I understand correctly, the only thing you want is to have this description with the int/string mappings in the Swagger UI? |
Not sure, I did not see how the default UI works. Currently, I am using UseSwaggerUi3. |
So you just want to see tjis in the web ui? |
What did you mean by tjis? Sorry for my poor English. |
tjis = this Sorry, i do not understand your problem |
Yes, I want to see enum with int/string in Swagger UI |
So you only want that this description field in the ui is shown with the mappings? |
yes, exactly |
Ok 🙂 This feature is not availble yet, but it would be quite simple to implement. The idea is to put this behind a setting, eg GenerateEnumMappingDescription Which only applies for int enums and which will generate this description. Does this sound good? Another better solution is to add support for x-enumNames to the Swagger UI project so that the ui just shows the names in the dropdown. Would this be a better solution for you? |
Thanks for your suggestion, I will try to figure it out. |
I can implement that... what do you think about my suggestions? |
I agree with you, this solution might be better |
Ok, as soon as i’m on a pc i’ll create issues in the swagger ui and redoc projects and add the new setting to njsonschema/nswag. |
Created new issues in Swagger UI and ReDoc (was already there). |
Added GenerateEnumMappingDescription in NJS, needs to be added as CLI/UI setting in NSwag |
I am also looking to do something similar to this and I haven't been able to find any solutions. I'm working on an app that consumes an API. I've added generated nswag interfaces from our API and it's working great but I am having an issue with enums. The app currently uses them as integers and I'm unable to convert them all to strings as it would be a massive refactoring effort. I have an enum in my API that looks like:
And I've only been able to generate
or
But I need a hybrid of the two such as
Is anyone able to point me in the right direction for this? |
I don't actually use NSwag for typescript generation but rather That generator utilizes the x-enumNames member of the spec to generate: export enum NotificationType {
Unknown = 0,
InAppMessage = 1,
WinnerNotification = 2,
PointsWinnerNotification = 3,
ConsecutiveDays = 4,
} I'm considering forking it to modify the enum template to generate in our project's style though: import { Opaque } from 'ts-essentials';
export const SigningMethod = {
Paper: 'paper' as Opaque<'paper', 'SigningMethod'>,
Electronic: 'electronic' as Opaque<'electronic', 'SigningMethod'>,
Transcribed: 'transcribed' as Opaque<'transcribed', 'SigningMethod'>,
} as const;
export type SigningMethod = typeof SigningMethod[keyof typeof SigningMethod]; The arch is easy to understand, it loads the spec and then runs a bunch of handlebars templates over the various components: https://github.com/cyclosproject/ng-openapi-gen |
@ryan4664 I'm using nSwag to generate specification and then open-api to generate TS client. For my case I needed to add x-enum-varnames data extension to specification, so client can be generated properly https://openapi-generator.tech/docs/templating/#enum In order to do that I had to implement custom operation processor
in AddOpenApiDocument configuration method
This should do the trick for openapi-gen, but I can't be sure if it's the right solution for other generators. Tweaking other nSwag options and adding StringEnum serializer didn't work for me. |
is there a formal solution to this? we'd like to see the option for enums to be converted to string values -> export enum NotificationTypeCurrent { export enum NotificationTypeDesired { |
@RicoSuter it looks like someone submitted a PR to add this to swagger-ui, but the maintainers rejected putting it in the core project. Maybe this could be pulled into nswag? Have you considered pulling in a new version of swagger-UI ? The latest couple patch versions have a fix for an annoying issue that we are running into. |
Just updated Swagger UI: |
Can you add a PR to add this? Mainly to see how this would work... |
With NSwag and System.Text.Json for me works:
and
It accepts int-s and strings, generates enums in open-api and .ts client with names and show enums with names in SwaggerUI
|
Hi, It does not work for me using newtonsoft, and only, or the numbers (without adding StringEnumConverter), or the enum names show up (when adding StringEnumConverter), on the UI. |
Yes, it's solution with System.Text.Json |
@ramax495, thanks for taking your time to answer it. Is there any plan to support the Newtonsoft library for this? |
@RicoSuter @ramax495 I get the following compilation error when trying to set
|
@harindaka look at the sources:
Property
|
I would like to show the enum with the int value and string.
I find a solution here, but it needs another library Swashbuckle.Core
https://stackoverflow.com/questions/36452468/swagger-ui-web-api-documentation-present-enums-as-strings
When I check the test code in source, I find https://github.com/RSuter/NSwag/blob/master/src/NSwag.SwaggerGeneration.WebApi.Tests/App_Start/SwaggerConfig.cs
What does it mean?Can I use NSwag and Swashbuckle at the same time?
Currently I am using NSwag with Owin,and the configuration is not GlobalConfiguration.Configuration. And I tried with the solution from stackoverflow, but it did not work.
The text was updated successfully, but these errors were encountered: