Giter Site home page Giter Site logo

stefh / anyof Goto Github PK

View Code? Open in Web Editor NEW
102.0 8.0 7.0 111 KB

Use the AnyOf<TFirst, TSecond, ...> type to handle multiple defined types as input parameters or return values for methods.

License: MIT License

C# 99.79% Batchfile 0.21%
anyof type types multiple typed

anyof's Introduction

Hi ๐Ÿ‘‹, I'm Stef Heyenrath

stefh

  • ๐Ÿ”ญ Iโ€™m currently working on WireMock.Net
  • ๐ŸŒฑ Iโ€™m currently learning more Azure and Blazor

Environments

windows azure docker linux

Languages

csharp dotnet nodejs javascript typescript

Frontends

blazor angularjs vuejs bootstrap html5 chartjs

stefh

stefh

mr62rashy

anyof's People

Contributors

snyk-bot avatar stefh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

anyof's Issues

Issues with matching types on nested objects

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.