-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from christianhelle/nswag-v14-prerequisites
Implement CustomCSharpPropertyNameGenerator
- Loading branch information
Showing
1 changed file
with
54 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,60 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
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) => | ||
string.IsNullOrWhiteSpace(property.Name) | ||
? "_" | ||
: ReplaceNameContainingReservedCharacters(property); | ||
|
||
/// <summary> | ||
/// 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" | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
private static string ReplaceNameContainingReservedCharacters(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 name; | ||
} | ||
} |