-
-
Notifications
You must be signed in to change notification settings - Fork 48
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 #188 from christianhelle/code-generator-settings
Add support for customizable type and contract generator settings
- Loading branch information
Showing
20 changed files
with
921 additions
and
170 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
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
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,36 +1,79 @@ | ||
using System.Diagnostics; | ||
|
||
using NJsonSchema.CodeGeneration.CSharp; | ||
|
||
using NSwag; | ||
using NSwag.CodeGeneration.CSharp; | ||
|
||
namespace Refitter.Core; | ||
|
||
internal class CSharpClientGeneratorFactory | ||
internal class CSharpClientGeneratorFactory(RefitGeneratorSettings settings, OpenApiDocument document) | ||
{ | ||
private readonly RefitGeneratorSettings settings; | ||
private readonly OpenApiDocument document; | ||
|
||
public CSharpClientGeneratorFactory(RefitGeneratorSettings settings, OpenApiDocument document) | ||
public CustomCSharpClientGenerator Create() | ||
{ | ||
this.settings = settings; | ||
this.document = document; | ||
var generator = new CustomCSharpClientGenerator( | ||
document, | ||
new CSharpClientGeneratorSettings | ||
{ | ||
GenerateClientClasses = false, | ||
GenerateDtoTypes = true, | ||
GenerateClientInterfaces = false, | ||
GenerateExceptionClasses = false, | ||
CodeGeneratorSettings = | ||
{ | ||
PropertyNameGenerator = new CustomCSharpPropertyNameGenerator(), | ||
}, | ||
CSharpGeneratorSettings = | ||
{ | ||
Namespace = settings.Namespace, | ||
JsonLibrary = CSharpJsonLibrary.SystemTextJson, | ||
TypeAccessModifier = settings.TypeAccessibility.ToString().ToLowerInvariant(), | ||
} | ||
}); | ||
|
||
MapCSharpGeneratorSettings( | ||
settings.CodeGeneratorSettings, | ||
generator.Settings.CSharpGeneratorSettings); | ||
|
||
return generator; | ||
} | ||
|
||
public CustomCSharpClientGenerator Create() => | ||
new(document, new CSharpClientGeneratorSettings | ||
private static void MapCSharpGeneratorSettings( | ||
CodeGeneratorSettings? source, | ||
CSharpGeneratorSettings destination) | ||
{ | ||
if (source is null) | ||
{ | ||
GenerateClientClasses = false, | ||
GenerateDtoTypes = true, | ||
GenerateClientInterfaces = false, | ||
GenerateExceptionClasses = false, | ||
CodeGeneratorSettings = | ||
return; | ||
} | ||
|
||
var defaultInstance = new CodeGeneratorSettings(); | ||
foreach (var property in source.GetType().GetProperties()) | ||
{ | ||
if (property.PropertyType != typeof(string) && | ||
property.PropertyType != typeof(bool)) | ||
{ | ||
PropertyNameGenerator = new CustomCSharpPropertyNameGenerator(), | ||
}, | ||
CSharpGeneratorSettings = | ||
continue; | ||
} | ||
|
||
var value = property.GetValue(source); | ||
if (value == null) | ||
{ | ||
Namespace = settings.Namespace, | ||
JsonLibrary = CSharpJsonLibrary.SystemTextJson, | ||
TypeAccessModifier = settings.TypeAccessibility.ToString().ToLowerInvariant() | ||
continue; | ||
} | ||
}); | ||
|
||
if (value.Equals(property.GetValue(defaultInstance))) | ||
{ | ||
continue; | ||
} | ||
|
||
var settingsProperty = destination.GetType().GetProperty(property.Name); | ||
if (settingsProperty == null) | ||
{ | ||
continue; | ||
} | ||
|
||
settingsProperty.SetValue(destination, value); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Text.Json; | ||
|
||
using JsonSerializerOptions = System.Text.Json.JsonSerializerOptions; | ||
|
||
namespace Refitter.Core; | ||
|
||
/// <summary> | ||
/// Provides methods for serializing and deserializing objects to and from JSON. | ||
/// This serializer is configured to be case-insensitive. | ||
/// </summary> | ||
public static class Serializer | ||
{ | ||
private static readonly JsonSerializerOptions JsonSerializerOptions = new() | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
/// <summary> | ||
/// Deserializes the JSON string to the specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The type to deserialize the JSON string to.</typeparam> | ||
/// <param name="json">The JSON string to deserialize.</param> | ||
/// <returns>The deserialized object of type T.</returns> | ||
public static T Deserialize<T>(string json) => | ||
JsonSerializer.Deserialize<T>(json, JsonSerializerOptions)!; | ||
|
||
/// <summary> | ||
/// Serializes the specified object to a JSON string. | ||
/// </summary> | ||
/// <param name="any">The object to serialize.</param> | ||
/// <returns>The JSON string representation of the object.</returns> | ||
public static string Serialize(object any) => | ||
JsonSerializer.Serialize(any, JsonSerializerOptions); | ||
} |
Oops, something went wrong.