glideapps / quicktype

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

fix(C#): added dependency usings based on types present in renderContext #2606

Open sakets594 opened 1 month ago

sakets594 commented 1 month ago

Description

change - forcing dependency namespace ("System.Collections.Generic") to be added for list and dictionary in cases where common namespace addition(CSharpRenderer.emitUsings) was skipped

Related Issue

https://github.com/glideapps/quicktype/issues/2523

Motivation and Context

https://github.com/glideapps/quicktype/issues/2523

Previous Behaviour / Output

if you run the C# renderer with --features just-types --array-type list then it creates List properties, but doesn't add the required using System.Collections.Generic;. It also happens without the --array-type list if a Dictionary<K,V> is generated as a property.

  1. Example with dictionary command: echo '{ "name": "David","keyval":{"1":{"prop1":1},"2":{"prop1":1},"3":{"prop1":3}} }' | script/quicktype -l csharp --features just-types output:

    namespace QuickType
    {
    
    public partial class TopLevel
    {
        public string Name { get; set; }
        public Dictionary<string, Keyval> Keyval { get; set; }
    }
    
    public partial class Keyval
    {
        public long Prop1 { get; set; }
    }
    }
  2. Example with array as list command: echo '{ "name": "David","arr":[1,2,3] }' | script/quicktype -l csharp --features just-types --array-type list output:

    namespace QuickType
    {
    
    public partial class TopLevel
    {
        public string Name { get; set; }
        public List<long> Arr { get; set; }
    }
    }

New Behaviour / Output

with new change using System.Collections.Generic gets added inside name space before class definition when required

  1. Example with dictionary command: echo '{ "name": "David","keyval":{"1":{"prop1":1},"2":{"prop1":1},"3":{"prop1":3}} }' | script/quicktype -l csharp --features just-types output:

    namespace QuickType
    {
    using System.Collections.Generic;
    
    public partial class TopLevel
    {
        public string Name { get; set; }
        public Dictionary<string, Keyval> Keyval { get; set; }
    }
    
    public partial class Keyval
    {
        public long Prop1 { get; set; }
    }
    }
  2. Example with array as list command: echo '{ "name": "David","arr":[1,2,3] }' | script/quicktype -l csharp --features just-types --array-type list output:

    namespace QuickType
    {
    using System.Collections.Generic;
    
    public partial class TopLevel
    {
        public string Name { get; set; }
        public List<long> Arr { get; set; }
    }
    }

How Has This Been Tested?

Manual testing using command line, covered negative and positive cases of examples provided above [EDIT] - tested with both array and dictionary present in json to validate single using System.Collections.Generic;