Problem/Motivation

If you return a \Drupal\rest\ResourceResponse from a custom route, you get a crash:

> TypeError: Symfony\Component\Serializer\Encoder\ChainEncoder::getEncoder(): Argument #1 ($format) must be of type string, null given, called in /var/www/vendor/symfony/serializer/Encoder/ChainEncoder.php on line 49 in Symfony\Component\Serializer\Encoder\ChainEncoder->getEncoder() (line 80 of /var/www/vendor/symfony/serializer/Encoder/ChainEncoder.php).

This is because ResourceResponseSubscriber::getResponseFormat() looks at the route definition for the _format requirement. If that is missing, then a NULL is incorrectly passed as a parameter.

Steps to reproduce

Proposed resolution

Add documentation to \Drupal\rest\ResourceResponse to say the route needs this requirement to be set.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3277148

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

joachim created an issue. See original summary.

larowlan’s picture

Jingting’s picture

I am a novice at Drupalcon working on this issue in a mentor contribution group for the next hour.

farnoosh’s picture

I am mentoring @Athrylith and @Jingting on this issue at Drupalcon Portland Mentored contribution. We will be working on this issue during the mentored contribution session on Wednesday, Apr 27th

athrylith’s picture

I am working on this issue at Portland DrupalCon

farnoosh’s picture

This solution was posted on the "contribute" channel on Drupal Slack by @Jingting:

I found the file under this path: /workspace/DrupalPod/web/core/modules/rest/src/ResourceResponse.php
From the proposed resolution in this issue, I want to add a comment in the file above (eg. * We need to set the route definition for the _format requirement.).

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

rpayanm’s picture

Status: Active » Needs review
joachim’s picture

Status: Needs review » Needs work

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

andregp’s picture

Status: Needs work » Needs review

Addressing @joachim comment.

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Looks great, thanks!

  • catch committed 58a5046 on 10.0.x
    Issue #3277148 by rpayanm, andregp, joachim, Farnoosh, Athrylith,...
  • catch committed 3cc2c85 on 9.5.x
    Issue #3277148 by rpayanm, andregp, joachim, Farnoosh, Athrylith,...

  • catch committed 86f111e on 9.4.x
    Issue #3277148 by rpayanm, andregp, joachim, Farnoosh, Athrylith,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 10.0.x, cherry-picked to 9.5.x and 9.4.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.