From 98c5c5ffb6642770912faa4f5011ac3da70b99e0 Mon Sep 17 00:00:00 2001 From: Emil Weihe Date: Wed, 10 Jan 2024 16:00:29 +0100 Subject: [PATCH 1/2] Use PropertyName instead of actual JSON Name for record generation --- .../Templates/Class.Constructor.Record.liquid | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Constructor.Record.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Constructor.Record.liquid index 5f576efd8..f88b7e1b9 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Constructor.Record.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Constructor.Record.liquid @@ -13,13 +13,13 @@ {%- else %} [Newtonsoft.Json.JsonConstructor] {%- endif %} -{% if IsAbstract %}protected{% else %}public{% endif %} {{ClassName}}({% for property in sortedProperties %}{%- if skipComma %}{%- assign skipComma = false %}{% else %}, {% endif %}{{ property.Type }} @{{ property.Name | lowercamelcase }}{% endfor %}) +{% if IsAbstract %}protected{% else %}public{% endif %} {{ClassName}}({% for property in sortedProperties %}{%- if skipComma %}{%- assign skipComma = false %}{% else %}, {% endif %}{{ property.Type }} @{{ property.PropertyName | lowercamelcase }}{% endfor %}) {%- assign skipComma = true %} {%- if HasInheritance %} - : base({%- for property in sortedParentProperties %}{%- if skipComma %}{%- assign skipComma = false %}{% else %}, {% endif %}{{ property.Name | lowercamelcase }}{%- endfor %}) + : base({%- for property in sortedParentProperties %}{%- if skipComma %}{%- assign skipComma = false %}{% else %}, {% endif %}{{ property.PropertyName | lowercamelcase }}{%- endfor %}) {%- endif %} { {%- for property in Properties %} - this.{{property.PropertyName}} = @{{property.Name | lowercamelcase}}; + this.{{property.PropertyName}} = @{{property.PropertyName | lowercamelcase}}; {%- endfor %} } From d8d037ae97501698b0181ff71913535d85c4ab71 Mon Sep 17 00:00:00 2001 From: Emil Weihe Date: Wed, 10 Jan 2024 16:34:08 +0000 Subject: [PATCH 2/2] Add test of parameter name --- .../GeneralGeneratorTests.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs index 5ac78e2ff..19fa4ab06 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs @@ -154,6 +154,33 @@ public async Task When_property_name_is_created_by_custom_fun_then_attribute_is_ AssertCompile(output); } + [Fact] + public async Task When_property_name_is_created_by_custom_fun_then_parameter_name_is_correct_for_record() + { + //// Arrange + var schema = NewtonsoftJsonSchemaGenerator.FromType
(); + var schemaData = schema.ToJson(); + var settings = new CSharpGeneratorSettings + { + ClassStyle = CSharpClassStyle.Record, + PropertyNameGenerator = new CustomPropertyNameGenerator(), + }; + var generator = new CSharpGenerator(schema, settings); + + //// Act + var output = generator.GenerateFile("Address"); + + //// Assert + Assert.DoesNotContain(@"public string Street { get; }", output); + Assert.Contains(@"public string MyCustomStreet { get; }", output); + Assert.Contains(@"this.MyCustomStreet = @myCustomStreet;", output); + + Assert.DoesNotContain(@"public Address(string @city, string @street)", output); + Assert.Contains(@"public Address(string @myCustomCity, string @myCustomStreet)", output); + + AssertCompile(output); + } + [Fact] public async Task When_schema_contains_ref_to_definition_that_refs_another_definition_then_result_should_contain_correct_target_ref_type() { @@ -1821,7 +1848,7 @@ public async Task When_class_is_abstract_constructor_is_protected_for_record() var data = schema.ToJson(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { - ClassStyle = CSharpClassStyle.Record + ClassStyle = CSharpClassStyle.Record, }); //// Act