Symfony 7 - Nelmio API Doc not generating docs properaly

57 views Asked by At

So I am trying to make a Swagger API docs using nelmio api doc.

So the issue I am currently running into is that I get the /api/doc route in my browser. My route is being seen correctly but for some reason non of my parameters are being recognized in documentation. Is that because of the usage of annotation instead of attribute?

Controller I am trying to document

<?php

namespace App\Controller;

use App\Entity\User;
use App\Form\RegisterType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use App\Utils\HelperFunctions;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use OpenApi\Annotations as OA;

#[Route('/api', name: "api_user_")]
class UserController extends AbstractController
{
    use HelperFunctions;
    public function __construct(
        private UserPasswordHasherInterface $passwordHasher,
        private EntityManagerInterface $entityManager
    ) {
    }


    /**
     * @OA\Post(
     *     path="/register",
     *     tags={"register"},
     *     summary="Register a new user",
     *     description="Register a new user",
     *     operationId="registerUser",
     *     @OA\RequestBody(
     *         required=true,
     *         description="User data",
     *         @OA\JsonContent(ref="#/components/schemas/User")
     *     ),
     *     @OA\Response(
     *         response=201,
     *         description="User registered successfully",
     *         @OA\JsonContent(ref="#/components/schemas/User")
     *     ),
     *     @OA\Response(
     *         response=400,
     *         description="Invalid data provided"
     *     )
     * )
     */
    #[Route('/register', name: 'register', methods: ['POST'])]
    public function register(Request $request): JsonResponse
    {
        $userData = json_decode($request->getContent(), true);

        $user = new User();

        $form = $this->createForm(RegisterType::class, $user);

        $form->submit($userData);

        if (!$form->isValid()) {
            $this->extractFormErrors($form);
        }

        $user->setPassword($this->passwordHasher->hashPassword($user, $user->getPassword()));

        $this->entityManager->persist($user);
        $this->entityManager->flush();

        $response = $this->json([
            'message' => "User has been registred successfully",
            "user" => $user
        ], 201);

        return $response;
    }
}

Nelmio api doc

nelmio_api_doc:
  documentation:
    info:
      title: Asco
      description: This is an awesome app!
      version: 1.0.0
  areas: # to filter documented areas
    path_patterns:
      - ^/api(?!/doc$) # Accepts routes under /api except /api/doc

So none of these properties are being displayed on /api/doc I know its working because when I type into comment block my info is displayed but when @OA* nothing is being displayed

0

There are 0 answers