From 530b0f33de4818fba78d5c4c6f4c14b2cb42a01f Mon Sep 17 00:00:00 2001
From: chenrong <2881645810@qq.com>
Date: Thu, 26 Sep 2024 15:03:53 +0800
Subject: [PATCH] configurable fieldname
---
.../CSharpGeneratorSettings.cs | 6 ++++++
.../Models/PropertyModel.cs | 3 ++-
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs
index db0bf6178..63597b65e 100644
--- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs
+++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs
@@ -164,5 +164,11 @@ public CSharpGeneratorSettings()
/// Generate C# 9.0 record types instead of record-like classes.
public bool GenerateNativeRecords { get; set; }
+
+ ///
+ /// The prefix appended when generating a field based on the property name. Default is "_";
+ ///
+ public string FieldNamePrefix { get; set; } = "_";
+
}
}
diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs
index bc1684a9b..ccb19b734 100644
--- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs
+++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs
@@ -9,6 +9,7 @@
using NJsonSchema.Annotations;
using System.Globalization;
using NJsonSchema.CodeGeneration.Models;
+using System.Runtime;
namespace NJsonSchema.CodeGeneration.CSharp.Models
{
@@ -49,7 +50,7 @@ public PropertyModel(
public string? Description => _property.Description;
/// Gets the name of the field.
- public string FieldName => "_" + ConversionUtilities.ConvertToLowerCamelCase(PropertyName, true);
+ public string FieldName => _settings.FieldNamePrefix + ConversionUtilities.ConvertToLowerCamelCase(PropertyName, true);
/// Gets a value indicating whether the property is nullable.
public override bool IsNullable => (_settings.GenerateOptionalPropertiesAsNullable && !_property.IsRequired) || base.IsNullable;