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
JsonProperty(ItemConverterType = typeof(StringEnumConverter)) not recognized without DescribeAllEnumsAsStrings() #1349
Comments
I have the same problem |
@artfulsage can you elaborate a little more on your usecase? I do understand the issue you're reporting but I guess I'm not understanding why you wouldn't just use the The only reason I can think of to NOT do this is if you wanted to serialize the enum as a string in some cases (e.g. when part of a certain property) and as an integer in others. However, the |
I have the same problem (using 5.0.0 RC-5). I have tried adding a global JsonConvert ( |
@artfulsage - You're using Newtonsoft attributes, but Swashbuckle.AspNetCore 5.x uses System.Text.Json by default. To fix this, either:
|
@domaindrivendev, @jcoutch thanks for your feedback!
.AddJsonOptions(options => {
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
}); |
@domaindrivendev it might be helpful to add that to the docs/WIKI (FAQ) : |
@ddeisadzevp - I found it right on the main page: |
@artfulsage, when you use |
@domaindrivendev @jcoutch @tobyartisan perhaps I do something wrong but this code still does not work netcoreapp3.1 public class Program
{
public static void Main(string[] args)
=> Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(b => b.UseStartup<Startup>())
.Build()
.Run();
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson();
services
.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" }))
.AddSwaggerGenNewtonsoftSupport();
}
public void Configure(IApplicationBuilder app)
=> app
.UseSwagger()
.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"))
.UseRouting()
.UseEndpoints(endpoints => endpoints.MapControllers());
}
public enum BadEnum
{
Freezing, Bracing, Chilly, Cool
}
public class WeatherForecast
{
public DateTime Date { get; set; }
[Newtonsoft.Json.JsonProperty(ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public List<BadEnum> BadValues { get; set; }
}
[ApiController]
[Route("weather")]
public class WeatherForecastController : ControllerBase
{
[HttpGet]
public IEnumerable<WeatherForecast> Get()
=> Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
BadValues = new List<BadEnum> { BadEnum.Cool }
});
} |
@artfulsage Do you need to do this bit?
|
@artfulsage - unfortunately [JsonConverter(typeof(StringEnumConverter))]
public enum BadEnum
{
Freezing, Bracing, Chilly, Cool
} However, it seems this may not be feasible for you. As many of the comments in this issue have deviated from the original, I think it would be best to create a new issue that specifically addresses the lack of support for enum serialization behavior that's configured via |
Hi. In
Swashbuckle.AspNetCore 5.0.0-rc4
methodSwaggerGenOptions.DescribeAllEnumsAsStrings()
marked as Obsolete. But without it attribute[Newtonsoft.Json.JsonProperty(ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
is not working properly. See example below.With
SwaggerGenOptions.DescribeAllEnumsAsStrings()
The text was updated successfully, but these errors were encountered: