Skip to content
Merged
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
39 changes: 39 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,5 +648,44 @@ public async Task When_enum_has_a_format_then_enum_is_generated_with_correct_bas
await VerifyHelper.Verify(code);
CSharpCompiler.AssertCompile(code);
}

[Fact]
public async Task When_nullable_enum_array_then_IsStringEnumArray_is_true()
{
// Arrange - schema with a nullable string enum array (items uses oneOf with null + $ref)
var json = @"
{
""type"": ""object"",
""properties"": {
""colors"": {
""type"": ""array"",
""items"": {
""oneOf"": [
{ ""type"": ""null"" },
{ ""$ref"": ""#/definitions/Color"" }
]
}
}
},
""definitions"": {
""Color"": {
""type"": ""string"",
""enum"": [""Red"", ""Green"", ""Blue""]
}
}
}";
var schema = await JsonSchema.FromJsonAsync(json);

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

// Assert - should have StringEnumConverter for the nullable enum array
await VerifyHelper.Verify(code);
CSharpCompiler.AssertCompile(code);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//----------------------
// <auto-generated>
// </auto-generated>
//----------------------


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

public enum Color
{

[System.Runtime.Serialization.EnumMember(Value = @"Red")]
Red = 0,


[System.Runtime.Serialization.EnumMember(Value = @"Green")]
Green = 1,


[System.Runtime.Serialization.EnumMember(Value = @"Blue")]
Blue = 2,


}

public partial class MyClass
{

[Newtonsoft.Json.JsonProperty("colors", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public System.Collections.Generic.ICollection<Color?> Colors { 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; }
}

}
}
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration/Models/PropertyModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ protected PropertyModelBase(
public bool IsStringEnumArray =>
_property.ActualTypeSchema.IsArray &&
_property.ActualTypeSchema.Item != null &&
_property.ActualTypeSchema.Item.ActualSchema.IsEnumeration &&
_property.ActualTypeSchema.Item.ActualSchema.Type.IsString();
_property.ActualTypeSchema.Item.ActualTypeSchema.IsEnumeration &&
_property.ActualTypeSchema.Item.ActualTypeSchema.Type.IsString();

/// <summary>Gets the property extension data.</summary>
public IDictionary<string, object?>? ExtensionData => _property.ExtensionData;
Expand Down