Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anyOf/oneOf with Discriminated Unions + Null doesn't work #4380

Open
4 tasks done
guilhermedelyra opened this issue Nov 13, 2024 · 1 comment
Open
4 tasks done

anyOf/oneOf with Discriminated Unions + Null doesn't work #4380

guilhermedelyra opened this issue Nov 13, 2024 · 1 comment
Labels
bug needs triage Initial label given, to be assigned correct labels and assigned

Comments

@guilhermedelyra
Copy link

guilhermedelyra commented Nov 13, 2024

Prerequisites

What theme are you using?

core

Version

5.x

Current Behavior

When using a JSON Schema that combines a discriminated union with null using anyOf, the form does not render the discriminated union options correctly. Instead, it only allows selecting the null option, and the expected fields for the other options are not displayed.

Expected Behavior

The form should correctly render the options from the discriminated union alongside the null option, allowing users to select any of the available types or null.

Steps To Reproduce

  1. Use the following JSON Schema in the react-jsonschema-form playground or your environment:
Failing Json Schema

playground

{
  "$defs": {
    "LogitechMouse": {
      "additionalProperties": false,
      "properties": {
        "name": {
          "default": "Logitech Mouse",
          "title": "Name",
          "type": "string"
        },
        "price": {
          "default": 10.0,
          "title": "Price",
          "type": "number"
        },
        "type": {
          "const": "peripheral",
          "default": "peripheral",
          "enum": [
            "peripheral"
          ],
          "title": "Type",
          "type": "string"
        },
        "kind": {
          "const": "mouse",
          "default": "mouse",
          "enum": [
            "mouse"
          ],
          "title": "Kind",
          "type": "string"
        },
        "max_dpi": {
          "default": 600,
          "title": "Max Dpi",
          "type": "integer"
        },
        "brand": {
          "const": "Logitech",
          "default": "Logitech",
          "enum": [
            "Logitech"
          ],
          "title": "Brand",
          "type": "string"
        }
      },
      "title": "LogitechMouse",
      "type": "object"
    },
    "RazerMouse": {
      "additionalProperties": false,
      "properties": {
        "name": {
          "default": "Razer Mouse",
          "title": "Name",
          "type": "string"
        },
        "price": {
          "default": 20.0,
          "title": "Price",
          "type": "number"
        },
        "type": {
          "const": "peripheral",
          "default": "peripheral",
          "enum": [
            "peripheral"
          ],
          "title": "Type",
          "type": "string"
        },
        "kind": {
          "const": "mouse",
          "default": "mouse",
          "enum": [
            "mouse"
          ],
          "title": "Kind",
          "type": "string"
        },
        "max_dpi": {
          "default": 1200,
          "title": "Max Dpi",
          "type": "integer"
        },
        "brand": {
          "const": "Razer",
          "default": "Razer",
          "enum": [
            "Razer"
          ],
          "title": "Brand",
          "type": "string"
        }
      },
      "title": "RazerMouse",
      "type": "object"
    }
  },
  "properties": {
    "components": {
      "anyOf": [
        {
          "discriminator": {
            "mapping": {
              "Logitech": "#/$defs/LogitechMouse",
              "Razer": "#/$defs/RazerMouse"
            },
            "propertyName": "brand"
          },
          "oneOf": [
            {
              "$ref": "#/$defs/LogitechMouse"
            },
            {
              "$ref": "#/$defs/RazerMouse"
            }
          ],
          "title": "AvailableMouses"
        },
        {
          "type": "null"
        }
      ],
      "title": "Components"
    }
  },
  "required": [
    "components"
  ],
  "title": "Computer",
  "type": "object"
}
  1. Render the form (https://rjsf-team.github.io/react-jsonschema-form/)
  2. Attempt to select options other than null for the components field.
  3. Observe that only the null option is selectable, and the discriminated union options are not available.

To prove that the discriminated union is not the problem, here's the same json without combining it with the null option:

Working Json Schema [playground](https://rjsf-team.github.io/react-jsonschema-form/#eyJmb3JtRGF0YSI6eyJjb21wb25lbnRzIjp7Im5hbWUiOiJMb2dpdGVjaCBNb3VzZSIsInByaWNlIjoxMCwidHlwZSI6InBlcmlwaGVyYWwiLCJraW5kIjoibW91c2UiLCJtYXhfZHBpIjo2MDAsImJyYW5kIjoiTG9naXRlY2gifX0sInNjaGVtYSI6eyIkZGVmcyI6eyJMb2dpdGVjaE1vdXNlIjp7ImFkZGl0aW9uYWxQcm9wZXJ0aWVzIjpmYWxzZSwicHJvcGVydGllcyI6eyJuYW1lIjp7ImRlZmF1bHQiOiJMb2dpdGVjaCBNb3VzZSIsInRpdGxlIjoiTmFtZSIsInR5cGUiOiJzdHJpb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0cyIsIm1lcmdlRGVmYXVsdHNJbnRvRm9ybURhdGEiOiJ1c2VGb3JtRGF0YUlmUHJlc2VudCJ9fX0=)
{
  "$defs": {
    "LogitechMouse": {
      "additionalProperties": false,
      "properties": {
        "name": {
          "default": "Logitech Mouse",
          "title": "Name",
          "type": "string"
        },
        "price": {
          "default": 10.0,
          "title": "Price",
          "type": "number"
        },
        "type": {
          "const": "peripheral",
          "default": "peripheral",
          "enum": [
            "peripheral"
          ],
          "title": "Type",
          "type": "string"
        },
        "kind": {
          "const": "mouse",
          "default": "mouse",
          "enum": [
            "mouse"
          ],
          "title": "Kind",
          "type": "string"
        },
        "max_dpi": {
          "default": 600,
          "title": "Max Dpi",
          "type": "integer"
        },
        "brand": {
          "const": "Logitech",
          "default": "Logitech",
          "enum": [
            "Logitech"
          ],
          "title": "Brand",
          "type": "string"
        }
      },
      "title": "LogitechMouse",
      "type": "object"
    },
    "RazerMouse": {
      "additionalProperties": false,
      "properties": {
        "name": {
          "default": "Razer Mouse",
          "title": "Name",
          "type": "string"
        },
        "price": {
          "default": 20.0,
          "title": "Price",
          "type": "number"
        },
        "type": {
          "const": "peripheral",
          "default": "peripheral",
          "enum": [
            "peripheral"
          ],
          "title": "Type",
          "type": "string"
        },
        "kind": {
          "const": "mouse",
          "default": "mouse",
          "enum": [
            "mouse"
          ],
          "title": "Kind",
          "type": "string"
        },
        "max_dpi": {
          "default": 1200,
          "title": "Max Dpi",
          "type": "integer"
        },
        "brand": {
          "const": "Razer",
          "default": "Razer",
          "enum": [
            "Razer"
          ],
          "title": "Brand",
          "type": "string"
        }
      },
      "title": "RazerMouse",
      "type": "object"
    }
  },
  "properties": {
    "components": {
      "discriminator": {
        "mapping": {
          "Logitech": "#/$defs/LogitechMouse",
          "Razer": "#/$defs/RazerMouse"
        },
        "propertyName": "brand"
      },
      "oneOf": [
        {
          "$ref": "#/$defs/LogitechMouse"
        },
        {
          "$ref": "#/$defs/RazerMouse"
        }
      ],
      "title": "AvailableMouses"
    }
  },
  "required": [
    "components"
  ],
  "title": "Computer",
  "type": "object"
}

Environment

-- using the live-playground (same behavior happens running locally) --

OS: Ubuntu 22.04.2 LTS on Windows 10 x86_64
Node: v20.5.0
npm: 9.8.0

Anything else?

Off-topic: I'm using Pydantic (version 2.9.2) to generate those Json-Schemas; here's the code:

Pydantic Code
from pydantic import BaseModel, Field, ConfigDict
from typing import Annotated, Literal, Union

class BaseProduct(BaseModel):
    name: str
    price: float

class BaseHardware(BaseProduct):
    type: Literal["hardware"] = "hardware"

class BasePeripheral(BaseProduct):
    type: Literal["peripheral"] = "peripheral"

class BaseMouseProduct(BasePeripheral):
    kind: Literal["mouse"] = "mouse"
    max_dpi: int

class LogitechMouse(BaseMouseProduct):
    model_config = ConfigDict(extra="forbid")

    brand: Literal["Logitech"] = "Logitech"
    name: str = Field("Logitech Mouse")
    max_dpi: int = Field(600)
    price: float = Field(10.0)

class RazerMouse(BaseMouseProduct):
    model_config = ConfigDict(extra="forbid")

    brand: Literal["Razer"] = "Razer"
    name: str = Field("Razer Mouse")
    max_dpi: int = Field(1200)
    price: float = Field(20.0)

AvailableMouses = Annotated[
    Union[LogitechMouse, RazerMouse],
    Field(title="AvailableMouses", discriminator="brand")
]

class Computer(BaseModel):
    components: Union[AvailableMouses, None]


if __name__ == "__main__":
    print(Computer.schema_json(indent=2))
@guilhermedelyra guilhermedelyra added bug needs triage Initial label given, to be assigned correct labels and assigned labels Nov 13, 2024
@guilhermedelyra
Copy link
Author

Another point i've noticed is:

  • the order of the mappings influence on the fields being selected (nothing wrong with that, just evidenciating this behavior);

Example

  • Valid:
"properties": {
    "components": {
      "discriminator": {
        "mapping": {
          "Logitech": "#/$defs/LogitechMouse",
          "Razer": "#/$defs/RazerMouse"
        },
        "propertyName": "brand"
      },
      "oneOf": [
        {
          "$ref": "#/$defs/LogitechMouse"
        },
        {
          "$ref": "#/$defs/RazerMouse"
        }
      ],
      "title": "AvailableMouses"
    }
  }
  • Invalid (results in buggy interface):
"properties": {
    "components": {
      "discriminator": {
        "mapping": {
          "Logitech": "#/$defs/LogitechMouse",
          "Razer": "#/$defs/RazerMouse" <-------
        },
        "propertyName": "brand"
      },
      "oneOf": [
        {
          "$ref": "#/$defs/RazerMouse"  <--------
        },
        {
          "$ref": "#/$defs/LogitechMouse"
        }
      ],
      "title": "AvailableMouses"
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs triage Initial label given, to be assigned correct labels and assigned
Projects
None yet
Development

No branches or pull requests

1 participant