From 64d0e737905ed006eb640cc61c28524ce0734847 Mon Sep 17 00:00:00 2001 From: Christian Helle Date: Mon, 8 Jan 2024 08:44:14 +0100 Subject: [PATCH 1/2] Implement IPropertyNameGenerator using code from CSharpPropertyNameGenerator --- .../CustomCSharpPropertyNameGenerator.cs | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/Refitter.Core/CustomCSharpPropertyNameGenerator.cs b/src/Refitter.Core/CustomCSharpPropertyNameGenerator.cs index c7c641f8..677f84f4 100644 --- a/src/Refitter.Core/CustomCSharpPropertyNameGenerator.cs +++ b/src/Refitter.Core/CustomCSharpPropertyNameGenerator.cs @@ -1,10 +1,49 @@ using NJsonSchema; -using NJsonSchema.CodeGeneration.CSharp; +using NJsonSchema.CodeGeneration; namespace Refitter.Core; -internal class CustomCSharpPropertyNameGenerator : CSharpPropertyNameGenerator +internal class CustomCSharpPropertyNameGenerator : IPropertyNameGenerator { - public override string Generate(JsonSchemaProperty property) => - string.IsNullOrWhiteSpace(property.Name) ? "_" : base.Generate(property); + private static readonly char[] ReservedFirstPassChars = ['"', '\'', '@', '?', '!', '$', '[', ']', '(', ')', '.', '=', '+']; + private static readonly char[] ReservedSecondPassChars = ['*', ':', '-', '#', '&']; + + public string Generate(JsonSchemaProperty property) + { + var name = property.Name; + + if (name.IndexOfAny(ReservedFirstPassChars) != -1) + { + name = name + .Replace("\"", string.Empty) + .Replace("'", string.Empty) + .Replace("@", string.Empty) + .Replace("?", string.Empty) + .Replace("!", string.Empty) + .Replace("$", string.Empty) + .Replace("[", string.Empty) + .Replace("]", string.Empty) + .Replace("(", "_") + .Replace(")", string.Empty) + .Replace(".", "-") + .Replace("=", "-") + .Replace("+", "plus"); + } + + name = ConversionUtilities.ConvertToUpperCamelCase(name, true); + + if (name.IndexOfAny(ReservedSecondPassChars) != -1) + { + name = name + .Replace("*", "Star") + .Replace(":", "_") + .Replace("-", "_") + .Replace("#", "_") + .Replace("&", "And"); + } + + return string.IsNullOrWhiteSpace(property.Name) + ? "_" + : name; + } } \ No newline at end of file From 31f0cb932a2eda3f12773c86be98ae119adf783f Mon Sep 17 00:00:00 2001 From: Christian Helle Date: Mon, 8 Jan 2024 09:30:15 +0100 Subject: [PATCH 2/2] Refactor CSharpPropertyNameGenerator to handle reserved characters --- .../CustomCSharpPropertyNameGenerator.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Refitter.Core/CustomCSharpPropertyNameGenerator.cs b/src/Refitter.Core/CustomCSharpPropertyNameGenerator.cs index 677f84f4..abe677fe 100644 --- a/src/Refitter.Core/CustomCSharpPropertyNameGenerator.cs +++ b/src/Refitter.Core/CustomCSharpPropertyNameGenerator.cs @@ -1,3 +1,5 @@ +using System.Diagnostics.CodeAnalysis; + using NJsonSchema; using NJsonSchema.CodeGeneration; @@ -8,7 +10,18 @@ internal class CustomCSharpPropertyNameGenerator : IPropertyNameGenerator private static readonly char[] ReservedFirstPassChars = ['"', '\'', '@', '?', '!', '$', '[', ']', '(', ')', '.', '=', '+']; private static readonly char[] ReservedSecondPassChars = ['*', ':', '-', '#', '&']; - public string Generate(JsonSchemaProperty property) + public string Generate(JsonSchemaProperty property) => + string.IsNullOrWhiteSpace(property.Name) + ? "_" + : ReplaceNameContainingReservedCharacters(property); + + /// + /// This code is taken directly from NJsonSchema.CodeGeneration.CSharp.CSharpPropertyNameGenerator + /// which since v14.0.0 is no longer extensible. + /// See https://github.com/RicoSuter/NJsonSchema/blob/3585d60e949e43284601e0bea16c33de4c6c21f5/src/NJsonSchema.CodeGeneration.CSharp/CSharpPropertyNameGenerator.cs#L12" + /// + [ExcludeFromCodeCoverage] + private static string ReplaceNameContainingReservedCharacters(JsonSchemaProperty property) { var name = property.Name; @@ -42,8 +55,6 @@ public string Generate(JsonSchemaProperty property) .Replace("&", "And"); } - return string.IsNullOrWhiteSpace(property.Name) - ? "_" - : name; + return name; } } \ No newline at end of file