You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
usingAnyOfTypes;usingAnyOfTypes.System.Text.Json;usingSystem.Text.Json;varoptions=newJsonSerializerOptions();options.Converters.Add(newAnyOfJsonConverter());options.PropertyNameCaseInsensitive=true;varjson1="{\"Value\": \"1\"}";varjson2="{\"StringValue\": \"1\"}";varjson3="{\"StringValue\": \"1\",\"SubClass\": {\"SampleProperty\": \"Abc\"}}";varresult=System.Text.Json.JsonSerializer.Deserialize<AnyOf<A,B>>(json1,options);// Matches type 2 and has a value of type "B". Works as expectedvarresult2=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 expectedvarresult3=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 resultvarresult4=System.Text.Json.JsonSerializer.Deserialize<AnyOf<A,B>>(json3,options);// Throws this exception: System.Text.Json.JsonException: 'No suitable type found.'publicclassSampleSubClass{publicstringSampleProperty{get;set;}}classA{publicSampleSubClassSubClass{get;set;}publicstringStringValue{get;set;}}classB{publicstringValue{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:
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.
The text was updated successfully, but these errors were encountered:
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.'
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!
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
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:
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.The text was updated successfully, but these errors were encountered: