- ๐ญ Iโm currently working on WireMock.Net
- ๐ฑ Iโm currently learning more Azure and Blazor
Environments
Languages
Frontends
Use the AnyOf<TFirst, TSecond, ...> type to handle multiple defined types as input parameters or return values for methods.
License: MIT License
Environments
Languages
Frontends
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.