-
-
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
Generating C# DTOs with custom generic class #1139
Comments
Swagger/Open API and JSON Schema do not support generics (also see RicoSuter/NJsonSchema#23). I think there are two options:
|
Ah, I see! Well I am in full control of the service, so it is probably best to just remove generics from the WebApi DTOs. Thanks! |
I have exactly the same problem. Maybe Swagger/OpenAPI doesn't support generic. But, suppose that you already have following type names in swagger.json (Generated from
Currently, during NSwag code generation, type names above are converted into:
Why is not possible to convert it into following type names? (e.g. by enabling some setting in configuration ".nswag" file):
Generation of generic Many thanks for answer. |
you can also define a wrap class, and use it instead of generic
|
I wrote an article to show one of the possible solutions |
I also have this kind a problem with a Blazor project, and I'd like a way to create object as original generic types.. |
I#ve created a tool for this problem. Its called makeGenericAgain and can be found here https://www.nuget.org/packages/MakeGenericAgain/ also source is available here https://github.com/fgilde/MakeGenericAgain |
@fgilde thank you so much for that tool! It's saving me a ton of time. |
Thank you very much. That makes me happy |
It's a little more effort, but generic classes can be remapped back to the original by:
|
Could you share a example file please? That would be mind blowing Do you guys recommend using this aproaches? Do you know of any OpenAPI alternative that has generics support build in? I would primarily need C# client generation, but also other languages for 3rd parties Any thoughts? Cheers |
PS: Generics are now supported by OpenAPI 3.1. So the statement at the top of this issue is no longer valid and this can be supported. |
I have a C# WebApi that exposes a class and enum like this:
The error code can be a string or one of several enum types.
The generated Swagger (not done with NSwag) looks like below. Only relevant parts and one of the enums included.
Is there anything I can do, either with the Swagger generation, or with the NSwag client generation, to get a generic DTO like the original one? Using NSwagStudio 11.12.16.0, I get the following output (some stuff removed for brevity):
Having an enum named "ErrorOfOneTimeAmountErrorTypeCode" is not what I call intuitive. :)
If it is impossible to generate a
Error<T>
, an acceptable solution for me would be if I could get more control of the generated names. I.e. the enum above should be named as the original enum (I am aware that that isn't present in the Swagger though..).Grateful for any help!
The text was updated successfully, but these errors were encountered: