RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.38k stars 532 forks source link

Array of nullable enums has IsStringEnumArray set to false (C#) #1660

Open bollhals opened 8 months ago

bollhals commented 8 months ago

Hey

I have a schema which I use to generate C# classes out of it. One issue that I observed was that a schema with an array property of a nullable string enum does not set the ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter), due to IsStringEnumArray returning false, which it does not when I remove the nullable part.

Examples:

...
"properties": {
    "Nullable": {
      "type": "array",
      "minItems": 4,
      "maxItems": 4,
      "items": {
        "oneOf": [
          {
            "type": "null"
          },
          {
            "$ref": "MyEnumDefinition.json"
          }
        ]
      }
    },
    "NotNullable": {
      "type": "array",
      "minItems": 4,
      "maxItems": 4,
      "items": {
        "$ref": "MyEnumDefinition.json"
      }
    }
}
...

In the search of what's wrong, I stumbled upon its sibling (IsStringEnum) which interesstingly is slightly differently implemented.

IsStringEnum => _property.ActualTypeSchema.IsEnumeration && _property.ActualTypeSchema.Type.IsString() IsStringEnumArray => _property.ActualTypeSchema.Item.ActualSchema.IsEnumeration && _property.ActualTypeSchema.Item.ActualSchema.Type.IsString()

So the first one uses the ActualTypeSchema while the 2nd uses ActualSchema for the item. When I debugged it, I saw that if the latter would use the ActualTypeSchema of the Item, my usecase would work as expected.

Any idea why ActualSchema is used instead of ActualTypeSchema?