Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/NumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,55 @@ public async Task When_number_decimal_type_setting_is_defined_then_setting_type_
// Assert
await VerifyHelper.Verify(code);
}

[Fact]
public async Task When_integer_has_no_format_then_default_int_is_generated()
{
// Arrange
var json =
@"{
""type"": ""object"",
""properties"": {
""count"" : {
""type"":""integer""
}
}
}";
var schema = await JsonSchema.FromJsonAsync(json);
var generator = new CSharpGenerator(schema);

// Act
var code = generator.GenerateFile("MyClass");

// Assert
await VerifyHelper.Verify(code);
CSharpCompiler.AssertCompile(code);
}

[Fact]
public async Task When_integer_type_setting_is_defined_then_setting_type_is_generated()
{
// Arrange
var json =
@"{
""type"": ""object"",
""properties"": {
""count"" : {
""type"":""integer""
}
}
}";
var schema = await JsonSchema.FromJsonAsync(json);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
IntegerType = "long"
});

// Act
var code = generator.GenerateFile("MyClass");

// Assert
await VerifyHelper.Verify(code);
CSharpCompiler.AssertCompile(code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//----------------------
// <auto-generated>
// </auto-generated>
//----------------------


namespace MyNamespace
{
#pragma warning disable // Disable all warnings

public partial class MyClass
{

[Newtonsoft.Json.JsonProperty("count", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public int Count { get; set; }

private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
set { _additionalProperties = value; }
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//----------------------
// <auto-generated>
// </auto-generated>
//----------------------


namespace MyNamespace
{
#pragma warning disable // Disable all warnings

public partial class MyClass
{

[Newtonsoft.Json.JsonProperty("count", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public long Count { get; set; }

private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
set { _additionalProperties = value; }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public CSharpGeneratorSettings()
TimeType = "System.TimeSpan";
TimeSpanType = "System.TimeSpan";

IntegerType = "int";

NumberType = "double";
NumberFloatType = "float";
NumberDoubleType = "double";
Expand Down Expand Up @@ -85,6 +87,9 @@ public CSharpGeneratorSettings()
/// <summary>Gets or sets the time span .NET type (default: 'TimeSpan').</summary>
public string TimeSpanType { get; set; }

/// <summary>Gets or sets the integer .NET type (default: "int"). This setting applies only to integer properties without an explicit format (e.g., not byte, long, or ulong).</summary>
public string IntegerType { get; set; }

/// <summary>Gets or sets the number .NET type (default: "double").</summary>
public string NumberType { get; set; }

Expand Down
15 changes: 8 additions & 7 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private static string ResolveBoolean(bool isNullable)
return isNullable ? "bool?" : "bool";
}

private static string ResolveInteger(JsonSchema schema, bool isNullable, string? typeNameHint)
private string ResolveInteger(JsonSchema schema, bool isNullable, string? typeNameHint)
{
if (schema.Format == JsonFormatStrings.Byte)
{
Expand All @@ -228,11 +228,6 @@ private static string ResolveInteger(JsonSchema schema, bool isNullable, string?
return isNullable ? "long?" : "long";
}

if (schema.Format is JsonFormatStrings.Long or "long")
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep this one, no? Breaking change otherwise.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this branch is duplicated just above, so basically removing one of them doesn't matter?

{
return isNullable ? "long?" : "long";
}

if (schema.Format is JsonFormatStrings.ULong or "ulong")
{
return isNullable ? "ulong?" : "ulong";
Expand All @@ -253,7 +248,13 @@ private static string ResolveInteger(JsonSchema schema, bool isNullable, string?
}
}

return isNullable ? "int?" : "int";
var integerType = Settings.IntegerType;
if (string.IsNullOrWhiteSpace(integerType))
{
integerType = "int";
}

return isNullable ? integerType + "?" : integerType;
}

private string ResolveNumber(JsonSchema schema, bool isNullable)
Expand Down