-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
C#:- Enums names not generating the same name in client side #1234
Comments
Are you using Swashbuckle to generate the Swagger spec? |
Yes. |
Check your Swagger spec: Is the required information (the actual enum names) even available in the spec? When NSwag generates a Swagger spec, it adds an additional x-enumNames property with all enum names so that the generator knows the names and values... In Swashbuckle I think you only have the option to use string serialization instead of numbers so that the value und names are the same... |
I am using "swagger": "2.0" and in the spec I have not found anything x-enumNames. Could you please guide me what I am doing wrong and let me know any additional information required |
The problem is that Swagger does not support number enums with names, this is why we added a custom property x-enumNames to NSwag so that you can do it... but this only works if the generator (like NSwag does) generates this property (Swashbuckle does not). But you can configure Swashbuckle and ASP.NET to serialize enums not as integer but as string so that the enum value also is the name and the code gen works as expected... |
Thanks for the quick response. I have added code to serialize enums as string instead of int Now I am getting enum with names but still not getting exact enum name as that of name defined in C# |
Whats the diff? |
export enum ClassATestEnum but the name is not changing. Even I found that if the same Enum is used in multiple classes then that many enums are generated but not the single one |
strange, can you post the part of this enum in the generated swagger spec? |
That was just an example but now I am posting the exact code Enum generated in the spec, export enum AdministrativeActivityInputDtoCategory { public enum ActivityCategoryEnum Hope it helps |
I need to see this in the generated Swagger spec? |
Are you referring to swagger.json file or proxy file which are generated through nswag |
swagger.json - i need to know what Swashbuckle generated... |
"AdministrativeActivityInputDto": { |
And what enum is generated? |
export enum AdministrativeActivityInputDtoCategory { |
Ok looks correct. What’s wrong about that? |
Name of enum generated on client side, it is not the mention on C#. Enum name on C# :- ActivityCategoryEnum So if the same enum is used in multiple classes then multiple enums are generated on client side instead of generating only one enum. |
Yes, there is no way to "restore" the original name with the Swashbuckle generated spec because the enum is not referenced and thus does not have a name... How would NSwag restore the name with the given spec? |
But don't you think so this way I will be having n number of enums generated if reference in n places. This way client side file size also increases and there by impacting the performance. Is there any possibility to handle the duplication I mean Can I apply some attribute on the enum to get the exact name and avoid duplication. |
If the spec is generated with NSwag it automatically uses references so that it is only generated once and with the correct name - I dont know if this is possible with Swashbuckle. |
Ok... Thanks for the update.. Lastly can I get the enum name along with the integer value associated to it like, Todo = 0, |
This is what Swashbuckle generates and Swagger originally supports: String enum:
Integer enum:
As you can see there is no way to store both information (name and value). This is why NSwag will generate and process the following to store both:
Also NSwag references the enum so that the original name can be restored. I don't know if this is also possible with Swashbuckle. |
Where can I see this piece of code
x-enumNames I am not able to find in the generated proxy of nsswag |
You currently generate the swagger.json with Swashbuckle, so this x-enumNames is missing... If you generate the swagger.json with NSwag it will automatically add x-enumNames which will be picked up by the NSwag code generators. Maybe there is a way to manually add this x-enumNames also with Swashbuckle (a document filter?) |
Can I get the example to generate swagger.json using NSwag |
e.g. with middlewares like in Swashbuckle (there are a lot of other options) |
Thanks for the quick response. Also, I have one more query. Can I generate multiple swagger.json file from a single api end point? Is there any way like to place some attribute to distinguish and generate multiple swagger.json file |
You can add multiple UseSwagger calls with different routes and a custom document processors (see wiki) to exclude operations |
My API's are generated dynamically I am using ABP(ASP.Net Boilerplate) so can you just possible ways to generate the multiple swagger.json file. |
In Startup.cs you probably have the UseSwagger registration, you can register multiple with different routes and custom exclusions. |
Ok.. will explore more into that option.. If you have any link with examples to show generating multiple swagger.json with single endpoint that would really helpful in context to ABP |
If you can't use NSwag to generate the swagger definition, you can extend swashbuckle to include x-enumNames like this: Do not use "DescribeAllEnumsAsStrings" option in Swaschbuckle.
Apply it like this: Swashbuckle willl now generate a swagger definition that NSwag code generators will handle fine. |
ah, I figured out why this problem occurred. Both of your two questions #1499 & #1234 are the same reason. It's not the bug of NSwag, it's abp's fault. |
You need to know the following:
|
Hi there, we've followed the NSwag Enum docs, but we're having some problems with .AddControllers()
.AddJsonOptions(options =>
{
// Serialize enums as strings globally.
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
}) The only solution to our problem at the moment is to annotate all our enum classes with the using System.Text.Json.Serialization;
[JsonConverter(typeof(JsonStringEnumConverter))] // Applying this means Swagger displays [Starting, Running], not [1,2]
public enum ImportState
{
Starting = 1,
Running = 2
} There was a similar issue raised on the Swashbuckle repo, domaindrivendev/Swashbuckle.AspNetCore#1269, is this something that's been seen before with Nswag? |
If you are using Newtonsoft.Json, can be fixed like this: |
@FinHorsley In the case of newtonsoft we use the same behavior, i.e. we inspect the serializer settings for this global filter or on the property or enum directly... (not an NSwag config). In the case of System.Text.Json, we map the JsonStringEnumConverter to the Newtonsoft one, so the behavior should be the same: |
I wonder if anyone could show how to do this with the latest version.... |
A version of the XEnumNamesSchemaFilter for the latest version Swashbuckle:
|
@satyanarayan18 it's easier to use own contracts so in |
Works like a charm. This should be added to the wiki. |
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
|
@ ramax495 this seems not to work either on 14.0.3 of nswag build. and with EnumConverter something like this: but what i really need would be like (with version 13.x):
is there a solution to generate the correct way again? update: |
C# Code:-
public enum Test
{
Pass,
Fail
}
public class ClassA
{
public Test TestEnum {get;set;}
}
Client Side Type Script:-
export enum ClassATestEnum
{
_0=0,
_1=1
}
The text was updated successfully, but these errors were encountered: