I'm encountering an issue with API Platform where it seems to treat a route parameter as a required idBadge, even though it's not specified as such in my route configuration. Here's the setup:
I have a Symfony controller method that retrieves a badge by its matricule (a specific identifier):
/**
* @Route("/badges_search/{matricule}", name="app_search_badge_matricule", methods={"GET"})
*/
public function searchBadgeByMatricule(SerializerInterface $serializer, BadgeRepository $badgeRepository, string $matricule): Response
{
$badge = $badgeRepository->findOneBy(['matricule' => $matricule]);
if (!$badge) {
return new Response(null, 404);
}
$badgeSerialize = $serializer->serialize($badge, 'json');
return new Response($badgeSerialize, 200, ['content-type' => 'application/json']);
}
However, when I view the generated API documentation, it incorrectly lists idBadge as a required parameter, even though idBadge is not part of my route parameters or specified anywhere in my annotations.
Here is my Badge
class
<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\BadgeRepository;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
#[ORM\Entity(repositoryClass: BadgeRepository::class)]
#[ORM\Table(name : "badge")]
#[ApiResource(
operations: [
new Get(),
new GetCollection(),
new Get(name: "recherche_badge_matricule",
routeName: "app_search_badge_matricule",
openapiContext: [
"parameters" => [
[
"name" => "matricule",
"in" => "path",
"required" => true,
"type" => "string",
"description" => "Matricule du badge"
]
],
"responses" => [
"200" => [
"description" => "Badge trouvé",
"content" => [
"application/json" => [
"schema" => [
"type" => "object",
"properties" => [
"idBadge" => [
"type" => "integer"
],
"matricule" => [
"type" => "integer"
],
"numeroBadge" => [
"type" => "integer"
],
"soldeBadge" => [
"type" => "number"
]
]
]
]
]
],
"404" => [
"description" => "Badge non trouvé"
]
],
"summary" => "Recherche d'un badge par matricule",
"description" => "Recherche d'un badge par matricule"
])
]
)]
class Badge
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(name: "idBadge")]
private ?int $idBadge = null;
#[ORM\Column(name: "matricule")]
private ?int $matricule = null;
#[ORM\Column(name: "numeroBadge")]
private ?int $numeroBadge = null;
#[ORM\Column(name: "soldeBadge")]
private ?float $soldeBadge = null;
I'm on this problem since yesterday but the documentation continues to show idBadge as a required parameter.
I've tried several annotations but can't seem to figure out where the issue is coming from. I've reviewed my entity and controller annotations multiple times, but the documentation still incorrectly lists idBadge as a required parameter. I’m not sure if the problem lies within the class definition or the controller configuration. Despite clearing caches and checking for any misconfigurations, I can't find the source of the issue. Being new to API Platform, it's likely a simple mistake in my annotations that I'm overlooking. Any guidance on how to correctly set up the documentation to reflect matricule as a path parameter without mistakenly identifying it as an idBadge would be greatly appreciated.
Could anyone advise on how to clarify to API Platform that matricule is not an idBadge and ensure it's correctly documented as a path parameter without confusing it with other entity identifiers?
I've identified the source of the issue. It appears that the Get()
method in my API Platform annotations requires a search on the primary key or a unique field, but "matricule" wasn't configured as such in my Badge entity. To fix this, I simply replaced Get()
with GetCollection()
in my annotations for the Badge
class. This helps API Platform understand that "matricule" is a path parameter distinct from the primary identifier (idBadge).