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 11 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 and others added 10 commits July 24, 2025 19:47
This feature adds more detail to the HTTP response errors as specified by RFC 7807 and completed by RFC 9457
….php

Co-authored-by: Simon André <smn.andre@gmail.com>
….php

Co-authored-by: Simon André <smn.andre@gmail.com>
…onResponseException.php

Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
….php

Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
@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“.

/**
* @author Abdellah Ramadan <ramadanabdel24@gmail.com>
*/
class ProblemDetailsJsonResponseException extends \Exception
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we be more specific for the class we extend?

Copy link
Author

@abdellahrk abdellahrk Jul 31, 2025

Choose a reason for hiding this comment

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

Okay @OskarStark.
As a first timer, I am still trying to get things right. Should I get rid of the entire exception class?

* @throws ProblemDetailsJsonResponseException
* @throws \JsonException
*/
protected function setProblemContent(): string
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason to not make it private?

Copy link
Author

Choose a reason for hiding this comment

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

No reason. I can make it private.

public function testNewProblemWithNoParams()
{
$problemDetails = new ProblemDetailsJsonResponse();
$this->assertEquals(520, $problemDetails->getStatusCode());
Copy link
Contributor

Choose a reason for hiding this comment

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

assertSame wherever possible please

Copy link
Author

Choose a reason for hiding this comment

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

Noted.

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)

$this->setProblemContent();
}

private function setHeaders(): void
Copy link
Member

Choose a reason for hiding this comment

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

we should get rid of those unusual private setters IMHO

Copy link
Author

Choose a reason for hiding this comment

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

Okay @nicolas-grekas.
I am doing that then.

* @throws ProblemDetailsJsonResponseException
* @throws \JsonException
*/
protected function setProblemContent(): string
Copy link
Member

Choose a reason for hiding this comment

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

protected means this is an extension point. I don't think that's legit.

Copy link
Author

Choose a reason for hiding this comment

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

Should it be made private?
Sorry for being silly on this one. Still trying to get everything right.

…esponseTest.php

Co-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
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.

6 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