Skip to content

Commit

Permalink
Add code for generating range for different formats
Browse files Browse the repository at this point in the history
  • Loading branch information
rbergheim committed Aug 1, 2024
1 parent 9bf8f69 commit ba4260e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
37 changes: 37 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/ValueGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,43 @@ public async Task When_schema_contains_range_then_code_is_correctly_generated()
Assert.Contains("[System.ComponentModel.DataAnnotations.Range(2, int.MaxValue)]", code);
}

[Theory]
[InlineData("integer", JsonFormatStrings.Integer, "1, int.MaxValue")]
[InlineData("integer", JsonFormatStrings.Long, "1L, long.MaxValue")]
[InlineData("integer", JsonFormatStrings.ULong, "1UL, ulong.MaxValue")]
[InlineData("number", JsonFormatStrings.Float, "1F, float.MaxValue")]
[InlineData("number", JsonFormatStrings.Double, "1D, double.MaxValue")]
[InlineData("number", JsonFormatStrings.Decimal, "1M, decimal.MaxValue")]
public async Task When_schema_contains_range_and_format_then_code_is_correctly_generated(string propertyType,
string propertyFormat, string expectedRange)
{
/// Arrange
var json = $$"""
{
"type": "object",
"properties": {
"pageSize": {
"type": "{{propertyType}}",
"format": "{{propertyFormat}}",
"minimum": 1
},
}
}
""";
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.Swagger2
});
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains($"[System.ComponentModel.DataAnnotations.Range({expectedRange})]", code);
}

[Fact]
public async Task When_property_is_integer_and_no_format_is_available_then_default_value_is_int32()
{
Expand Down
2 changes: 2 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public override string GetNumericValue(JsonObjectType type, object value, string
return Convert.ToInt32(value, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
case JsonFormatStrings.Long:
return Convert.ToInt64(value, CultureInfo.InvariantCulture) + "L";
case JsonFormatStrings.ULong:
return Convert.ToUInt64(value, CultureInfo.InvariantCulture) + "UL";
case JsonFormatStrings.Double:
return ConvertNumberToString(value) + "D";
case JsonFormatStrings.Float:
Expand Down
34 changes: 30 additions & 4 deletions src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using NJsonSchema.Annotations;
using System.Globalization;
using System.Linq;
using NJsonSchema.CodeGeneration.Models;

namespace NJsonSchema.CodeGeneration.CSharp.Models
Expand All @@ -18,6 +19,16 @@ public class PropertyModel : PropertyModelBase
private readonly JsonSchemaProperty _property;
private readonly CSharpGeneratorSettings _settings;
private readonly CSharpTypeResolver _resolver;
private static readonly string[] RangeFormats =
[
JsonFormatStrings.Integer,
JsonFormatStrings.Float,
JsonFormatStrings.Double,
JsonFormatStrings.Long,
JsonFormatStrings.ULong,
JsonFormatStrings.Decimal
];


/// <summary>Initializes a new instance of the <see cref="PropertyModel"/> class.</summary>
/// <param name="classTemplateModel">The class template model.</param>
Expand Down Expand Up @@ -139,8 +150,8 @@ public string RangeMinimumValue
{
var schema = _property.ActualSchema;
var propertyFormat = GetSchemaFormat(schema);
var format = propertyFormat == JsonFormatStrings.Integer ? JsonFormatStrings.Integer : JsonFormatStrings.Double;
var type = propertyFormat == JsonFormatStrings.Integer ? "int" : "double";
var format = GetRangeFormat(propertyFormat);
var type = GetRangeType(propertyFormat);

var minimum = schema.Minimum;
if (minimum.HasValue && schema.IsExclusiveMinimum)
Expand Down Expand Up @@ -171,8 +182,8 @@ public string RangeMaximumValue
{
var schema = _property.ActualSchema;
var propertyFormat = GetSchemaFormat(schema);
var format = propertyFormat == JsonFormatStrings.Integer ? JsonFormatStrings.Integer : JsonFormatStrings.Double;
var type = propertyFormat == JsonFormatStrings.Integer ? "int" : "double";
var format = GetRangeFormat(propertyFormat);
var type = GetRangeType(propertyFormat);

var maximum = schema.Maximum;
if (maximum.HasValue && schema.IsExclusiveMaximum)
Expand Down Expand Up @@ -311,5 +322,20 @@ public bool RenderRegularExpressionAttribute

return schema.Format;
}

private static string GetRangeFormat(string? propertyFormat) =>
RangeFormats.Contains(propertyFormat) ? propertyFormat! : JsonFormatStrings.Double;

private static string GetRangeType(string? propertyFormat) =>
propertyFormat switch
{
JsonFormatStrings.Integer => "int",
JsonFormatStrings.Float => "float",
JsonFormatStrings.Double => "double",
JsonFormatStrings.Long => "long",
JsonFormatStrings.ULong => "ulong",
JsonFormatStrings.Decimal => "decimal",
_ => "double",
};
}
}

0 comments on commit ba4260e

Please sign in to comment.