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

Issues with matching types on nested objects #17

Closed
dan-drews opened this issue Mar 29, 2023 · 5 comments
Closed

Issues with matching types on nested objects #17

dan-drews opened this issue Mar 29, 2023 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@dan-drews
Copy link

Good afternoon! I was exploring this project and I personally think it's awesome! Good work! Just wanted to raise an issue on this, let me know if it's something you'd plan to handle or not please, no worries if it doesn't fit into the scope or goals of this project.

I am attempting to utilize this for supporting multiple unique json responses for a given API endpoint.

When working with flat responses, this tool works really well. The issue I'm encountering is when each object has sub-objects on it. What I ahve found is that you need to register every single type from every possible property in your API response as part of the AnyOf<T1,T2,T3> call, otherwise the sub-objects can't have their types resolved.

Here is a sample I have put together

using AnyOfTypes;
using AnyOfTypes.System.Text.Json;
using System.Text.Json;



var options = new JsonSerializerOptions();
options.Converters.Add(new AnyOfJsonConverter());
options.PropertyNameCaseInsensitive = true;

var json1 = "{\"Value\": \"1\"}";
var json2 = "{\"StringValue\": \"1\"}";
var json3 = "{\"StringValue\": \"1\",\"SubClass\": {\"SampleProperty\": \"Abc\"}}";

var result = System.Text.Json.JsonSerializer.Deserialize<AnyOf<A, B>>(json1, options); // Matches type 2 and has a value of type "B". Works as expected
var result2 = System.Text.Json.JsonSerializer.Deserialize<AnyOf<A, B>>(json2, options); // Matches type 1 and has a value of type "A". Works as expected. SubClass on the result is null, as expected
var result3 = System.Text.Json.JsonSerializer.Deserialize<AnyOf<A, B, SampleSubClass>>(json3, options); // Matches type 2 and has a value of type "B". Works as expected. SubClass has a value with "Abc" as the sample property result
var result4 = System.Text.Json.JsonSerializer.Deserialize<AnyOf<A, B>>(json3, options); // Throws this exception: System.Text.Json.JsonException: 'No suitable type found.'

public class SampleSubClass
{
    public string SampleProperty { get; set; }
}

class A
{
    public SampleSubClass SubClass { get; set; }
    public string StringValue { get; set; }
}

class B
{
    public string Value { get; set; }
}

Would it be possible to recursively build a list of all sub-types on all classes that are passed as type arguments?

Additionally, this doesn't work if not every possible value on Json is used. For example, let's look at the example above, but with:

var json4 = "{\"StringValue\": \"1\",\"SubClass\": {\"SampleProperty\": \"Abc\"}, \"UnusedSubClass\": {\"UnusedProperty\": 123} }";

Well if I don't have a need for that in my use-case, FindBestObjectMatch will still try to find a match for it and throw an exception instead of simply ignoring it and picking the best match on the parent.

@StefH
Copy link
Owner

StefH commented Mar 29, 2023

I've created version "AnyOf.System.Text.Json 0.4.0-preview-01" which should solve var result4 = System.Text.Json.JsonSerializer.Deserialize<AnyOf<A, B>>(json3, options); // Throws this exception: System.Text.Json.JsonException: 'No suitable type found.'

Can you test it?

@dan-drews
Copy link
Author

That seemed to have done the trick.

Also I tested my note below about the unusedSubClass - It looks like this either wasn't an issue or was also resolved with that version 0.4. Thanks for the very quick response!

@StefH
Copy link
Owner

StefH commented Mar 29, 2023

If you can. Do some more tests.

( I will also check the NewtonSoft version for the same logic)

@StefH
Copy link
Owner

StefH commented Mar 30, 2023

I applied the same test for Newtonsoft, but that one just worked fine.

So I'll release a new 0.0.4 version in some time on NuGet.

@StefH StefH closed this as completed Mar 30, 2023
@StefH StefH added the bug Something isn't working label Mar 30, 2023
@StefH StefH self-assigned this Mar 30, 2023
@StefH
Copy link
Owner

StefH commented Mar 30, 2023

#19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants