Skip to content

AI: If string is nullable, data annotations maxLength turns to maxItems #6676

@kzu

Description

@kzu

Description

[MaxLength(n)] is turned into maxLength propertly if the property is also required, but if it's nullable, it's turned into maxItems

Reproduction Steps

Given the following record and function:

record Payload([property: MaxLength(24), Required] string Value);
static void SetPayload(Payload payload) { }

The schema becomes:

 {
  "type": "object",
  "properties": {
    "payload": {
      "type": "object",
      "properties": {
        "value": {
          "type": "string",
          "maxLength": 24
        }
      },
      "required": [
        "value"
      ]
    }
  },
  "required": [
    "payload"
  ]
}

But if the payload value is made nullable: record Payload([property: MaxLength(24)] string? Value);, the schema now becomes:

 {
  "type": "object",
  "properties": {
    "payload": {
      "type": "object",
      "properties": {
        "value": {
          "type": [
            "string",
            "null"
          ],
          "default": null,
          "maxItems": 24
        }
      }
    }
  },
  "required": [
    "payload"
  ]
}

Expected behavior

maxLength is consistently emitted in both cases. AFAIK, using maxItems on a non-array type is invalid (and string isn't, although from .NET's point of view it is a char[].... so perhaps that's the source of the bug?)

Actual behavior

Emits (invalid?) json schema with maxItems for a [string, null] type.

Regression?

No response

Known Workarounds

No response

Configuration

net10 preview 6, MEAI 9.7.0

Other information

No response

Metadata

Metadata

Assignees

Labels

area-aiMicrosoft.Extensions.AI librariesbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions