From e34f960321e044c8e66dc52cebbfe38674f55608 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Sun, 1 Mar 2026 15:57:04 +0000
Subject: [PATCH 1/2] Initial plan
From 2465783df3b60394242d533cbfd2f1c9397024fc Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Sun, 1 Mar 2026 16:02:56 +0000
Subject: [PATCH 2/2] Fix infinite loop in RenamePropertyWithSameNameAsClass,
add regression test
Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
---
.../GeneralGeneratorTests.cs | 26 ++++++++++++++++
...renamed_without_infinite_loop.verified.txt | 30 +++++++++++++++++++
.../CSharpGenerator.cs | 1 +
3 files changed, 57 insertions(+)
create mode 100644 src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_has_same_name_as_class_and_suffixed_name_is_taken_then_it_is_renamed_without_infinite_loop.verified.txt
diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs
index b2d944613..cdc579d62 100644
--- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs
+++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs
@@ -603,6 +603,32 @@ public async Task When_property_has_same_name_as_class_then_it_is_renamed()
CSharpCompiler.AssertCompile(code);
}
+ [Fact]
+ public async Task When_property_has_same_name_as_class_and_suffixed_name_is_taken_then_it_is_renamed_without_infinite_loop()
+ {
+ // Arrange
+ var schemaJson = @"{
+ ""type"": ""object"",
+ ""properties"": {
+ ""Foo"": {
+ ""type"": ""string""
+ },
+ ""Foo1"": {
+ ""type"": ""string""
+ }
+ }
+}";
+ var schema = await JsonSchema.FromJsonAsync(schemaJson);
+
+ // Act
+ var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });
+ var code = generator.GenerateFile("Foo");
+
+ // Assert
+ await VerifyHelper.Verify(code);
+ CSharpCompiler.AssertCompile(code);
+ }
+
[Fact]
public async Task When_patternProperties_is_set_with_string_value_type_then_correct_dictionary_is_generated()
{
diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_has_same_name_as_class_and_suffixed_name_is_taken_then_it_is_renamed_without_infinite_loop.verified.txt b/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_has_same_name_as_class_and_suffixed_name_is_taken_then_it_is_renamed_without_infinite_loop.verified.txt
new file mode 100644
index 000000000..e2840c65e
--- /dev/null
+++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_has_same_name_as_class_and_suffixed_name_is_taken_then_it_is_renamed_without_infinite_loop.verified.txt
@@ -0,0 +1,30 @@
+//----------------------
+//
+//
+//----------------------
+
+
+namespace MyNamespace
+{
+ #pragma warning disable // Disable all warnings
+
+ public partial class Foo
+ {
+
+ [Newtonsoft.Json.JsonProperty("Foo", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
+ public string Foo2 { get; set; }
+
+ [Newtonsoft.Json.JsonProperty("Foo1", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
+ public string Foo1 { get; set; }
+
+ private System.Collections.Generic.IDictionary _additionalProperties;
+
+ [Newtonsoft.Json.JsonExtensionData]
+ public System.Collections.Generic.IDictionary AdditionalProperties
+ {
+ get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
+ set { _additionalProperties = value; }
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs
index b0d429315..fe8575dc3 100644
--- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs
+++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs
@@ -140,6 +140,7 @@ private static void RenamePropertyWithSameNameAsClass(string typeName, List p.PropertyName == candidate))
{
number++;
+ candidate = typeName + number;
}
propertyWithSameNameAsClass.PropertyName += number;