-
Notifications
You must be signed in to change notification settings - Fork 25
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
not working deserialazer in controller #33
Comments
@CodeBlanch running into the same issue. Any ideas on how one would work around this? EDIT: After setting some breakpoints in Setting the following has not worked either: builder.Services.AddControllers().AddJsonOptions(
x => x.JsonSerializerOptions.Converters.Add(new JsonStringEnumMemberConverter())
); |
Okay, seems like this is limited to extracting values from the path/query string. When making sure the value is extracted from the body of the request it works fine. I presume it doesn't attempt to use the jsonserializer at all when binding the model from the path/query string. So this isn't really an issue of this package, but more an issue of aspnetcore. |
Running into this specific issue as well on a GET endpoint. This is apparently normal and the process to handle this is to make a ModelBinder. https://learn.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding Though for me specifying the ModelBinder on the enum did not work
Instead it needed to be specified for the parameter.
Is this annoying? Yeah because it's incredibly basic and should not be required for enums at all. Would be nice if aspnet handled the deserialization on simple query params. |
Settings for controller:
builder.Services.AddControllers().AddJsonOptions(options =>
{ options.JsonSerializerOptions.Converters
.Add(new JsonStringEnumMemberConverter());
});
Enums:
using System;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
namespace OCPIServer.Application.Ocpi.Contracts.Enums.Versioning;
//I tried with and without attributes
//[JsonStringEnumMemberConverterOptions(deserializationFailureFallbackValue: OcpiVersion.Unknown)]
//[JsonConverter(typeof(JsonStringEnumMemberConverter))]
public enum OcpiVersion
{
[EnumMember(Value = "unknown")]
Unknown = 0,
}
Controller(endpoint):
Everything works if you set 210 or v2_1_0 in the request. But I need the value (2.1.0) to be specified in the request. Like this
https://localhost:7059/.../versions/2.1.0.
But that doesn't work. Deserialization does not occur. I get error 400 "The value '2.1.0' is not valid."
The text was updated successfully, but these errors were encountered: