Open daiplusplus opened 6 years ago
The additional usages setting is mainly used to import DTO types when DTO generation is disabled.
Template directory is explained here: https://github.com/RSuter/NSwag/wiki/Templates
But i thinks most of your suggestions just make the default templates more robust.
Need to check your suggestions in detail...
Most import is that we dont break existing users...
Similar issue, maybe we should sync, https://github.com/RSuter/NSwag/issues/1666
I ran into a similar issue using the NSWagCSharp OpenApiReference CodeGenerateor via the csproj file.
I have my own DTOs so generating with /GenerateDtoTypes:false
I could not find an option that allows you to specify additional namespaces to be used in the generated client.
For anyone else that runs into this issue.. C# 10 supports global using that can mostly solve this.
Usings.cs:
global using TheNamespaceOf.YourDTOs;
global using Task = TheNamespaceOf.YourDTOs.Task;
^ I have a Task class that conflicts with the System.Threading.Tasks.Task class - override it this way.
The result is the Api Client is generated with the expected types that don't resolve, adding the global using allows it to compile.
(I realize C# 10 was released in 2021 some time after this issue was opened.. mentioning incase someone else lands here)
@aluitink found the way how to make it work
The parameter to add namespace using works. You could try
<Options>/jsonLibrary:SystemTextJson /generateDtoTypes:false /AdditionalNamespaceUsages:"My.Namespace1,My.Namespace2"</Options>
The serialization problem that controllers usually use Camel Case. NSwag has default one with 1-1 mapping (first letter must be capital).
To change default serialization on controller side (.NET 7 Asp.net core):
builder.Services.AddControllersWithViews().AddJsonOptions(options `=>`
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
In the current NSwag C# client generation template all types have fully-qualified namespace names, e.g.
In NSwag.Studio I set "Additional namespace usages" to
System.Net.Http,System.Threading.Tasks,Newtonsoft.Json
in the expectation that this would cause the generated C# code to look like this instead;...but it doesn't, because the namespaces are hardcoded in the C# templates:
If the aim is to ensure the types fully resolve then the generated code should use the
global::
keyword like the VS2005 WinForms designer did.I have other template changes I'd like to make, I'll be happy to submit a PR but because this enters opinionated programming territory I imagine the project owners are not very receptive to changes to those templates, or are they?
The changes I'd like to make include:
#pragma warning disable
and generate code that doesn't generate warnings in the first place (and use specific targeted warning-disable pragmas, and/or make it an NSwag configuration option).var
.JsonTextReader
instead of reading into aString
first, which is faster and avoids extra string allocations (but retain theString
version for debugging)int
notstring
(why does this happen?)using
instead of trivialtry/finally/dispose
My question:
Update:
I see NSwag.Studio has "Template directory" as an option but I can't see any documentation for it. The existing Issue is from 2016 and the wiki article even says it's outdated.