-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add customizable number types in code generation
This commit introduces an update enabling the customization of number types during code generation. It introduces settings to customize the .NET type of number, float, double, and decimal data types during code generation in 'CSharpGeneratorSettings.cs' and updates 'CSharpTypeResolver.cs' to use these settings. This change allows better user control over how the number types get translated, making the code generation more versatile. Unit tests for different scenarios are included in the new 'NumberTests.cs'.
- Loading branch information
Jimmy
committed
Nov 27, 2023
1 parent
63ee615
commit 0828652
Showing
3 changed files
with
308 additions
and
20 deletions.
There are no files selected for viewing
263 changes: 263 additions & 0 deletions
263
src/NJsonSchema.CodeGeneration.CSharp.Tests/NumberTests.cs
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,263 @@ | ||
using System.Threading.Tasks; | ||
using NJsonSchema.CodeGeneration.CSharp; | ||
using Xunit; | ||
|
||
namespace NJsonSchema.CodeGeneration.Tests.CSharp; | ||
|
||
public class NumberTests | ||
{ | ||
[Fact] | ||
public async Task When_number_has_no_format_then_default_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"" | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public double Amount", code); | ||
} | ||
|
||
[Fact] | ||
public async Task When_number_has_decimal_format_then_decimal_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"", | ||
""format"": ""decimal"" | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public decimal Amount", code); | ||
} | ||
|
||
[Fact] | ||
public async Task When_number_has_double_format_then_double_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"", | ||
""format"": ""double"" | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public double Amount", code); | ||
} | ||
|
||
[Fact] | ||
public async Task When_number_has_float_format_then_float_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"", | ||
""format"": ""float"" | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public float Amount", code); | ||
} | ||
|
||
[Fact] | ||
public async Task When_number_type_setting_is_defined_then_setting_type_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"" | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings | ||
{ | ||
NumberType = "customNumberType" | ||
}); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public customNumberType Amount", code); | ||
} | ||
|
||
[Fact] | ||
public async Task When_number_type_setting_is_whitespace_then_double_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"" | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings | ||
{ | ||
NumberType = " \t\n" | ||
}); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public double Amount", code); | ||
} | ||
|
||
[Fact] | ||
public async Task When_number_type_setting_is_null_then_double_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"", | ||
""nullable"": true | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings | ||
{ | ||
NumberType = null! | ||
}); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public double? Amount", code); | ||
} | ||
|
||
[Fact] | ||
public async Task When_number_float_type_setting_is_defined_then_setting_type_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"", | ||
""format"":""float"", | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings | ||
{ | ||
NumberFloatType = "customFloatType" | ||
}); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public customFloatType Amount", code); | ||
} | ||
|
||
[Fact] | ||
public async Task When_number_double_type_setting_is_defined_then_setting_type_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"", | ||
""format"":""double"", | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings | ||
{ | ||
NumberDoubleType = "customDoubleType" | ||
}); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public customDoubleType Amount", code); | ||
} | ||
|
||
[Fact] | ||
public async Task When_number_decimal_type_setting_is_defined_then_setting_type_is_generated() | ||
{ | ||
//// Arrange | ||
var json = | ||
@"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""amount"" : { | ||
""type"":""number"", | ||
""format"":""decimal"", | ||
} | ||
} | ||
}"; | ||
var schema = await JsonSchema.FromJsonAsync(json); | ||
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings | ||
{ | ||
NumberDecimalType = "customDecimalType" | ||
}); | ||
|
||
//// Act | ||
var code = generator.GenerateFile("MyClass"); | ||
|
||
//// Assert | ||
Assert.Contains("public customDecimalType Amount", code); | ||
} | ||
} |
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