Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Refitter.Core/CSharpClientGeneratorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -41,6 +41,11 @@ public CustomCSharpClientGenerator Create()
}
};

if (settings.ParameterNameGenerator != default)
{
csharpClientGeneratorSettings.ParameterNameGenerator = settings.ParameterNameGenerator;
}

csharpClientGeneratorSettings.CSharpGeneratorSettings.TemplateFactory = new CustomTemplateFactory(
csharpClientGeneratorSettings.CSharpGeneratorSettings,
[
Expand Down
15 changes: 12 additions & 3 deletions src/Refitter.Core/Settings/CodeGeneratorSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Refitter.Core;
using System.Text.Json.Serialization;
using NJsonSchema.CodeGeneration;

namespace Refitter.Core;

/// <summary>
/// CSharp code generator settings
Expand Down Expand Up @@ -145,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).
/// </summary>
public bool GenerateDefaultValues { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether named/referenced any schemas should be inlined or generated as class.
/// </summary>
Expand All @@ -155,4 +158,10 @@ public class CodeGeneratorSettings
/// Gets or sets the excluded type names (must be defined in an import or other namespace).
/// </summary>
public string[] ExcludedTypeNames { get; set; } = Array.Empty<string>();
}

/// <summary>
/// Gets or sets a custom <see cref="IPropertyNameGenerator"/>.
/// </summary>
[JsonIgnore]
public IPropertyNameGenerator? PropertyNameGenerator { get; set; }
}
4 changes: 4 additions & 0 deletions src/Refitter.Core/Settings/RefitGeneratorSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using NSwag.CodeGeneration;

namespace Refitter.Core;

Expand Down Expand Up @@ -226,4 +227,7 @@ public class RefitGeneratorSettings
/// See https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism for more information
/// </summary>
public bool UsePolymorphicSerialization { get; set; }

[JsonIgnore]
public IParameterNameGenerator? ParameterNameGenerator { get; set; }
}
15 changes: 10 additions & 5 deletions src/Refitter.Tests/SerializerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Atc.Test;
using System.Reflection;
using Atc.Test;
using FluentAssertions;
using Refitter.Core;
using Xunit;
Expand All @@ -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<RefitGeneratorSettings>(json)
.Should()
.BeEquivalentTo(settings);
.BeEquivalentTo(settings, options => options
.Excluding(settings => settings.ParameterNameGenerator)
.Excluding(settings => settings.CodeGeneratorSettings.PropertyNameGenerator));
}

[Theory, AutoNSubstituteData]
Expand All @@ -37,13 +40,15 @@ public void Deserialize_Is_Case_Insensitive(
{
var jsonProperty = "\"" + property.Name + "\"";
json = json.Replace(
jsonProperty,
jsonProperty,
jsonProperty.ToUpperInvariant());
}

Serializer
.Deserialize<RefitGeneratorSettings>(json)
.Should()
.BeEquivalentTo(settings);
.BeEquivalentTo(settings, options => options
.Excluding(settings => settings.ParameterNameGenerator)
.Excluding(settings => settings.CodeGeneratorSettings.PropertyNameGenerator));
}
}