For all my other enum classes swagger shows the string definition but for one enum class that I use in my 'ExceptionMiddleware' class it shows the numeric value. But in the swagger documentation example it shows the string value..
My enum class :
public enum ErrorCode
{
Undefined = -1,
None = 0,
ContractNotFound = 1000
}
One of my other enum classes that doesn't have this "problem" :
public enum ContractStatus
{
Undefined = 0,
Created = 1,
Valid = 2,
Invalid = 3
}
A result when the contract is not found :
I also have to add '[JsonPropertyName("errorCode")]' so the properties start with a lowercase letter. For all my other models this is not needed...
The class :
public class ExceptionResponse
{
[JsonPropertyName("errorCode")]
public ErrorCode ErrorCode { get; set; }
[JsonPropertyName("errorCodeLabel")]
public string ErrorCodeLabel { get; set; }
[JsonPropertyName("errorMessage")]
public string ErrorMessage { get; set; }
}
Configuration in 'Program.cs' :
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
If I remove this all enum's show numeric values instead of string values.
How I build the 'ExceptionResponse' model in my 'ExceptionMiddleware' class :
var exceptionResponse = new ExceptionResponse()
{
ErrorCode = ErrorCode.Undefined,
ErrorCodeLabel = ErrorCode.Undefined.ToString(),
ErrorMessage = "A random message."
};
And if there is an error :
await httpContext.Response.WriteAsync(JsonSerializer.Serialize(exceptionResponse));