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):
php
/**
* @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 AppEntity;
use ApiPlatformMetadataApiResource;
use AppRepositoryBadgeRepository;
use DoctrineORMMapping as ORM;
use ApiPlatformMetadataGet;
use ApiPlatformMetadataGetCollection;
#[ORMEntity(repositoryClass: BadgeRepository::class)]
#[ORMTable(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
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn(name: "idBadge")]
private ?int $idBadge = null;
#[ORMColumn(name: "matricule")]
private ?int $matricule = null;
#[ORMColumn(name: "numeroBadge")]
private ?int $numeroBadge = null;
#[ORMColumn(name: "soldeBadge")]
private ?float $soldeBadge = null;
I’m on this problem since yesterday but the documentation continues to show idBadge as a required parameter.
A screen of my API Platform Documentation
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?
Thank you!
Esteban RACINE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.