I've enabled my API to serialize/deserialize enum using string values. To do that I've added JsonStringEnumConverter to the list of supported JsonConverters in my API's Startup class:
.AddJsonOptions(opts =>
{
var enumConverter = new JsonStringEnumConverter();
opts.JsonSerializerOptions.Converters.Add(enumConverter);
});
It works fine- my API succeessfully serialize and deserialize enums as a string.
Now- I'm trying to build integration test for my API and having some issue.
I'm using HttpContentJsonExtensions.ReadFromJsonAsync
to deserialize the API response but an exception is thrown over an enum property.
The problem is obvious- HttpContentJsonExtensions.ReadFromJsonAsync
is not aware to the list of converters used by the API (since, as I mentioned earlier, I've added the JsonStringEnumConverter
to the list of supported converters and it works fine).
If I do this in my test function:
var options = new System.Text.Json.JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverter());
SomeClass result= await response.Content.ReadFromJsonAsync<SomeClass>(options);
Then the enum property is deserialized and no exception is thrown. However now, ReadFromJsonAsync
is only aware of JsonStringEnumConverter
and not to other JSON converters that are used by the API (like Guid converter)
How can I make sure that HttpContentJsonExtensions.ReadFromJsonAsync
will be able to use all JSON converters that are used by the API?
Thanks!