From 01ee840a03c176be5a3c5dd591ed248d57a42efa Mon Sep 17 00:00:00 2001
From: AntonYeurasau <164544791+AntonYeurasau@users.noreply.github.com>
Date: Tue, 26 Mar 2024 22:42:42 +0100
Subject: [PATCH] Update PropertyNameGenerator - escape "|" character in
property names (#1684)
Some swagger documents contain | character in property names which is a reserved character in C# and is not valid for use in property names. This code change replaces vertical line character |with an underscore character _ to generate a valid (in C#) property name
---
.../CSharpPropertyNameGenerator.cs | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpPropertyNameGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpPropertyNameGenerator.cs
index 2c61c752f..2a42c6df8 100644
--- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpPropertyNameGenerator.cs
+++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpPropertyNameGenerator.cs
@@ -11,7 +11,7 @@ namespace NJsonSchema.CodeGeneration.CSharp
/// Generates the property name for a given CSharp .
public sealed class CSharpPropertyNameGenerator : IPropertyNameGenerator
{
- private static readonly char[] _reservedFirstPassChars = { '"', '\'', '@', '?', '!', '$', '[', ']', '(', ')', '.', '=', '+' };
+ private static readonly char[] _reservedFirstPassChars = { '"', '\'', '@', '?', '!', '$', '[', ']', '(', ')', '.', '=', '+', '|' };
private static readonly char[] _reservedSecondPassChars = { '*', ':', '-', '#', '&' };
/// Generates the property name.
@@ -35,7 +35,8 @@ public string Generate(JsonSchemaProperty property)
.Replace(")", string.Empty)
.Replace(".", "-")
.Replace("=", "-")
- .Replace("+", "plus");
+ .Replace("+", "plus")
+ .Replace("|", "_");
}
name = ConversionUtilities.ConvertToUpperCamelCase(name, true);
@@ -53,4 +54,4 @@ public string Generate(JsonSchemaProperty property)
return name;
}
}
-}
\ No newline at end of file
+}