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

Error on deserialize json to Either<string, T> #6

Open
josercf opened this issue Dec 3, 2019 · 3 comments
Open

Error on deserialize json to Either<string, T> #6

josercf opened this issue Dec 3, 2019 · 3 comments

Comments

@josercf
Copy link

josercf commented Dec 3, 2019

I tried use a Either<string, T> as property of a view model in one web api with .NET CORE 3.0

When I sent a string as value of the property, it deserialize correctly, but when property values is an any T, the Either type identify as TRight correctly but can't deserialize it.

*When I set the same data directly from code C# it's works correctly

Below I pasted some evidencies of this issue

csproj
Microsoft.Net.Compilers Version="3.3.1"
System.ValueTuple Version="4.5.0"
Tango Version="3.1.2"

View Model
`public class SampleRequestModel
{
[JsonProperty("option")]
public int Option{ get; set; }

    [JsonProperty("payload")]
    public Either<string, Location> Payload { get; set; }
}

public class Location
{
    [JsonProperty("latitude")]
    public double Latitude { get; set; }

    [JsonProperty("longitude")]
    public double Longitude { get; set; }
}`

Controller

`[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
public async Task Post([FromBody] SampleRequestModel model)
{
string filter = string.Empty;

        Location location = model.Payload.Match(
             (payload) =>
             {
                 return payload;
             },
             (text) =>
             {
                 filter = text;
                 return default;
             });

        return Ok(new { location, filter});
    }
}`

Request

1 - Works
curl -X POST
http://localhost:50535/api/values
-H 'Accept: /'
-H 'Accept-Encoding: gzip, deflate'
-H 'Cache-Control: no-cache'
-H 'Connection: keep-alive'
-H 'Content-Length: 36'
-H 'Content-Type: application/json'
-H 'Host: localhost:50535'
-H 'Postman-Token: 45c3d866-2cd1-4266-8364-b22dd6aa8283,660d6618-8e8d-4ef5-983b-00ee0e6bb9a9'
-H 'User-Agent: PostmanRuntime/7.20.1'
-H 'cache-control: no-cache'
-d '{
"option": 1,
"payload": "jose"
}'

2- Dont work
curl -X POST
http://localhost:50535/api/values
-H 'Accept: /'
-H 'Accept-Encoding: gzip, deflate'
-H 'Cache-Control: no-cache'
-H 'Connection: keep-alive'
-H 'Content-Length: 36'
-H 'Content-Type: application/json'
-H 'Host: localhost:50535'
-H 'Postman-Token: 45c3d866-2cd1-4266-8364-b22dd6aa8283,0a834c05-a2d6-4faf-81e7-5ea53e6a4a3d'
-H 'User-Agent: PostmanRuntime/7.20.1'
-H 'cache-control: no-cache'
-d '{
"option": 1,
"payload": {
"latitude": 23.123,
"longitude": 48.334
}
}'

image

@LeonardoPrange
Copy link

Hello, it may have been a mistake typing here in Issue but I see your JsonProperty is ("loongitude") and in payload this "longitude", can you check if this is the problem?

@josercf
Copy link
Author

josercf commented Feb 3, 2020

Hi!
I have make a mistake on type the code here. The deserialization doesn't occurs correctly. I think that the model binder provider doesn't know create a instance of Either type

@LeonardoPrange
Copy link

Model Binder has its limitations, and the Either type is very complex for it to convert correctly, I think that to resolve it would be to change the payload to a simpler class and do the conversion later.

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

2 participants