Skip to content
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

ErrorResource missing title and details in JSON+LD response #6926

Open
derTuca opened this issue Jan 23, 2025 · 3 comments
Open

ErrorResource missing title and details in JSON+LD response #6926

derTuca opened this issue Jan 23, 2025 · 3 comments

Comments

@derTuca
Copy link

derTuca commented Jan 23, 2025

API Platform version(s) affected: 4.0.16

Description
I have created this ErrorResource class.

<?php
// api/src/ApiResource/OtpAlreadyRequestedError.php

namespace App\ApiResource;
use ApiPlatform\Metadata\ErrorResource;
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;

#[ErrorResource]
class OtpAlreadyRequestedError extends \Exception implements ProblemExceptionInterface
{
    public function getType(): string
    {
        return '/errors/otp-already-requested';
    }
    public function getTitle(): ?string
    {
        return 'Code already requested';
    }
    public function getStatus(): ?int
    {
        return 406;
    }
    public function getDetail(): ?string
    {
        return 'Please wait 1 minute before requesting another code';
    }
    public function getInstance(): ?string
    {
        return null;
    }
}

When throwing this error, i would expect the response to contain the title and details. Instead, the response looks as such. The title and detail fields are missing.

{
  "@context": "/contexts/OtpAlreadyRequestedError",
  "@id": "/otp_already_requested_errors",
  "@type": "OtpAlreadyRequestedError"
}
@derTuca derTuca changed the title ErrorResource missing title and details ErrorResource missing title and details in JSON+LD response Jan 23, 2025
@derTuca
Copy link
Author

derTuca commented Jan 23, 2025

So for some context, I have the LoginRequest entity and I throw the error above in LoginRequestStateProcessor. This seems to happen when I have a group applied on the normalizationContext of LoginRequest for this operation. If I remove the serialization group, all properties get returned. Is it expected behavior that the serialization group should affect the entity's errors as well?

@soyuka
Copy link
Member

soyuka commented Jan 28, 2025

Mhh interesting groups are set there:

https://github.com/api-platform/core/blob/main/src/Symfony/EventListener/ErrorListener.php#L110

But they should not inherit from your original resource groups, would you be able to create a reproducer? You can also check inside the ErrorListener and the SerializeProcessor whether groups are correctly reset.

@derTuca
Copy link
Author

derTuca commented Jan 31, 2025

I have created a basic reproduction here. I'm not really sure whether I should reset the groups myself in the processor or if this should happen on its own. I am not overriding the ErrorListener, so that logic should still happen, right? Thanks!

To reproduce, one needs to run the following:

  1. docker compose build && docker compose up -d
  2. curl -k -X POST -H "Content-Type: application/ld+json" -d '{}' https://localhost/login_requests

Commenting out the groups from LoginRequest returns the whole error object.

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

No branches or pull requests

2 participants