Permalink
Browse files

Addd float format type, improved default value generators, closes #595

  • Loading branch information...
RSuter committed Jan 10, 2018
1 parent ee54251 commit 465fb67e03dd49f504e800a9965940c37667011a
@@ -31,5 +31,38 @@ public async Task When_schema_contains_range_then_code_is_correctly_generated()
//// Assert
Assert.Contains("[System.ComponentModel.DataAnnotations.Range(2, int.MaxValue)]", code);
}
[Fact]
public async Task When_property_is_integer_and_no_format_is_available_then_default_value_is_int32()
{
/// Arrange
var json = @"{
""type"": ""object"",
""properties"": {
""pageSize"": {
""type"": ""integer"",
""default"": 10,
""minimum"": 1
},
""pagingSize"": {
""type"": ""integer"",
""default"": 5,
""minimum"": 1
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);
/// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.Swagger2
});
var code = generator.GenerateFile("MyClass");
/// Assert
Assert.Contains("public int? PageSize { get; set; } = 10;", code);
}
}
}
@@ -6,9 +6,6 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System;
using System.Globalization;
namespace NJsonSchema.CodeGeneration.CSharp
{
/// <summary>Converts the default value to a TypeScript identifier.</summary>
@@ -45,46 +42,40 @@ public override string GetDefaultValue(JsonSchema4 schema, bool allowsNull, stri
return "new " + targetType + "()";
}
}
return value;
}
/// <summary>Converts the default value to a C# number literal. </summary>
/// <param name="type">The JSON type.</param>
/// <param name="value">The value to convert.</param>
/// <param name="format">Optional schema format</param>
/// <returns>The C# number literal.</returns>
public override string GetNumericValue(object value, string format)
public override string GetNumericValue(JsonObjectType type, object value, string format)
{
if (string.IsNullOrEmpty(format))
var valueString = ConvertNumberToString(value);
if (valueString != null)
{
if (value is byte) return "(byte)" + ((byte)value).ToString(CultureInfo.InvariantCulture);
if (value is sbyte) return "(sbyte)" + ((sbyte)value).ToString(CultureInfo.InvariantCulture);
if (value is short) return "(short)" + ((short)value).ToString(CultureInfo.InvariantCulture);
if (value is ushort) return "(ushort)" + ((ushort)value).ToString(CultureInfo.InvariantCulture);
if (value is int) return ((int)value).ToString(CultureInfo.InvariantCulture);
if (value is uint) return ((uint)value).ToString(CultureInfo.InvariantCulture) + "U";
if (value is long) return ((long)value).ToString(CultureInfo.InvariantCulture) + "L";
if (value is ulong) return ((ulong)value).ToString(CultureInfo.InvariantCulture) + "UL";
if (value is float) return ((float)value).ToString("r", CultureInfo.InvariantCulture) + "F";
if (value is double) return ((double)value).ToString("r", CultureInfo.InvariantCulture) + "D";
if (value is decimal) return ((decimal)value).ToString(CultureInfo.InvariantCulture) + "M";
return null;
switch (format)
{
case JsonFormatStrings.Byte:
return "(byte)" + valueString;
case JsonFormatStrings.Integer:
return valueString;
case JsonFormatStrings.Long:
return valueString + "L";
case JsonFormatStrings.Double:
return valueString + "D";
case JsonFormatStrings.Float:
return valueString + "F";
case JsonFormatStrings.Decimal:
return valueString + "M";
default:
return type.HasFlag(JsonObjectType.Integer) ? valueString : valueString + "D";
}
}
switch (format)
{
case JsonFormatStrings.Byte:
return "(byte)" + Convert.ToByte(value).ToString(CultureInfo.InvariantCulture);
case JsonFormatStrings.Integer:
return Convert.ToInt64(value).ToString(CultureInfo.InvariantCulture);
case JsonFormatStrings.Long:
return Convert.ToInt64(value).ToString(CultureInfo.InvariantCulture) + "L";
case JsonFormatStrings.Double:
return Convert.ToDouble(value).ToString("r", CultureInfo.InvariantCulture) + "D";
case JsonFormatStrings.Decimal:
return Convert.ToDecimal(value).ToString(CultureInfo.InvariantCulture) + "M";
default:
return null;
}
return null;
}
/// <summary>Gets the enum default value.</summary>
@@ -120,7 +120,7 @@ public string RangeMinimumValue
"double" : "int";
return _property.Minimum.HasValue
? ValueGenerator.GetNumericValue(_property.Minimum.Value, format)
? ValueGenerator.GetNumericValue(_property.Type, _property.Minimum.Value, format)
: type + "." + nameof(double.MinValue);
}
}
@@ -140,7 +140,7 @@ public string RangeMaximumValue
"double" : "int";
return _property.Maximum.HasValue
? ValueGenerator.GetNumericValue(_property.Maximum.Value, format)
? ValueGenerator.GetNumericValue(_property.Type, _property.Maximum.Value, format)
: type + "." + nameof(double.MaxValue);
}
}
@@ -46,6 +46,7 @@ public void When_schema_has_default_value_of_long_it_is_generated_in_CSharp_and_
var schema = new JsonSchema4()
{
Type = JsonObjectType.Integer,
Format = JsonFormatStrings.Long,
Default = 6000000000L
};
var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "long", "long", true);
@@ -56,6 +57,45 @@ public void When_schema_has_default_value_of_long_it_is_generated_in_CSharp_and_
Assert.Equal("6000000000", typescriptValue);
}
[Fact]
public void When_schema_has_default_value_of_double_it_is_generated_in_CSharp_and_TypeScript_correctly()
{
//// Arrange
//// Act
var schema = new JsonSchema4()
{
Type = JsonObjectType.Number,
Format = JsonFormatStrings.Double,
Default = 1234.567F
};
var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "double", "double", true);
var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "double", "double", true);
//// Assert
Assert.Equal("1234.567D", csharpValue);
Assert.Equal("1234.567", typescriptValue);
}
[Fact]
public void When_schema_has_default_value_of_double_without_format_it_is_generated_in_CSharp_and_TypeScript_correctly()
{
//// Arrange
//// Act
var schema = new JsonSchema4()
{
Type = JsonObjectType.Number,
Default = 1234.567F
};
var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "double", "double", true);
var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "double", "double", true);
//// Assert
Assert.Equal("1234.567D", csharpValue);
Assert.Equal("1234.567", typescriptValue);
}
[Fact]
public void When_schema_has_default_value_of_float_it_is_generated_in_CSharp_and_TypeScript_correctly()
{
@@ -65,6 +105,7 @@ public void When_schema_has_default_value_of_float_it_is_generated_in_CSharp_and
var schema = new JsonSchema4()
{
Type = JsonObjectType.Number,
Format = JsonFormatStrings.Float,
Default = 1234.567F
};
var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "float", "float", true);
@@ -6,8 +6,6 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System.Globalization;
namespace NJsonSchema.CodeGeneration.TypeScript
{
/// <summary>Converts the default value to a TypeScript identifier.</summary>
@@ -16,7 +14,7 @@ public class TypeScriptValueGenerator : ValueGeneratorBase
/// <summary>Initializes a new instance of the <see cref="TypeScriptValueGenerator"/> class.</summary>
/// <param name="typeResolver">The type resolver.</param>
/// <param name="settings">The settings</param>
public TypeScriptValueGenerator(TypeResolverBase typeResolver, TypeScriptGeneratorSettings settings)
public TypeScriptValueGenerator(TypeResolverBase typeResolver, TypeScriptGeneratorSettings settings)
: base(typeResolver, settings.EnumNameGenerator)
{
}
@@ -46,27 +44,18 @@ public override string GetDefaultValue(JsonSchema4 schema, bool allowsNull, stri
return "new " + targetType + "()";
}
}
return value;
}
/// <summary>Converts the default value to a TypeScript number literal. </summary>
/// <param name="type">The JSON type.</param>
/// <param name="value">The value to convert.</param>
/// <param name="format">Optional schema format</param>
/// <returns>The TypeScript number literal.</returns>
public override string GetNumericValue(object value, string format)
public override string GetNumericValue(JsonObjectType type, object value, string format)
{
if (value is byte) return ((byte)value).ToString(CultureInfo.InvariantCulture);
if (value is sbyte) return ((sbyte)value).ToString(CultureInfo.InvariantCulture);
if (value is short) return ((short)value).ToString(CultureInfo.InvariantCulture);
if (value is ushort) return ((ushort)value).ToString(CultureInfo.InvariantCulture);
if (value is int) return ((int)value).ToString(CultureInfo.InvariantCulture);
if (value is uint) return ((uint)value).ToString(CultureInfo.InvariantCulture);
if (value is long) return ((long)value).ToString(CultureInfo.InvariantCulture);
if (value is ulong) return ((ulong)value).ToString(CultureInfo.InvariantCulture);
if (value is float) return ((float)value).ToString("r", CultureInfo.InvariantCulture);
if (value is double) return ((double)value).ToString("r", CultureInfo.InvariantCulture);
if (value is decimal) return ((decimal)value).ToString(CultureInfo.InvariantCulture);
return null;
return ConvertNumberToString(value);
}
}
}
@@ -6,6 +6,7 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System.Globalization;
using System.Linq;
namespace NJsonSchema.CodeGeneration
@@ -47,16 +48,17 @@ public virtual string GetDefaultValue(JsonSchema4 schema, bool allowsNull, strin
return schema.Default.ToString().ToLowerInvariant();
if (schema.Type.HasFlag(JsonObjectType.Integer) ||
schema.Type.HasFlag(JsonObjectType.Number))
return GetNumericValue(schema.Default, schema.Format);
return GetNumericValue(schema.Type, schema.Default, schema.Format);
return null;
}
/// <summary>Converts the default value to a number literal. </summary>
/// <param name="type">The JSON type.</param>
/// <param name="value">The value to convert.</param>
/// <param name="format">Optional schema format</param>
/// <returns>The number literal.</returns>
public abstract string GetNumericValue(object value, string format);
public abstract string GetNumericValue(JsonObjectType type, object value, string format);
/// <summary>Gets the enum default value.</summary>
/// <param name="schema">The schema.</param>
@@ -74,5 +76,46 @@ protected virtual string GetEnumDefaultValue(JsonSchema4 schema, JsonSchema4 act
return typeName + "." + _enumNameGenerator.Generate(index, enumName, schema.Default, actualSchema);
}
/// <summary>Converts a number to its string representation.</summary>
/// <param name="value">The value.</param>
/// <returns>The string.</returns>
protected string ConvertNumberToString(object value)
{
if (value is byte)
return ((byte)value).ToString(CultureInfo.InvariantCulture);
if (value is sbyte)
return ((sbyte)value).ToString(CultureInfo.InvariantCulture);
if (value is short)
return ((short)value).ToString(CultureInfo.InvariantCulture);
if (value is ushort)
return ((ushort)value).ToString(CultureInfo.InvariantCulture);
if (value is int)
return ((int)value).ToString(CultureInfo.InvariantCulture);
if (value is uint)
return ((uint)value).ToString(CultureInfo.InvariantCulture);
if (value is long)
return ((long)value).ToString(CultureInfo.InvariantCulture);
if (value is ulong)
return ((ulong)value).ToString(CultureInfo.InvariantCulture);
if (value is float)
return ((float)value).ToString("r", CultureInfo.InvariantCulture);
if (value is double)
return ((double)value).ToString("r", CultureInfo.InvariantCulture);
if (value is decimal)
return ((decimal)value).ToString(CultureInfo.InvariantCulture);
return null;
}
}
}
@@ -41,6 +41,9 @@ public static class JsonFormatStrings
/// <summary>Format for a double number. </summary>
public const string Double = "double";
/// <summary>Format for a float number. </summary>
public const string Float = "float";
/// <summary>Format for a decimal number. </summary>
public const string Decimal = "decimal";

0 comments on commit 465fb67

Please sign in to comment.