Can I change {id} to a different name?
See original GitHub issueI am using api-platform with Symfony 4. It works fine, but I would like to change a GET url from: /booking/{id} to /booking/{bookingId}
I’m using my own DTO object and custom data provider (not Doctrine ORM).
Here are the current @ApiResource and @ApiProperty definitions that work fine:
/**
*
* @ApiResource(
* itemOperations={
* "get"={
* "path"="/booking/{id}",
* },
* "api_bookings_get_item"={
* "swagger_context"={
* "operationId"="getBookingItem",
* "summary"="Retrieves details on a booking",
* "parameters"= {
* {
* "name"="id",
* "description"="Booking ID",
* "default"="15000",
* "in"="path",
* "required"=true,
* "type"="string"
* }
* },
* "responses"={
* "200"={
* "description"="Results retrieved"
* },
* "404"={
* "description"="Booking not found"
* }
* }
* }
* }
* },
* collectionOperations={}
* )
*/
final class Booking
{
/**
* @var string
* @Assert\NotBlank
*
* @ApiProperty(
* identifier=true,
* attributes={
* "swagger_context"={
* "description"="Booking ID",
* "required"=true,
* "type"="string",
* "example"="123456"
* }
* }
* }
*/
public $id;
// other variables
}
However, if I change all the references from 'id' to 'bookingId' it stops working and I get a 404 error. Here are the changes I made to the above code:
"path"="/booking/{bookingId}"
"name"="bookingId"
public $bookingId;
Is api-platform hard-coded to use 'id' as an identifier? Is there any way to change this?
From what I can tell, it appears to be hard-coded in the extractIdentifiers() function. I’ve been trying to figure out the code, but it is quite complex. I don’t know if making a change there will work or may have unintended consequences.
It seems to me that it should use whatever variable is tagged by the @apiProperty(identifier=true) annotation, but it does not appear to work like this. It appears that the getIdentifiersFromResourceClass() function that checks the identifier annotation is being called AFTER extractIdentifiers(), which does not make sense to me.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:19 (8 by maintainers)

Top Related StackOverflow Question
idseems to be hardcoded: https://github.com/api-platform/core/blob/76722a74b479cab0c0b262e8dfa689fad68e1d8d/src/Bridge/Symfony/Routing/IriConverter.php#L124Wait what is your full example? You should specify a path and correlation between parameters => [class, property]: