Skip to content
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

Open
Jump33 opened this issue Nov 30, 2022 · 3 comments
Open

not working deserialazer in controller #33

Jump33 opened this issue Nov 30, 2022 · 3 comments

Comments

@Jump33
Copy link

Jump33 commented Nov 30, 2022

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,

[EnumMember(Value = "2.0")]
v2_0 = 200,

[EnumMember(Value = "2.1")]
v2_1 = 210

}

Controller(endpoint):

[HttpGet("versions/{version}")]
    public GetVersionModel GetVersions(OcpiVersion version)
    {
        return _ocpiVersionsService.GetVersion(version);
    }

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."

@bbartels
Copy link

bbartels commented Jul 4, 2023

@CodeBlanch running into the same issue. Any ideas on how one would work around this?

EDIT: After setting some breakpoints in JsonStringEnumMemberConverter<TEnum> it seems like it is not called when the controller attempts to bind the incoming json to the parameters. Is there some separate registration of the converter needed or something?

Setting the following has not worked either:

builder.Services.AddControllers().AddJsonOptions(
    x => x.JsonSerializerOptions.Converters.Add(new JsonStringEnumMemberConverter())
);

@bbartels
Copy link

bbartels commented Jul 5, 2023

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.

@korydondzila
Copy link

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
https://stackoverflow.com/a/56843019

Though for me specifying the ModelBinder on the enum did not work

[JsonConverter(typeof(JsonStringEnumMemberConverter))]
public enum PaymentFrequency
{
    WEEKLY,
    [EnumMember(Value = "BI-WEEKLY")] BI_WEEKLY,
    MONTHLY
}

Instead it needed to be specified for the parameter.

public IActionResult GetPaymentPlanPreviewAsync(
        [FromQuery] [ModelBinder(typeof(PaymentFrequencyBinder))] PaymentFrequency? paymentFrequency)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants