NJsonSchema icon indicating copy to clipboard operation
NJsonSchema copied to clipboard

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

Open bollhals opened this issue 1 year ago • 0 comments

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?

bollhals avatar Jan 04 '24 14:01 bollhals