Problem/Motivation
Since Drupal 11.2.0, released in June 2025, primitive data types can describe their normalized output as JSON Schema. See this change record at https://www.drupal.org/node/3424710 for context.
However, core's serializer can't describe a list yet. For typed data of type list, getJsonSchema() returns a $comment, a JSON Schema keyword for notes.
Validators ignore $comments (https://json-schema.org/draft/2020-12/json-schema-core#section-8.3): nothing gets asserted, and any data is accepted.
This is the list counterpart of #3627318: ComplexDataNormalizer does not provide a JSON Schema for complex data, which does the same for maps.
Steps to reproduce
$definition = ListDataDefinition::create('string')
->setItemDefinition(DataDefinition::create('string')->setRequired(TRUE));
$data = \Drupal::typedDataManager()->create($definition);
$schema = \Drupal::service('serializer')->getJsonSchema($data, []);Returns:
// ['$comment' => 'No schema is defined for property of type Drupal\Core\TypedData\Plugin\DataType\ItemList. ...']
Desired output:
{
"type": "array",
"items": {
"type": "string"
}
}Proposed resolution
We added a new normalizer, ListSchemaNormalizer (service serializer.normalizer.list_schema) that builds the schema that getJsonSchema() returns for a list.
With the new ListSchemaNormalizer, the schema describes the list's normalized data per the example above. It supports the following features:
- Its
itemshave the schema of the list's item definition, so an optional primitive item, like a string, also accepts null. - A list of lists gets a nested schema.
- A required list gets
"minItems": 1. Validation also rejects a required list of optional items that are all empty, like [null] or [[]], which the schema accepts. #3625989: NotNullConstraintValidator conflates "required" with "non-empty" for lists and complex data; only config schema is exempted proposes letting a list keep NULL and empty apart.
Subclasses of ItemList, like field item lists, are unchanged and get the schema they got before. Their items are field items, which #3627318: ComplexDataNormalizer does not provide a JSON Schema for complex data leaves unchanged too. This could be something to work on in a follow-up issue.
Remaining tasks
Review.
User interface changes
None.
Introduced terminology
None.
API changes
A new normalizer, ListSchemaNormalizer.
See https://www.drupal.org/node/3641093.
Data model changes
None.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3627328
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
mglamanComment #3
mglamanComment #5
dries commentedI opened a merge request for this. I built this the same way as the map work in #3627318: ComplexDataNormalizer does not provide a JSON Schema for complex data. I applied the feedback from #3627318 to this MR, and followed the same patterns. You might want to read/review that issue first.
To check the schema, you can run this on a site with Drush or
dr, before and after applying the merge request:ddev dr php:eval '$definition = \Drupal\Core\TypedData\ListDataDefinition::create("string")->setItemDefinition(\Drupal\Core\TypedData\DataDefinition::create("string")->setRequired(TRUE)); $list = \Drupal::typedDataManager()->create($definition, ["Ada", NULL]); echo json_encode(\Drupal::service("serializer")->getJsonSchema($list, []), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), "\n";'Before the MR, this prints the JSON Spec fallback:
According to thee JSON Schema spec,
$commenthas no effect on validation (https://json-schema.org/draft/2020-12/json-schema-core#section-8.3). This means validation is skipped and any value is accepted, so not ideal.With this MR, Drupal prints a proper JSON Schema for the list:
The following features are supported:
itemsbecomes{"oneOf": [{"type": "string"}, {"type": "null"}]}."minItems": 1.The following is not supported:
The MR has a helper function,
assertTypedDataMatchesJsonSchema(), that I copied verbatim from #3627318. If #3627318 lands first, we'll have to remove it from this MR. That is easy.I'm happy to work on the issue summary and change record next.
Disclaimer: I built this with the help of an LLM but reviewed everything extensively.
Comment #7
dries commentedUpdated the issue summary.
Comment #8
dries commentedComment #9
mglamanComment #10
d34dman commentedFlowDrop needs array schemas with typed
itemsbefore any value exists, so this is the piece we would use.Two additions to @mglaman's review:
1. One more case for the minItems @todo
A required list with optional items that holds
[NULL].ItemList::isEmpty()is true, so NotNull fails.But
minItems: 1with nullable items accepts[null]. The nested-list provider covers required items (['Ada', NULL]), not this case.2. Labels
the item's
titleanddescriptionaren't carried over, unlike in the map MR (!17844).Comment #11
dries commentedAdded a change record at https://www.drupal.org/node/3641093 and updated the issue summary.
Back to 'Needs review'.
Comment #12
mglamanI left notes on the MR for @d34dman's feedback in #10.
The change record also needs to say what the schema has beyond
"type": "array":"minItems": 1, which #3625989 will change for a required list that may be empty.{"oneOf": [{"type": "string"}, {"type": "null"}]}. That's why the example sets the item definition as required.titleofitems, once that's in the MR.Setting to Needs work.
Generated with the help of an LLM.
Comment #13
dries commentedThanks @mglaman. Both points on the MR are in. I also updated the change record and issue summary.
Setting back to "Needs review".
Comment #14
dries commentedComment #15
mglamanRe-reviewed the MR. Nothing left to do. Change record looks good to me!
Comment #16
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. The merge request has merge conflicts and cannot be merged. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #17
dries commentedRebased on main. There were two conflicts: (1) #3623409: Add the "ShareEnvironment" attribute to all Kernel tests adding
ShareEnvironmentto every kernel test and (2) #3640003: Typed data docblocks don't allow an integer list delta making some of the same docblock fixes. The code is otherwise unchanged since @mglaman's RTBC in #15, so I'm setting it back to RTBC.