Last active
September 30, 2019 23:32
-
-
Save GeorgDangl/af167dd657b8f8b2879f8208341304a4 to your computer and use it in GitHub Desktop.
Fixing NSwag issue with duplicated enum values in the generated Swagger document, https://blog.dangl.me/archive/remove-duplicate-enum-entries-in-swagger-documents-with-nswag-in-aspnet-core/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum HttpStatusCode | |
{ | |
/* Excerpt from System.Net.HttpStatusCode */ | |
RedirectKeepVerb = 307, | |
TemporaryRedirect = 307 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 information | |
foreach (var enumType in x.Definitions.Select(d => d.Value).Where(d => d.IsEnumeration)) | |
{ | |
var distinctValues = enumType.Enumeration.Distinct().ToList(); | |
enumType.Enumeration.Clear(); | |
foreach (var distinctValue in distinctValues) | |
{ | |
enumType.Enumeration.Add(distinctValue); | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment