By dries on
Change record status:
Draft (View all draft change records)
Project:
Introduced in branch:
main
Introduced in version:
12.1.0
Issue links:
Description:
The serializer's getJsonSchema() now describes lists (typed data).
Before, it returned just a $comment for a list, which matches any value in JSON Schema.
Example, a required list of names whose item is optional and has a label:
$definition = ListDataDefinition::create('string')
->setRequired(TRUE)
->setItemDefinition(DataDefinition::create('string')->setLabel(new TranslatableMarkup('Name')));
$data = \Drupal::typedDataManager()->create($definition);
$schema = \Drupal::service('serializer')->getJsonSchema($data, []);
$schema is then, encoded as JSON:
{"type":"array","items":{"oneOf":[{"type":"string"},{"type":"null"}],"title":"Name"},"minItems":1}
What each part comes from:
- "title" is the item definition's label. An item with no label gets no title.
- "minItems": 1 is added for a required list, because validation treats an empty required list as NULL and rejects it. #3625989: NotNullConstraintValidator conflates "required" with "non-empty" for lists and complex data; only config schema is exempted proposes letting a required list allow an empty value, and such a list would then get no minItems.
Validation also treats a required list of optional items that are all empty, like [null] or [[]], as NULL and rejects it. The schema accepts that list, because it has items.
Impacts:
Module developers