The schema of an optional enum input allows null in its type but not in its enum, so null is rejected
### Problem/Motivation
An optional input with a `Choice` or `AllowedValues` constraint (or a derived `PluginExists` enum) is normalized with a null branch in its type and an `enum` that does not list null. On 11.1+:
```json
"sort_order": {
"oneOf": [{"type": "string"}, {"type": "null"}],
"default": "DESC",
"enum": ["ASC", "DESC"]
}
```
On cores before 11.1 the type is `["string", "null"]` with the same `enum`.
In JSON Schema, `enum` applies to the whole value, beside `oneOf`/`type`. `null` passes the type but fails the enum, so the schema accepts no null at all. The null branch is dead, and the schema contradicts itself.
Tool API itself accepts null for these inputs: Symfony's `ChoiceValidator` skips null, and `validateInputs()` reports no violation for an optional `Choice` input set to NULL. A client that validates against the published schema rejects what the tool would take. mcp_server validates `tools/call` arguments against the input schema (mcp/sdk `CallToolHandler`), so the call fails with JSON-RPC -32602 before Tool API sees it. Seen in an MCP end-to-end pass on Drupal 11.4:
* tool_belt `entity_list` with `{"sort_order": null}`: "Property '/sort_order': Value must be one of the allowed values: "ASC", "DESC"."
* tool_belt `entity_metadata` with `{"entity": "handle:...", "entity_type_id": null, "entity_id": null}`: rejected on `/entity_type_id`, whose enum lists the site's entity types.
* canvas_tools `canvas_publish_auto_saves` with `{"target_type": null, ...}`: rejected on `/target_type`.
Sending null for an input the model does not want to set is ordinary model behavior, and the other optional inputs of the same tools accept it (`entity_list` with `amount`, `offset`, `sort_field` and `fields` all null succeeds).
The code: src/Normalizer/ContextDefinitionNormalizer.php:323-349 sets `enum` from `Choice` and `AllowedValues`, and :350-377 from `PluginExists`. The optional handling at :281-295 adds the null type on the legacy path (core adds the `oneOf` null branch on 11.1+) but leaves `enum` alone. `FixedValue` has the same gap: an optional input gets `const`, which also rejects null.
`InputSchemaMatrixTest` pins the contradictory shape as expected (tests/src/Kernel/Matrix/InputSchemaMatrixTest.php:223-232, `nullableScalar('string') + ['enum' => [...]]`), and `SchemaSampleGeneratorTrait` always picks the non-null branch (tests/src/Traits/SchemaSampleGeneratorTrait.php:44-60), so `AdvertisedSchemaAcceptanceTest` never sends null for an optional input. Neither test notices.
### Steps to reproduce
1. Enable `tool` and `tool_test`.
2. Normalize the input schema of `schema_matrix_tool` and look at `enum_string`:
```php
$tool = \Drupal::service('plugin.manager.tool')->createInstance('schema_matrix_tool');
$schema = \Drupal::service('tool.definition_serializer')->normalizeInputSchema($tool);
$schema['properties']['enum_string'];
```
Observed (11.1+): `{"oneOf": [{"type": "string"}, {"type": "null"}], "enum": ["alpha", "beta", "gamma"], ...}`.
3. Validate `{"enum_string": null}` against the schema with any JSON Schema validator: it fails on `enum`.
4. Set the input to NULL on the tool and call `validateInputs()`: no violation for `enum_string`.
Over MCP: call tool_belt `entity_list` with `{"entity_type_id": "node", "sort_order": null}`.
### Proposed resolution
When the normalizer keeps the null branch for an optional input (ContextDefinitionNormalizer.php:281-295), make the value keywords agree with it:
* If `enum` is set and does not contain null, append `null` to it.
* If `const` is set, replace it with `enum: [<const>, null]`.
When the null branch is removed (NotNull, or NotBlank without allowNull), leave `enum` and `const` as they are. Required inputs are unchanged.
Adding null to `enum` works for both the legacy `type` array and the core `oneOf` shape, so no per-core branching is needed.
### Remaining tasks
* Implement, and update the `enum_string` and `enum_integer` expectations in `InputSchemaMatrixTest`.
* Add a null value for optional inputs to the round-trip property in `AdvertisedSchemaAcceptanceTest`, so a schema that rejects null for an optional input fails a test.
* Check derived enums (`PluginExists`) and any enum a transform subscriber adds after normalization.
### AI usage (if applicable)
* [x] AI Assisted Issue: This issue was generated with AI assistance, but was reviewed and refined by the creator.
* [ ] AI Assisted Code: This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.
* [ ] AI Generated Code: This code was mainly generated by an AI with human guidance, and reviewed, tested, and refined by a human.
* [ ] Vibe Coded: This code was generated by an AI and has only been functionally tested.
issue
GitLab AI Context
Project: project/tool
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/tool/-/raw/1.0.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/tool
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD