Phares Test and { init; get; }
This commit is contained in:
parent
29fb70ce06
commit
4be9cae9e6
@ -490,12 +490,11 @@ public class CSharpCodeWriter : ICodeBuilder
|
|||||||
{
|
{
|
||||||
if (field.Type.Type == JsonTypeEnum.Array)
|
if (field.Type.Type == JsonTypeEnum.Array)
|
||||||
{
|
{
|
||||||
// TODO: Respect config.CollectionType
|
_ = sw.AppendFormat(indentMembers + "public {0} {1} {{ init; get; }}{2}", GetCollectionTypeName(elementTypeName: GetTypeName(field.Type.InternalType, config), config.CollectionType), classPropertyName, Environment.NewLine);
|
||||||
_ = sw.AppendFormat(indentMembers + "public IReadOnlyList<{0}> {1} {{ get; }}{2}", GetTypeName(field.Type.InternalType, config), classPropertyName, Environment.NewLine);
|
|
||||||
}
|
}
|
||||||
else
|
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
|
else
|
||||||
|
@ -33,6 +33,12 @@
|
|||||||
<None Include="Test_10_SETTINGS_IMMUTABLE_CLASSES_INPUT.txt">
|
<None Include="Test_10_SETTINGS_IMMUTABLE_CLASSES_INPUT.txt">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Include="Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_OUTPUT.txt">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
<None Include="Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_INPUT.txt">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
<None Include="Test_11_NoListSetter_INPUT.txt">
|
<None Include="Test_11_NoListSetter_INPUT.txt">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,104 @@
|
|||||||
|
// Root myDeserializedClass = JsonSerializer.Deserialize<Root>(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<User> 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<User> 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; }
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user