Skip to content

[HttpFoundation] Add ProblemDetailsJsonResponse for HTTP APIs #61208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: 7.4
Choose a base branch
from

Conversation

abdellahrk
Copy link

@abdellahrk abdellahrk commented Jul 22, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
License MIT

This PR introduces the ProblemDetailJsonResponse which is an implementation of RFC 9457 to describe the specifics of a problem encountered:

{
  "type": "https://example.com/probs/out-of-credit",
  "title": "You do not have enough credit.",
  "detail": "Your current balance is 30, but that costs 50.",
  "instance": "/account/12345/msgs/abc",
  "balance": 30,
  "accounts": ["/account/12345", "/account/67890"]
}

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@abdellahrk abdellahrk force-pushed the feature/problem_detail_response branch from 2bbb577 to 7cecc40 Compare July 22, 2025 22:01
@abdellahrk
Copy link
Author

Thank you very much @smnandre

@abdellahrk abdellahrk requested a review from smnandre July 23, 2025 07:14
@abdellahrk
Copy link
Author

Thank you @OskarStark

@OskarStark OskarStark changed the title [HttpFoundation] Add Problem Details for HTTP APIs [HttpFoundation] Add ProblemDetailJsonResponse for HTTP APIs Jul 24, 2025
@OskarStark OskarStark changed the title [HttpFoundation] Add ProblemDetailJsonResponse for HTTP APIs [HttpFoundation] Add ProblemDetailsJsonResponse for HTTP APIs Jul 24, 2025
@abdellahrk abdellahrk force-pushed the feature/problem_detail_response branch from 8befed4 to a51dabd Compare July 24, 2025 18:48
@mahono
Copy link

mahono commented Jul 29, 2025

The RFC also supports “errors”. Would it make sense to also support these in the class?

@abdellahrk
Copy link
Author

The RFC also supports “errors”. Would it make sense to also support these in the class?

@mahono sure. The "errors" can be added as an extension as indicated here

This is an example with the errors extension
image

@mahono
Copy link

mahono commented Jul 31, 2025

Great. I missed that one because I was only scanning for „error“.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like everything should be in the constructor, or, if we split in methods, computed lazily (but not sure that'd be useful here)

@abdellahrk abdellahrk force-pushed the feature/problem_detail_response branch from 49d5149 to 03299c5 Compare July 31, 2025 17:46
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I may miss a lot, so here are quick questions to help me understand

class ProblemDetailsJsonResponse extends Response
{
public function __construct(
private ?int $status = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about making this not nullable and default to 500?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh thank you @nicolas-grekas.
I seriously have to do this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the RFC does not specify a default status code. I just think it'll be wise to have a default and I don't know if 500 is okay.

Comment on lines 40 to 41
if ($this->title && null === $this->type) {
$this->title = Response::$statusTexts[$this->status];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks strange to me to override the title if it's already set before

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the problem type is not specified, the status code text is used as the title

When "about:blank" is used, the title SHOULD be the same as the recommended HTTP status phrase for that code (e.g., "Not Found" for 404, and so on), although it MAY be localized to suit client preferences (expressed with the Accept-Language request header)

Per the RFC

This feature adds more detail to the HTTP response errors as specified by RFC 7807 and completed by RFC 9457
@abdellahrk abdellahrk force-pushed the feature/problem_detail_response branch from 4c366c3 to 3f25fa6 Compare August 1, 2025 18:08
@chalasr
Copy link
Member

chalasr commented Aug 1, 2025

Congrats on your first PR to Symfony core, Abdellah.
Symfony provides a ProblemNormalizer that turns exception objects into API Problem Details compliant arrays. I wonder if there could be any correlation between it and the proposed response class? They have things in common, and the normalizer automatically leverages symfony/translation and symfony/validator when possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy