You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following is the definition of the System.Net.HttpStatusCode type (shortened) from .nuget\packages\netstandard.library\2.0.3\build\netstandard2.0\ref\netstandard.dll:
namespace System.Net
{//// Summary:// Contains the values of status codes defined for HTTP.publicenumHttpStatusCode{//// Summary:// Equivalent to HTTP status 300. System.Net.HttpStatusCode.Ambiguous indicates// that the requested information has multiple representations. The default action// is to treat this status as a redirect and follow the contents of the Location// header associated with this response.Ambiguous=300,//// Summary:// Equivalent to HTTP status 300. System.Net.HttpStatusCode.MultipleChoices indicates// that the requested information has multiple representations. The default action// is to treat this status as a redirect and follow the contents of the Location// header associated with this response.MultipleChoices=300,}}
After a bit more processing (I'm using a model containing a HttpStatusCode property in a Web API, which gets then converted via NSwag to a Swagger definition, which then again is used to create a C# client) this is the generated C# client code (shortened):
To mitigate this, the following PostProcess works for me in ASP.NET Core:
services.AddSwaggerDocument(c =>{ c.PostProcess =(x)=>{// Some enum classes use multiple integer values for the same value, e.g.// System.Net.HttpStatusCode uses these:// RedirectKeepVerb = 307// TemporaryRedirect = 307// MVC is configured to use the StringEnumConverter, and NJsonSchema errorenously// outputs the duplicates. For the example above, the value 'TemporaryRedirect' is// serialized twice, 'RedirectKeepVerb' is missing.// The following post process action should remove duplicated enums// See https://github.com/RSuter/NJsonSchema/issues/800 for more informationforeach(var enumType in x.Definitions.Select(d => d.Value).Where(d => d.IsEnumeration)){vardistinctValues= enumType.Enumeration.Distinct().ToList(); enumType.Enumeration.Clear();foreach(var distinctValue in distinctValues){ enumType.Enumeration.Add(distinctValue);}}};});
I think you also need to remove the corresponding name in EnumerationNames… otherwise the name-value mapping might be wrong in the generated code. I think this problem should be fixed directly in NJS... i.e. filter out duplicates as you do it manually now...
The following is the definition of the
System.Net.HttpStatusCode
type (shortened) from.nuget\packages\netstandard.library\2.0.3\build\netstandard2.0\ref\netstandard.dll
:Per this StackOverflow question (which references the C# spec), this is allowed and correct syntax.
Generating the schema
Produces this (shortened) JSON schema:
After a bit more processing (I'm using a model containing a
HttpStatusCode
property in a Web API, which gets then converted via NSwag to a Swagger definition, which then again is used to create a C# client) this is the generated C# client code (shortened):This just fails at runtime when deserializing with Newtonsoft.Json:
The workaround for me is to simply use the
System.Net.HttpStatusCode
enum in my generated client instead of the generated one.The text was updated successfully, but these errors were encountered: