Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sakets594
Copy link

@sakets594 sakets594 commented May 25, 2024

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

#2523

Motivation and Context

#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; }
    }
}
  1. 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; }
    }
}
  1. 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;

@inferrinizzard inferrinizzard changed the title added dependecy usings based on types present in renderContext fix(C#): added dependency usings based on types present in renderContext May 27, 2024
@sakets594 sakets594 force-pushed the bugFix/C#-usings-for-type-dependency branch from 0f67311 to 8169c75 Compare June 8, 2024 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants