1

Having a bit of trouble understanding the combo of tools in the title.

When I view my docs the model is empty: Empty Models

That said I've tried a bunch of things including disabling jms which works but I want to use JMS.

Here's my config:

nelmio_api_doc.yaml:

nelmio_api_doc:
  models:
    use_jms: true
  documentation:
    host: cms-api.example.com
    schemes: [ http, https ]
    info:
      title: CMS API
      description: This is an awesome app!
      version: 1.0.0
    securityDefinitions:
      Bearer:
        type: apiKey
        description: 'Value: Bearer {jwt}'
        name: Authorization
        in: header
    security:
      - Bearer: [ ]
  areas:
    default:
      path_patterns: [ ^/api/v1/ ]

User Model:

<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use JMS\Serializer\Annotation as Serializer;


/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @Serializer\ExclusionPolicy("all")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @SWG\Property(description="The unique identifier of the user.")
     * @Serializer\Expose()
     * @Serializer\Type("int")
     * @Serializer\Groups({"default"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     * @SWG\Property(type="string", maxLength=255)
     * @Serializer\Expose()
     * @Serializer\Type("string")
     * @var string|null
     */
    private $email;

    /**
     * @ORM\Column(type="json")
     * @SWG\Property(type="array", @SWG\Items(type="string"))
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     */
    private $password;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUserIdentifier(): string
    {
        return (string) $this->email;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see PasswordAuthenticatedUserInterface
     */
    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Returning a salt is only needed, if you are not using a modern
     * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
     *
     * @see UserInterface
     */
    public function getSalt(): ?string
    {
        return null;
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }
}

UserController:

<?php

namespace App\Controller\api\v1;

use App\Entity\User;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Swagger\Annotations as SWG;
use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Annotation\Security;

final class UserController extends AbstractRESTController
{
    /**
     * @Route("/api/v1/user", name="user_get_all_v1", methods={"GET"})
     * @SWG\Response(
     *     response=200,
     *     description="Returns the rewards of an user",
     *     @SWG\Schema(
     *         type="array",
     *         @SWG\Items(ref=@Model(type=User::class, groups={"full"}))
     *     )
     * )
     * @SWG\Tag(name="User")
     * @Security(name="Bearer")
     */
    public function getAllUsers(): JsonResponse
    {
        return $this->respond(['hello' => 'world']);
    }

    /**
     * @Route("/api/v1/user/{id}", name="user_get_one_v1", methods={"GET"})
     * @param User $user
     * @SWG\Response(
     *     response=200,
     *     description="Returns the rewards of an user",
     *     @SWG\Schema(
     *         type="array",
     *         @SWG\Items(ref=@Model(type=User::class, groups={"full"}))
     *     )
     * )
     * @SWG\Tag(name="User")
     * @Security(name="Bearer")
     *
     * @return JsonResponse
     */
    public function getOneUser(User $user): JsonResponse
    {
        return $this->respond([
            'id' => $user->getId(),
            'email' => $user->getEmail()
        ]);
    }
}

Any help or ideas would be great, I tried adding a group to the id but it still didn't work, I left it in the code example. What am I missing?

Lulceltech
  • 1,662
  • 11
  • 22

1 Answers1

0

One reason could be that in your controller methods annotations you use a group named full, but I don't see where that group is applied in your User class.

So you have to remove groups={"full"} from your controller annotations (or replace it with default which is used in User class), or add that group in the User model, for example like this @Serializer\Groups({"default", "full"}).

There probably may be other reasons why the doc is not working, but you should definitely fix the group name.

xtx
  • 4,306
  • 4
  • 28
  • 30