SwaggerToCSharpClientGenerator: support of JsonPatchDocument<T> (HttpPatch)
See original GitHub issueHello,
when having a HttpPatch rest api method with a Microsoft.AspNetCore.JsonPatch.JsonPatchDocument<T>
as argument the csharp client generation creates a custom poco type like JsonPatchDocumentOfMyT
.
Since JsonPatchDocument provides a fluent way to define the patch operations like for example patchDoc.Replace(o => o.StringProperty, "B");
the generated custom poco is not very helpful.
Is there a way to exclude generic types from poco generation (such that the Microsoft.AspNetCore.JsonPatch.JsonPatchDocument<T>
is kept in the client method signature)? Or, where can I preferably control the generation/mapping of such action method parameters in using the NSwag.CodeGeneration.CSharp?
Best regards, peter
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:11 (3 by maintainers)
Top Results From Across the Web
SwaggerToCSharpClientGenerat...
Hello, when having a HttpPatch rest api method with a Microsoft. ... SwaggerToCSharpClientGenerator: support of JsonPatchDocument<T> ...
Read more >SwaggerToCSharpClientGenerat...
SwaggerToCSharpClientGenerator : JSON output of JsonPatchDocument (HttpPatch) includes the operations property wrapper, causing model binding ...
Read more >JsonPatch in ASP.NET Core web API
This article explains how to handle JSON Patch requests in an ASP.NET Core web API. Package installation. JSON Patch support in ASP.NET Core...
Read more >Swagger unexpected API PATCH action documentation of ...
Why is Swagger generating an alternate structure for a JsonPatchDocument object in the request body for the PATCH endpoint? How do I fix...
Read more >Using HttpClient to Send HTTP PATCH Requests in ASP. ...
We can see that if we want to support a request body for the PATCH requests, we have to use the JsonPatchDocument class....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am also struggling with this, trying to create a patch according from the example in MS docs with an empty WebApi .netCore 3.0 project.
My action is specified as:
First, if I use default values for Api and the
CSharpClientGenerator
, I get same described in this issue (i.e. a type namedJsonPatchDocumentOfWeatherForecast
intead ofJsonPatchDocument<WeatherForecast>
).If I instead use below to configure swagger generation:
I get a swagger json file like:
For the
CSharpClientGenerator
this generates below code.Excluding type
JsonPatchDocumentOfWeatherForecast
in the generator does not help since then I only end up with theSystem.Collections.Generic.IDictionary<string, object> patchDoc
parameter for the patch action.Any more hints of how to proceed? Thanks.
For those who still have this problem, I have found a workaround, even if it requires manual adjustment for each case.
Publish your DTO types (where the Generics are) as separate package
Reference your DTO’s in the Project where you use the Generated C# Client
Manually Add all wrongly generated Generics (e.g.
ResponseOfFoo,ResponseOfBar,ResponseOfFooBar
) toexcludedTypeNames
in your.nswag
-file. This can be tedious depending on your project size but in my case this part will probably not change a lot in the future… And there may be a way to grab these automatically if you really need to.Tell NSwag to use your custom Generics from your DTO’s by adding the required imports to
additionalNamespaceUsages
in your.nswag
-fileafter NSwag Client generation, call MakeGenericAgain. I simply do this in my
.csproj
like so:And you can basically do the same thing for TypeScript.
Hope this helps - Cheers 😃