glideapps / quicktype

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

[BUG]: C# codegen does not generate all fields when using dependecies with oneOf #2562

Open Chalolennox opened 2 months ago

Chalolennox commented 2 months ago

Input Format: JSON Schema Draft 07

Output Language: C#

CLI, npm, or app.quicktype.io: app.quicktype.io Version:

Description

when I combine dependecies keyword with oneOf keyword, some field are not generated in the output C# code.

Input Data

 {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Conditional Properties Example",
    "type": "object",
    "properties": {
        "acceptsTerms": {
            "type": "boolean",
            "title": "Do you accept the terms and conditions?"
        },
        "userDetails": {
            "type": "object",
            "properties": {
                "firstName": {
                    "type": "string",
                    "title": "First Name"
                },
                "lastName": {
                    "type": "string",
                    "title": "Last Name"
                },
                "email": {
                    "type": "string",
                    "format": "email",
                    "title": "Email Address"
                }
            },
            "required": [
                "firstName",
                "lastName",
                "email"
            ]
        }
    },
    "dependencies": {
        "acceptsTerms": {
            "oneOf": [
                {
                    "properties": {
                        "acceptsTerms": {
                            "const": true
                        },
                        "userDetails": {
                            "type": "object",
                            "properties": {
                                "comment": {
                                    "type": "string",
                                    "title": "comment"
                                }
                            }
                        }
                    },
                    "required": [
                        "userDetails"
                    ]
                },
                {
                    "properties": {
                        "acceptsTerms": {
                            "const": false
                        }
                    }
                }
            ]
        }
    }
} 

Expected Behaviour / Output

Property "Comment" should be inserted

// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var test = Test.FromJson(jsonString);

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

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

    public partial class Test
    {
        [JsonProperty("acceptsTerms", NullValueHandling = NullValueHandling.Ignore)]
        public virtual bool? AcceptsTerms { get; set; }

        [JsonProperty("userDetails", NullValueHandling = NullValueHandling.Ignore)]
        public virtual UserDetails UserDetails { get; set; }
    }

    public partial class UserDetails
    {
        [JsonProperty("email")]
        public virtual string Email { get; set; }

        [JsonProperty("firstName")]
        public virtual string FirstName { get; set; }

        [JsonProperty("lastName")]
        public virtual string LastName { get; set; }

        [JsonProperty("comment")]
        public virtual string Comment { get; set; }

    }

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

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

    ............
}

Current Behaviour / Output

Property "Comment" is missing.

// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var test = Test.FromJson(jsonString);

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

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

    public partial class Test
    {
        [JsonProperty("acceptsTerms", NullValueHandling = NullValueHandling.Ignore)]
        public virtual bool? AcceptsTerms { get; set; }

        [JsonProperty("userDetails", NullValueHandling = NullValueHandling.Ignore)]
        public virtual UserDetails UserDetails { get; set; }
    }

    public partial class UserDetails
    {
        [JsonProperty("email")]
        public virtual string Email { get; set; }

        [JsonProperty("firstName")]
        public virtual string FirstName { get; set; }

        [JsonProperty("lastName")]
        public virtual string LastName { get; set; }
    }

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

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