Swashbuckle.AspNetCore
Swashbuckle.AspNetCore copied to clipboard
StringEnumConverter is not respected on property
Swashbuckle.AspNetCore.Newtonsoft 6.4.0
Code to reproduce:
public enum PromotionType
{
New,
Paying,
}
public class Model
{
[JsonConverter(typeof(StringEnumConverter))]
public PromotionType PromotionType { get; set; }
}
output is
"PromotionType": {
"format": "int32",
"enum": [
0,
1
],
"type": "integer"
},
while using
[JsonConverter(typeof(StringEnumConverter))]
public enum PromotionType
{
New,
Paying,
}
public class Model
{
public PromotionType PromotionType { get; set; }
}
will produce
"PromotionType": {
"enum": [
"New",
"Paying"
],
"type": "string"
},
Interestingly
[JsonConverter(typeof(StringEnumConverter))]
public enum PromotionType
{
New,
Paying,
}
public class Model
{
[JsonConverter(typeof(OtherConverter))]
public PromotionType PromotionType { get; set; }
}
will produce int as well, so it is somehow aware of the property converter.
"PromotionType": {
"format": "int32",
"enum": [
0,
1
],
"type": "integer"
},
Might be related to #2474
Are you calling services.AddSwaggerGenNewtonsoftSupport();
?
Are you registering the converters?
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
})
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new StringOnlyEnumConverter());
})
Does it work if you call services.AddSwaggerGen(c => {c.DescribeAllEnumsAsStrings(); } );
?
@icnocop
Are you calling
services.AddSwaggerGenNewtonsoftSupport();
?
Yes, other JSON configurations work just fine (like JsonProperty attribute with custom naming)
Are you registering the converters?
Nope, that's not intended. We don't want to enable such converter by default, only for certain cases.
Does it work if you call
services.AddSwaggerGen(c => {c.DescribeAllEnumsAsStrings(); } );
?
I guess it would, but same as above, that's not intended, we want to follow case by case basis. I can try if you insist.
Same issue here as @fabicz.
The goal is to apply enum-to-string converter to specific API endpoints not globally across the project.
Unfortunately the API has to be backwards compatible so the global approach isn't an option.
What is the recommended approach?
This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.
I have not been able to reproduce this behaviour and I am not thinking that this could be issued anyway. Because the object types are being represented in the Components/Schemas section.
If you have the same enum marked in one property as String and in other as int.. The object representation is probably not going to be the expected.
internal class TypeWithStringEnumConverter
{
[JsonConverter(typeof(StringEnumConverter))]
public IntEnum Property { get; set; }
public IntDerivatedEnum IntDerivatedEnum { get; set; }
[JsonConverter(typeof(IntEnumConverter))]
public IntDerivatedEnum Second { get; set; }
}
[JsonConverter(typeof(StringEnumConverter))]
public enum IntDerivatedEnum
{
Value2 = 2,
Value4 = 4,
Value8 = 8,
}
public class IntEnumConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType.IsEnum;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return Convert.ChangeType(existingValue, objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue((int)value);
}
}
In this example I show you how you would like to be represented the IntDerivatedEnum object? as a String or as a Int? The library supports the global serializer options + type serializer options, but I think it does not support the property serializer options.
Reproducing your problem with the current version gives me this results:
Scenario | Result |
---|---|
Property Scenario | Global Serializer Options |
Enum Scenario | Enum Serializer Options |
Property + Enum Scenario | Enum Serializer Options |
I find this results to be right
@martincostello this issue is a candidate to close
This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.