domn1995/dunet

Question: make it to work with asp net

Closed this issue · 6 comments

Hello,
I'm really sorry to ask here, but I would like to know how can I make it to work with asp (the serialization / deserialization part).

I created from scratch a repro:
https://github.com/titouancreach/repro-dunet

I've no problem in serializating my object to json, but the issue come when I try to deserialize it from json (see .http file)

I would be really grateful if you have any idea.
Thanks very muche

Hey, thank for the question! Looks like your request file is missing a quote on line 14. Maybe that's the issue?

If I don't hear back from you tonight I'll look into it deeper and create a sample once I figure it out.

Looks like the issue is the extra quote in https://github.com/titouancreach/repro-dunet/blob/main/Repro.http#L14C20-L14C20. Worked fine for me once I removed it.

Oh sure you are right ! I need to update my repro repo because I have an issue in my "real" repo (and I hope it's not a quote since I spent several hours on it) 😮. I will reopen with a proper repro. Thanks you very miche for your reactivity, I appreciate !

Ohh once I removed the quote, I got the error I have in my real code:



fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Shape'. Path: $.param | LineNumber: 0 | BytePositionInLine: 20.
       ---> System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Shape'.
         --- End of inner exception stack trace ---
         at System.Text.Json.ThrowHelper.ThrowNotSupportedException(ReadStack& state, Utf8JsonReader& reader, NotSupportedException ex)
         at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
         at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
         at System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter`5.TryRead[TArg](ReadStack& state, Utf8JsonReader& reader, JsonParameterInfo jsonParameterInfo, TArg& arg)
         at System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter`5.ReadAndCacheConstructorArgument(ReadStack& state, Utf8JsonReader& reader, JsonParameterInfo jsonParameterInfo)
         at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.ReadConstructorArgumentsWithContinuation(ReadStack& state, Utf8JsonReader& reader, JsonSerializerOptions options)
         at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
         at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
         at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
         at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.ContinueDeserialize(ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack)
         at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.DeserializeAsync(Stream utf8Json, CancellationToken cancellationToken)
         at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.DeserializeAsObjectAsync(Stream utf8Json, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Http.HttpRequestJsonExtensions.ReadFromJsonAsync(HttpRequest request, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Http.HttpRequestJsonExtensions.ReadFromJsonAsync(HttpRequest request, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Http.RequestDelegateFactory.<HandleRequestBodyAndCompileRequestDelegateForJson>g__TryReadBodyAsync|94_0(HttpContext httpContext, Type bodyType, String parameterTypeName, String parameterName, Boolean allowEmptyRequestBody, Boolean throwOnBadRequest, JsonTypeInfo jsonTypeInfo)
         at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass94_2.<<HandleRequestBodyAndCompileRequestDelegateForJson>b__2>d.MoveNext()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

How did you make it work ?


Edit1:

Postman generate this request:

curl --location 'http://localhost:5237/du/' \
--header 'Content-Type: application/json' \
--data '{
    "param": {
        "$type": "Circle",
        "radius": 45
    }
}
'

and it works

and my vim plugin generate:

curl -sSL --compressed -X 'POST' -H 'Content-Type: application/json' --data-raw '{"param": {"radius": 45, "$type": "Circle"}}' 'localhost:5237/du/'

that doesn't work


Edit2:

Ok I want to die, actually the order is important, "$type" should be first !

Should we put this in the Readme or in a comment in the example ? I literally wasted like 4h/5h of debug

https://stackoverflow.com/questions/16870416/does-the-sequence-of-the-values-matter-in-a-json-object

Oh, man. That must not have been fun to troubleshoot. Thanks for taking the time. I've created a PR updating the relevant sample and readme with that information. Hopefully others don't run into the same issue.

Yes, I made some tests, and without Dunet, the message is kind of explicit (it tells you $type should be present and first) but with Dunet, the message is shadowed.

Thanks for making it clear in the readme :)