glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
12.15k stars 1.06k forks source link

JsonSchema OneOf C# CodeGen Issue #2007

Open PeterJsonUser opened 1 year ago

PeterJsonUser commented 1 year ago

This schema: ` { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "title": "Container", "properties": { "Animal": { "oneOf": [ { "$ref": "#/definitions/Dog" }, { "$ref": "#/definitions/Cat" } ] } }, "definitions": { "Dog":{ "type": "object", "properties": { "dogname":{ "type": "string" } } }, "Cat":{ "type": "object", "properties": { "catname":{ "type": "number" } } } } }

`

generates only one class: Dog. Is it a bug or feature? I am expecting to see two classes generated Dog and Cat represented via another class Animal. How can I get it? ` // // // To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: // // using QuickType; // // var coordinate = Coordinate.FromJson(jsonString);

namespace QuickType { using System; using System.Collections.Generic;

using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public partial class Coordinate
{
    [JsonProperty("Animal", NullValueHandling = NullValueHandling.Ignore)]
    public Dog Animal { get; set; }
}

public partial class Dog
{
    [JsonProperty("dogname", NullValueHandling = NullValueHandling.Ignore)]
    public string Dogname { get; set; }

    [JsonProperty("catname", NullValueHandling = NullValueHandling.Ignore)]
    public double? Catname { get; set; }
}

public partial class Coordinate
{
    public static Coordinate FromJson(string json) => JsonConvert.DeserializeObject<Coordinate>(json, QuickType.Converter.Settings);
}

public static class Serialize
{
    public static string ToJson(this Coordinate self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
}

internal static class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
        Converters =
        {
            new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
        },
    };
}

}

`

ShaneMurphy2 commented 1 year ago

Seeing this same behavior when generating Rust code with oneOf