Skip to content

Commit

Permalink
remove enum handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Suter committed Sep 25, 2023
1 parent cd6d62a commit 589eb9c
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 85 deletions.
23 changes: 0 additions & 23 deletions src/NJsonSchema.NewtonsoftJson/Generation/EnumHandling.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Reflection;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace NJsonSchema.Generation
{
Expand All @@ -20,20 +19,13 @@ public class NewtonsoftJsonSchemaGeneratorSettings : JsonSchemaGeneratorSettings
{
private Dictionary<string, JsonContract> _cachedContracts = new Dictionary<string, JsonContract>();

private EnumHandling _defaultEnumHandling;
private PropertyNameHandling _defaultPropertyNameHandling;
private JsonSerializerSettings _serializerSettings;

/// <summary>Initializes a new instance of the <see cref="JsonSchemaGeneratorSettings"/> class.</summary>
public NewtonsoftJsonSchemaGeneratorSettings()
{
ReflectionService = new NewtonsoftJsonReflectionService();
SerializerSettings = new JsonSerializerSettings();

#pragma warning disable CS0618
DefaultEnumHandling = EnumHandling.Integer;
DefaultPropertyNameHandling = PropertyNameHandling.Default;
#pragma warning restore CS0618
}

/// <summary>Gets or sets the Newtonsoft JSON serializer settings.</summary>
Expand All @@ -47,28 +39,6 @@ public JsonSerializerSettings SerializerSettings
}
}

/// <summary>Gets or sets the default property name handling (default: Default).</summary>
[Obsolete("Use SerializerSettings directly instead. In NSwag.AspNetCore the property is set automatically.")]
public PropertyNameHandling DefaultPropertyNameHandling
{
get => _defaultPropertyNameHandling; set
{
_defaultPropertyNameHandling = value;
UpdateActualContractResolverAndSerializerSettings();
}
}

/// <summary>Gets or sets the default enum handling (default: Integer).</summary>
[Obsolete("Use SerializerSettings directly instead. In NSwag.AspNetCore the property is set automatically.")]
public EnumHandling DefaultEnumHandling
{
get => _defaultEnumHandling; set
{
_defaultEnumHandling = value;
UpdateActualSerializerSettings();
}
}

/// <summary>Gets the contract resolver.</summary>
/// <returns>The contract resolver.</returns>
/// <exception cref="InvalidOperationException">A setting is misconfigured.</exception>
Expand Down Expand Up @@ -101,37 +71,5 @@ public JsonContract ResolveContract(Type type)

return _cachedContracts[key];
}

#pragma warning disable CS0618
private void UpdateActualContractResolverAndSerializerSettings()
{
_cachedContracts = new Dictionary<string, JsonContract>();

if (DefaultPropertyNameHandling == PropertyNameHandling.CamelCase)
{
SerializerSettings.ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy(false, true) };
}
else if (DefaultPropertyNameHandling == PropertyNameHandling.SnakeCase)
{
SerializerSettings.ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy(false, true) };
}

UpdateActualSerializerSettings();
}

private void UpdateActualSerializerSettings()
{
if (DefaultEnumHandling == EnumHandling.String)
{
SerializerSettings.Converters.Add(new StringEnumConverter());
}
else if (DefaultEnumHandling == EnumHandling.CamelCaseString)
{
SerializerSettings.Converters.Add(new StringEnumConverter(true));
}

_cachedContracts.Clear();
}
#pragma warning restore CS0618
}
}

0 comments on commit 589eb9c

Please sign in to comment.