-
-
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
Generated class names cause compiler warning. #4849
Comments
Same as issue #4837 ? |
Just to add a few search keywords: this warning was introduced in .NET7 (C# 11) and thus also appears in .NET8 projects. We are hit by it after updating a .NET6 application to .NET8. |
Workaround: add a file ".editorconfig" in the affected project and switch off the warning for those generated files:
|
After digging through the code I think this could be fixed by modifying https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/DefaultTypeNameGenerator.cs#L91, because "GenerateAnonymousTypeName" does not call "ConversionUtilities.ConvertToUpperCamelCase" when it builds types from the hint. It could be modified like this (adding a call three times): private string GenerateAnonymousTypeName(string typeNameHint, IEnumerable<string> reservedTypeNames)
{
if (!string.IsNullOrEmpty(typeNameHint))
{
if (this.TypeNameMappings.TryGetValue(typeNameHint, out var mapping))
{
typeNameHint = mapping;
}
typeNameHint = GetLastSegment(typeNameHint)!;
typeNameHint = ConversionUtilities.ConvertToUpperCamelCase(typeNameHint, true);
if (typeNameHint != null &&
!reservedTypeNames.Contains(typeNameHint) &&
Array.IndexOf(reservedTypeNames.ToArray(), typeNameHint) == -1)
{
return typeNameHint;
}
var count = 1;
do
{
count++;
} while (reservedTypeNames.Contains(ConversionUtilities.ConvertToUpperCamelCase(typeNameHint + count, true)));
return ConversionUtilities.ConvertToUpperCamelCase(typeNameHint + count, true);
}
return GenerateAnonymousTypeName("Anonymous", reservedTypeNames);
} At least this fixes the generating of lower case class names for me. And I compared the code usages of my own generated client from NSwag 13 with the NSwag 14 version, and I see that my code that uses lower case type names now would use the the same type names with numbers ("Data3") that were created by NSwag 13. @RicoSuter What do you think? Shall I send a pull request? |
I'm using this yaml file: https://github.com/legalesign/Legalesign-V1-OpenAPI3/blob/master/legalesign-api-v1.yaml
When I process that using nswag 13.8.2 it creates C# classes named
Objects2
andObjects3
and this compiles without warnings.However, when I use nswag 14.0.7 instead (the latest release), the created C# classes now have names
objects
andobjects2
.Whilst that compiles, there's a warning that wasn't there with the older nswag release:
The text was updated successfully, but these errors were encountered: