From 60236d44768c7d26796ab2e35551274e6e4592f8 Mon Sep 17 00:00:00 2001 From: Frank Samiec Date: Thu, 31 Oct 2024 15:38:57 +0100 Subject: [PATCH 1/2] added options for a custom PropertyNameGenerator and a custom ParameterNamegenerator --- src/Refitter.Core/CSharpClientGeneratorFactory.cs | 7 ++++++- .../Settings/CodeGeneratorSettings.cs | 13 +++++++++++-- .../Settings/RefitGeneratorSettings.cs | 4 ++++ src/Refitter.Tests/SerializerTests.cs | 15 +++++++++++---- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Refitter.Core/CSharpClientGeneratorFactory.cs b/src/Refitter.Core/CSharpClientGeneratorFactory.cs index 25a97616d..2a819b5ff 100644 --- a/src/Refitter.Core/CSharpClientGeneratorFactory.cs +++ b/src/Refitter.Core/CSharpClientGeneratorFactory.cs @@ -24,7 +24,7 @@ public CustomCSharpClientGenerator Create() GenerateDtoTypes = true, GenerateClientInterfaces = false, GenerateExceptionClasses = false, - CodeGeneratorSettings = { PropertyNameGenerator = new CustomCSharpPropertyNameGenerator(), }, + CodeGeneratorSettings = { PropertyNameGenerator = settings.CodeGeneratorSettings?.PropertyNameGenerator ?? new CustomCSharpPropertyNameGenerator() }, CSharpGeneratorSettings = { Namespace = settings.ContractsNamespace ?? settings.Namespace, @@ -41,6 +41,11 @@ public CustomCSharpClientGenerator Create() } }; + if (settings.ParameterNameGenerator != default) + { + csharpClientGeneratorSettings.ParameterNameGenerator = settings.ParameterNameGenerator; + } + csharpClientGeneratorSettings.CSharpGeneratorSettings.TemplateFactory = new CustomTemplateFactory( csharpClientGeneratorSettings.CSharpGeneratorSettings, [ diff --git a/src/Refitter.Core/Settings/CodeGeneratorSettings.cs b/src/Refitter.Core/Settings/CodeGeneratorSettings.cs index cde6819cb..75c771a48 100644 --- a/src/Refitter.Core/Settings/CodeGeneratorSettings.cs +++ b/src/Refitter.Core/Settings/CodeGeneratorSettings.cs @@ -1,4 +1,7 @@ -namespace Refitter.Core; +using System.Text.Json.Serialization; +using NJsonSchema.CodeGeneration; + +namespace Refitter.Core; /// /// CSharp code generator settings @@ -155,4 +158,10 @@ public class CodeGeneratorSettings /// Gets or sets the excluded type names (must be defined in an import or other namespace). /// public string[] ExcludedTypeNames { get; set; } = Array.Empty(); -} \ No newline at end of file + + /// + /// Gets or sets a custom . + /// + [JsonIgnore] + public IPropertyNameGenerator? PropertyNameGenerator { get; set; }// = new CustomCSharpPropertyNameGenerator(); +} diff --git a/src/Refitter.Core/Settings/RefitGeneratorSettings.cs b/src/Refitter.Core/Settings/RefitGeneratorSettings.cs index 43282b2f2..6f3f4f55e 100644 --- a/src/Refitter.Core/Settings/RefitGeneratorSettings.cs +++ b/src/Refitter.Core/Settings/RefitGeneratorSettings.cs @@ -1,5 +1,6 @@ using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; +using NSwag.CodeGeneration; namespace Refitter.Core; @@ -226,4 +227,7 @@ public class RefitGeneratorSettings /// See https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism for more information /// public bool UsePolymorphicSerialization { get; set; } + + [JsonIgnore] + public IParameterNameGenerator? ParameterNameGenerator { get; set; } } diff --git a/src/Refitter.Tests/SerializerTests.cs b/src/Refitter.Tests/SerializerTests.cs index 7cd61edf7..c199cfb67 100644 --- a/src/Refitter.Tests/SerializerTests.cs +++ b/src/Refitter.Tests/SerializerTests.cs @@ -1,4 +1,5 @@ -using Atc.Test; +using System.Reflection; +using Atc.Test; using FluentAssertions; using Refitter.Core; using Xunit; @@ -18,14 +19,16 @@ public void Can_Serialize_RefitGeneratorSettings( } [Theory, AutoNSubstituteData] - public void Can_Deserialize_RefitGeneratorSettings( + public void Can_Deserialize_RefitGeneratorSettingsWithoutNameGenerators( RefitGeneratorSettings settings) { var json = Serializer.Serialize(settings); Serializer .Deserialize(json) .Should() - .BeEquivalentTo(settings); + .BeEquivalentTo(settings, options => options + .Excluding(settings => settings.ParameterNameGenerator) + .Excluding(settings => settings.CodeGeneratorSettings.PropertyNameGenerator)); } [Theory, AutoNSubstituteData] @@ -44,6 +47,10 @@ public void Deserialize_Is_Case_Insensitive( Serializer .Deserialize(json) .Should() - .BeEquivalentTo(settings); + .BeEquivalentTo(settings, options => options + .Excluding(settings => settings.ParameterNameGenerator) + .Excluding(settings => settings.CodeGeneratorSettings.PropertyNameGenerator)); } + + } From 734b069d999a64812d8d845ae2c43616daf527ab Mon Sep 17 00:00:00 2001 From: Frank Samiec Date: Thu, 31 Oct 2024 16:12:48 +0100 Subject: [PATCH 2/2] fixed sonar issue --- src/Refitter.Core/Settings/CodeGeneratorSettings.cs | 4 ++-- src/Refitter.Tests/SerializerTests.cs | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Refitter.Core/Settings/CodeGeneratorSettings.cs b/src/Refitter.Core/Settings/CodeGeneratorSettings.cs index 75c771a48..ed97dc2ba 100644 --- a/src/Refitter.Core/Settings/CodeGeneratorSettings.cs +++ b/src/Refitter.Core/Settings/CodeGeneratorSettings.cs @@ -148,7 +148,7 @@ public class CodeGeneratorSettings /// Gets or sets a value indicating whether to generate default values for properties (when JSON Schema default is set, default: true). /// public bool GenerateDefaultValues { get; set; } = true; - + /// /// Gets or sets a value indicating whether named/referenced any schemas should be inlined or generated as class. /// @@ -163,5 +163,5 @@ public class CodeGeneratorSettings /// Gets or sets a custom . /// [JsonIgnore] - public IPropertyNameGenerator? PropertyNameGenerator { get; set; }// = new CustomCSharpPropertyNameGenerator(); + public IPropertyNameGenerator? PropertyNameGenerator { get; set; } } diff --git a/src/Refitter.Tests/SerializerTests.cs b/src/Refitter.Tests/SerializerTests.cs index c199cfb67..b20bc0e9d 100644 --- a/src/Refitter.Tests/SerializerTests.cs +++ b/src/Refitter.Tests/SerializerTests.cs @@ -40,7 +40,7 @@ public void Deserialize_Is_Case_Insensitive( { var jsonProperty = "\"" + property.Name + "\""; json = json.Replace( - jsonProperty, + jsonProperty, jsonProperty.ToUpperInvariant()); } @@ -51,6 +51,4 @@ public void Deserialize_Is_Case_Insensitive( .Excluding(settings => settings.ParameterNameGenerator) .Excluding(settings => settings.CodeGeneratorSettings.PropertyNameGenerator)); } - - }