diff --git a/Json2CSharpCodeGenerator.Lib/CodeWriters/CSharpCodeWriter.cs b/Json2CSharpCodeGenerator.Lib/CodeWriters/CSharpCodeWriter.cs index 55c6f70..ae1d9e9 100644 --- a/Json2CSharpCodeGenerator.Lib/CodeWriters/CSharpCodeWriter.cs +++ b/Json2CSharpCodeGenerator.Lib/CodeWriters/CSharpCodeWriter.cs @@ -490,12 +490,11 @@ public class CSharpCodeWriter : ICodeBuilder { if (field.Type.Type == JsonTypeEnum.Array) { - // TODO: Respect config.CollectionType - _ = sw.AppendFormat(indentMembers + "public IReadOnlyList<{0}> {1} {{ get; }}{2}", GetTypeName(field.Type.InternalType, config), classPropertyName, Environment.NewLine); + _ = sw.AppendFormat(indentMembers + "public {0} {1} {{ init; get; }}{2}", GetCollectionTypeName(elementTypeName: GetTypeName(field.Type.InternalType, config), config.CollectionType), classPropertyName, Environment.NewLine); } else { - _ = sw.AppendFormat(indentMembers + "public {0} {1} {{ get; }}{2}", field.Type.GetTypeName(), classPropertyName, Environment.NewLine); + _ = sw.AppendFormat(indentMembers + "public {0} {1} {{ init; get; }}{2}", field.Type.GetTypeName(), classPropertyName, Environment.NewLine); } } else diff --git a/Json2CSharpCodeGenerator.Tests/Json2CSharpCodeGenerator.Tests.csproj b/Json2CSharpCodeGenerator.Tests/Json2CSharpCodeGenerator.Tests.csproj index a6e7957..d9b869a 100644 --- a/Json2CSharpCodeGenerator.Tests/Json2CSharpCodeGenerator.Tests.csproj +++ b/Json2CSharpCodeGenerator.Tests/Json2CSharpCodeGenerator.Tests.csproj @@ -33,6 +33,12 @@ Always + + Always + + + Always + Always diff --git a/Json2CSharpCodeGenerator.Tests/Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES.cs b/Json2CSharpCodeGenerator.Tests/Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES.cs new file mode 100644 index 0000000..1e39cc8 --- /dev/null +++ b/Json2CSharpCodeGenerator.Tests/Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES.cs @@ -0,0 +1,38 @@ +using Json2CSharpCodeGenerator.Lib; +using Json2CSharpCodeGenerator.Lib.CodeWriters; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Json2CSharpCodeGenerator.Tests; + +[TestClass] +public class Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES +{ + + [TestMethod] + public void Run() + { + string path = Path.Combine(AppContext.BaseDirectory, "Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_INPUT.txt"); + string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_OUTPUT.txt"); + string input = File.ReadAllText(path); + JsonClassGenerator jsonClassGenerator = new() + { + AttributeLibrary = JsonLibrary.SystemTextJson, + AttributeUsage = JsonPropertyAttributeUsage.OnlyWhenNecessary, + CodeWriter = new CSharpCodeWriter(), + CollectionType = OutputCollectionType.MutableList, + OutputType = OutputTypes.ImmutableClass, + UsePascalCase = true, + UseThisKeyWord = false, + }; + string returnVal = jsonClassGenerator.GenerateClasses(input, errorMessage: out _).ToString(); + string resultsCompare = File.ReadAllText(resultPath); + string expected = resultsCompare.NormalizeOutput(); + string actual = returnVal.NormalizeOutput(); + if (expected != actual) + { + File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_OUTPUT.actual.txt"), returnVal); + File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_OUTPUT.expected.txt"), resultsCompare); + } + Assert.AreEqual(expected, actual); + } +} \ No newline at end of file diff --git a/Json2CSharpCodeGenerator.Tests/Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_INPUT.txt b/Json2CSharpCodeGenerator.Tests/Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_INPUT.txt new file mode 100644 index 0000000..50cad5b --- /dev/null +++ b/Json2CSharpCodeGenerator.Tests/Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_INPUT.txt @@ -0,0 +1,30 @@ +{ + "Class1":{ + "id":4, + "user_id":"user_id_value", + "awesomeobject": {"SomeProps1" : 1, "SomeProps2" : "test"}, + "created_at":"2015-06-02 23:33:90", + "updated_at":"2015-06-02 23:33:90", + "users":[ + { + "id":"6", + "name":"Test Child 1", + "created_at":"2015-06-02 23:33:90", + "updated_at":"2015-06-02 23:33:90", + "e mail":"test@gmail.com" + }, + { + "id":"6", + "name":"Test Child 1", + "created_at":"2015-06-02 23:33:90", + "updated_at":"2015-06-02 23:33:90", + "e mail":"test@gmail.com", + "testanadditionalfield":"tet" + } + ], + }, + + "Class2" : { + "SomePropertyOfClass2" : "SomeValueOfClass2" +} +} \ No newline at end of file diff --git a/Json2CSharpCodeGenerator.Tests/Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_OUTPUT.txt b/Json2CSharpCodeGenerator.Tests/Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_OUTPUT.txt new file mode 100644 index 0000000..8dc03e9 --- /dev/null +++ b/Json2CSharpCodeGenerator.Tests/Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_OUTPUT.txt @@ -0,0 +1,104 @@ +// Root myDeserializedClass = JsonSerializer.Deserialize(myJsonResponse); + public class Awesomeobject + { + [JsonConstructor] + public Awesomeobject( + int someProps1, + string someProps2 + ) + { + SomeProps1 = someProps1; + SomeProps2 = someProps2; + } + + public int SomeProps1 { init; get; } + public string SomeProps2 { init; get; } + } + + public class Class1 + { + [JsonConstructor] + public Class1( + int id, + string userId, + Awesomeobject awesomeobject, + string createdAt, + string updatedAt, + List users + ) + { + Id = id; + UserId = userId; + Awesomeobject = awesomeobject; + CreatedAt = createdAt; + UpdatedAt = updatedAt; + Users = users; + } + + public int Id { init; get; } + public string UserId { init; get; } + public Awesomeobject Awesomeobject { init; get; } + public string CreatedAt { init; get; } + public string UpdatedAt { init; get; } + public List Users { init; get; } + } + + public class Class2 + { + [JsonConstructor] + public Class2( + string somePropertyOfClass2 + ) + { + SomePropertyOfClass2 = somePropertyOfClass2; + } + + public string SomePropertyOfClass2 { init; get; } + } + + public class Root + { + [JsonConstructor] + public Root( + Class1 class1, + Class2 class2 + ) + { + Class1 = class1; + Class2 = class2; + } + + public Class1 Class1 { init; get; } + public Class2 Class2 { init; get; } + } + + public class User + { + [JsonConstructor] + public User( + string id, + string name, + string createdAt, + string updatedAt, + [JsonPropertyName("e mail")] string email, + string testanadditionalfield + ) + { + Id = id; + Name = name; + CreatedAt = createdAt; + UpdatedAt = updatedAt; + EMail = email; + Testanadditionalfield = testanadditionalfield; + } + + public string Id { init; get; } + public string Name { init; get; } + public string CreatedAt { init; get; } + public string UpdatedAt { init; get; } + + [JsonPropertyName("e mail")] + public string EMail { init; get; } + public string Testanadditionalfield { init; get; } + } +