-
-
Notifications
You must be signed in to change notification settings - Fork 160
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 #528 from juwens/feature/property-name-by-member-info
added ability to use a custom name resolving logic PropertyNameResolvingMethods so users can provide their custom …
- Loading branch information
Showing
5 changed files
with
163 additions
and
16 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
using NUnit.Framework; | ||
|
||
using static Json.Schema.Generation.Tests.AssertionExtensions; | ||
|
||
namespace Json.Schema.Generation.Tests; | ||
|
||
public class PropertyNameResolverTests | ||
{ | ||
class Target | ||
{ | ||
[JsonPropertyName("JsonName")] | ||
public string PropertyThatNeeds_Changing { get; set; } | ||
} | ||
|
||
class TargetWithOutJsonPropertyName | ||
{ | ||
public string PropertyThatNeeds_Changing { get; set; } | ||
} | ||
|
||
public static IEnumerable<TestCaseData> TestCases | ||
{ | ||
get | ||
{ | ||
yield return new TestCaseData(typeof(Target), PropertyNameResolvers.AsDeclared, "PropertyThatNeeds_Changing"); | ||
yield return new TestCaseData(typeof(Target), PropertyNameResolvers.CamelCase, "propertyThatNeedsChanging"); | ||
yield return new TestCaseData(typeof(Target), PropertyNameResolvers.PascalCase, "PropertyThatNeedsChanging"); | ||
yield return new TestCaseData(typeof(Target), PropertyNameResolvers.KebabCase, "property-that-needs-changing"); | ||
yield return new TestCaseData(typeof(Target), PropertyNameResolvers.UpperKebabCase, "PROPERTY-THAT-NEEDS-CHANGING"); | ||
yield return new TestCaseData(typeof(Target), PropertyNameResolvers.SnakeCase, "property_that_needs_changing"); | ||
yield return new TestCaseData(typeof(Target), PropertyNameResolvers.UpperSnakeCase, "PROPERTY_THAT_NEEDS_CHANGING"); | ||
yield return new TestCaseData(typeof(Target), PropertyNameResolvers.ByJsonPropertyName, "JsonName"); | ||
yield return new TestCaseData(typeof(Target), new PropertyNameResolver(static x => "CustomName"), "CustomName"); | ||
yield return new TestCaseData(typeof(TargetWithOutJsonPropertyName), PropertyNameResolvers.ByJsonPropertyName, "PropertyThatNeeds_Changing"); | ||
} | ||
} | ||
|
||
[TestCaseSource(nameof(TestCases))] | ||
public void VerifyNameChanges(Type type, PropertyNameResolver resolver, string expectedName) | ||
{ | ||
var config = new SchemaGeneratorConfiguration | ||
{ | ||
PropertyNameResolver = resolver | ||
}; | ||
var expected = new JsonSchemaBuilder() | ||
.Type(SchemaValueType.Object) | ||
.Properties( | ||
(expectedName, new JsonSchemaBuilder().Type(SchemaValueType.String)) | ||
) | ||
.Build(); | ||
|
||
var actual = new JsonSchemaBuilder().FromType(type, config).Build(); | ||
|
||
AssertEqual(expected, actual); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text.Json.Serialization; | ||
using Humanizer; | ||
|
||
namespace Json.Schema.Generation; | ||
|
||
/// <summary> | ||
/// Declares a property name resolution which is used to provide a property name. | ||
/// </summary> | ||
/// <param name="input">The property.</param> | ||
/// <returns>The property name</returns> | ||
public delegate string? PropertyNameResolver(MemberInfo input); | ||
|
||
/// <summary> | ||
/// Defines a set of predefined property name resolution methods. | ||
/// </summary> | ||
public static class PropertyNameResolvers | ||
{ | ||
/// <summary> | ||
/// Makes no changes. Properties are generated with the name of the property in code. | ||
/// </summary> | ||
public static readonly PropertyNameResolver AsDeclared = x => x.Name; | ||
/// <summary> | ||
/// Property names to camel case (e.g. `camelCase`). | ||
/// </summary> | ||
public static readonly PropertyNameResolver CamelCase = x => x.Name.Camelize(); | ||
/// <summary> | ||
/// Property names to pascal case (e.g. `PascalCase`). | ||
/// </summary> | ||
public static readonly PropertyNameResolver PascalCase = x => x.Name.Pascalize(); | ||
/// <summary> | ||
/// Property names to snake case (e.g. `Snake_Case`). | ||
/// </summary> | ||
public static readonly PropertyNameResolver SnakeCase = x => x.Name.Underscore(); | ||
/// <summary> | ||
/// Property names to lower snake case (e.g. `lower_snake_case`). | ||
/// </summary> | ||
public static readonly PropertyNameResolver LowerSnakeCase = x => x.Name.Underscore().ToLowerInvariant(); | ||
/// <summary> | ||
/// Property names to upper snake case (e.g. `UPPER_SNAKE_CASE`). | ||
/// </summary> | ||
public static readonly PropertyNameResolver UpperSnakeCase = x => x.Name.Underscore().ToUpperInvariant(); | ||
/// <summary> | ||
/// Property names to kebab case (e.g. `Kebab-Case`). | ||
/// </summary> | ||
public static readonly PropertyNameResolver KebabCase = x => x.Name.Kebaberize(); | ||
/// <summary> | ||
/// Property names to upper kebab case (e.g. `UPPER-KEBAB-CASE`). | ||
/// </summary> | ||
public static readonly PropertyNameResolver UpperKebabCase = x => x.Name.Kebaberize().ToUpperInvariant(); | ||
/// <summary> | ||
/// Property name is read from <see cref="JsonPropertyNameAttribute"/>. | ||
/// </summary> | ||
public static readonly PropertyNameResolver ByJsonPropertyName = x => x.GetCustomAttributes<JsonPropertyNameAttribute>().FirstOrDefault()?.Name /* TODO: what do we do with the nullable here? */; | ||
} |
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