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:

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.

Issue fork drupal-3627328

Command icon 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

mglaman created an issue. See original summary.

mglaman’s picture

Issue summary: View changes
mglaman’s picture

Issue tags: +rosetta

dries made their first commit to this issue’s fork.

dries’s picture

I 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:

{
    "$comment": "No schema is defined for property of type Drupal\\Core\\TypedData\\Plugin\\DataType\\ItemList. See https://www.drupal.org/node/3424710 for information on implementing schemas in your program code."
}

According to thee JSON Schema spec, $comment has 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:

{
    "type": "array",
    "items": {
        "type": "string"
    }
}

The following features are supported:

  • With optional items, items becomes {"oneOf": [{"type": "string"}, {"type": "null"}]}.
  • A list of lists gets a nested schema.
  • A required list also gets "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.

dries’s picture

Issue summary: View changes

Updated the issue summary.

dries’s picture

mglaman’s picture

Status: Needs review » Needs work
d34dman’s picture

FlowDrop needs array schemas with typed items before 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: 1 with nullable items accepts [null]. The nested-list provider covers required items (['Ada', NULL]), not this case.

2. Labels

the item's title and description aren't carried over, unlike in the map MR (!17844).

dries’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs change record

Added a change record at https://www.drupal.org/node/3641093 and updated the issue summary.

Back to 'Needs review'.

mglaman’s picture

Status: Needs review » Needs work

I 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":

  • A required list gets "minItems": 1, which #3625989 will change for a required list that may be empty.
  • An optional item's schema allows null, such as {"oneOf": [{"type": "string"}, {"type": "null"}]}. That's why the example sets the item definition as required.
  • The item's label becomes the title of items, once that's in the MR.

Setting to Needs work.

Generated with the help of an LLM.

dries’s picture

Issue summary: View changes
Status: Needs work » Needs review

Thanks @mglaman. Both points on the MR are in. I also updated the change record and issue summary.

Setting back to "Needs review".

mglaman’s picture

Status: Needs review » Reviewed & tested by the community

Re-reviewed the MR. Nothing left to do. Change record looks good to me!

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new98 bytes

The 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.

dries’s picture

Status: Needs work » Reviewed & tested by the community

Rebased on main. There were two conflicts: (1) #3623409: Add the "ShareEnvironment" attribute to all Kernel tests adding ShareEnvironment to 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.