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

Custom Controller via POST accessible, not via GET-Collection #2681

Open
cloudmaker97 opened this issue Mar 29, 2024 · 0 comments
Open

Custom Controller via POST accessible, not via GET-Collection #2681

cloudmaker97 opened this issue Mar 29, 2024 · 0 comments

Comments

@cloudmaker97
Copy link

cloudmaker97 commented Mar 29, 2024

API Platform version(s) affected: 3.2

Description
I've tried to create a custom endpoint for retrieving the user details for the current authenticated user. It took me several hours to figure out, why it doesn't worked as seen on the documentation. I tried this even in YAML, as even in the PHP-Attribute Version. When using POST-Attribute, the route is correct and the Controller is responding with the correct data from the method. When i try to access the same (as required) with a GET-Attribute, i get always 404 error. The code examples are shown below in additional context.

How to reproduce

Not working example with GET-Collection
#[GetCollection(
    controller: UserController::class,
    openapi: new Operation(
        summary: 'Get the current user details',
        description: 'Get the current user details',
        requestBody: new RequestBody(),
    ),
    security: 'is_granted("ROLE_USER")',
    name: 'app_user_current'
)]
Working example with POST

Exactly the same, only change has been made in the first line

#[Post(
    uriTemplate: '/api/users/current',
    controller: UserController::class,
    openapi: new Operation(
        summary: 'Get the current user details',
        description: 'Get the current user details',
        requestBody: new RequestBody(),
    ),
    security: 'is_granted("ROLE_USER")',
    name: 'app_user_current'
)]
Code from the Controller
#[Route('/api/users/current', name: 'app_user_current')]
public function test(): Response
{
    $user = $this->entityManager->getRepository(User::class)->find($this->getUser()->getId());
    $returnData = [
        'id' => $user->getId(),
        'email' => $user->getEmail(),
        'firstname' => $user->getFirstname(),
        'lastname' => $user->getLastname(),
        'roles' => $user->getRoles(),
    ];
    return $this->json($returnData);
}

Additional Context

The SwaggerUI is showing the route in both variants, but the GET-Response is always 404 not found. The Symfony router debug command is showing these routes also in both variants.

Error-Response when requesting via GET-Request
{
  "title": "An error occurred",
  "detail": "Not Found",
  "status": 404,
  "type": "/errors/404",
  "trace": [
    {
      "file": "/var/www/html/backend/vendor/api-platform/core/src/Symfony/Bundle/SwaggerUi/SwaggerUiProvider.php",
      "line": 48,
      "function": "provide",
      "class": "ApiPlatform\\State\\Provider\\ReadProvider",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/api-platform/core/src/Symfony/Security/State/AccessCheckerProvider.php",
      "line": 53,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Bundle\\SwaggerUi\\SwaggerUiProvider",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/api-platform/core/src/State/Provider/DeserializeProvider.php",
      "line": 47,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Security\\State\\AccessCheckerProvider",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/api-platform/core/src/Symfony/Security/State/AccessCheckerProvider.php",
      "line": 53,
      "function": "provide",
      "class": "ApiPlatform\\State\\Provider\\DeserializeProvider",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/api-platform/core/src/Symfony/Validator/State/QueryParameterValidateProvider.php",
      "line": 42,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Security\\State\\AccessCheckerProvider",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/api-platform/core/src/Symfony/Validator/State/ValidateProvider.php",
      "line": 32,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Validator\\State\\QueryParameterValidateProvider",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/api-platform/core/src/Symfony/Security/State/AccessCheckerProvider.php",
      "line": 53,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Validator\\State\\ValidateProvider",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/api-platform/core/src/State/Provider/ContentNegotiationProvider.php",
      "line": 56,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Security\\State\\AccessCheckerProvider",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/api-platform/core/src/Symfony/Controller/MainController.php",
      "line": 82,
      "function": "provide",
      "class": "ApiPlatform\\State\\Provider\\ContentNegotiationProvider",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/symfony/http-kernel/HttpKernel.php",
      "line": 178,
      "function": "__invoke",
      "class": "ApiPlatform\\Symfony\\Controller\\MainController",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/symfony/http-kernel/HttpKernel.php",
      "line": 76,
      "function": "handleRaw",
      "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/symfony/http-kernel/Kernel.php",
      "line": 185,
      "function": "handle",
      "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php",
      "line": 35,
      "function": "handle",
      "class": "Symfony\\Component\\HttpKernel\\Kernel",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/vendor/autoload_runtime.php",
      "line": 29,
      "function": "run",
      "class": "Symfony\\Component\\Runtime\\Runner\\Symfony\\HttpKernelRunner",
      "type": "->"
    },
    {
      "file": "/var/www/html/backend/public/index.php",
      "line": 5,
      "function": "require_once"
    }
  ]
}

Thank you for your amazing work! ❤️

@cloudmaker97 cloudmaker97 changed the title Custom Controller only works on POST, not on GET-Collection Custom Controller via POST accessible, not via GET-Collection Mar 29, 2024
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

1 participant