quicktype
quicktype copied to clipboard
fix(C#): added dependency usings based on types present in renderContext
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<T> 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.
- Example with dictionary
command:
echo '{ "name": "David","keyval":{"1":{"prop1":1},"2":{"prop1":1},"3":{"prop1":3}} }' | script/quicktype -l csharp --features just-typesoutput:
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; }
}
}
- Example with array as list
command:
echo '{ "name": "David","arr":[1,2,3] }' | script/quicktype -l csharp --features just-types --array-type listoutput:
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
- Example with dictionary
command:
echo '{ "name": "David","keyval":{"1":{"prop1":1},"2":{"prop1":1},"3":{"prop1":3}} }' | script/quicktype -l csharp --features just-typesoutput:
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; }
}
}
- Example with array as list
command:
echo '{ "name": "David","arr":[1,2,3] }' | script/quicktype -l csharp --features just-types --array-type listoutput:
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;