linkml icon indicating copy to clipboard operation
linkml copied to clipboard

JsonSchemaGenerator adds unnecessary null types to optional fields

Open mahdanoura opened this issue 7 months ago • 0 comments

Describe the bug A clear and concise description of what the bug is (including the error message that is printed, if any)

The JsonSchemaGenerator adds "null" as an allowed type for all optional fields, even when this isn't desired. This manifests in two ways:

  1. For simple types, it creates arrays like: "type": ["string", "null"]
  2. For references, it adds null options in anyOf/oneOf constructs:
"anyOf": [
    { "$ref": "#/$defs/SomeType" },
    { "type": "null" }
]

This behaviour makes the generated schemas more verbose and can cause issues when integrating with systems that expect simpler schemas.

Version of LinkML you are using 1.7.0

Please provide a schema (and if applicable, a data file) that replicates the issue

Here's a minimal example that demonstrates the issue:

id: http://example.org/test
name: test
imports:
  - linkml:types

classes:
  Person:
    attributes:
      id:
        description: Required field
        identifier: true
        range: string
      name:
        description: Optional field
        range: string
      age:
        description: Optional field
        range: integer

This generates a JSON Schema where both optional fields allow null:

{
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": ["string", "null"]
    },
    "age": {
      "type": ["integer", "null"]
    }
  },
  "required": ["id"]
}

The desired output would be:

{
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  },
  "required": ["id"]
}

mahdanoura avatar Jun 02 '25 13:06 mahdanoura