I have to migrate an API developed on APIPlatform v2 to APIPlatform v3.
In this project the previous developer created custom patch actions as this (in a YAML file) :
reactivated:
security_post_denormalize: 'is_granted("OT", object)'
security_post_denormalize_message: "Vous n'avez pas l'accès à cette ressource."
method: PATCH
path: /o_ts/{id}/reactivated
controller: App\Controller\OTReactivated
openapi_context:
summary: Work Order reactivated
I transformed it to PHP 8 attribute like this :
#[ApiResource(
operations: [
new GetCollection(normalizationContext: ['groups' => ['ot_collection_read']]),
new Get(requirements: ['id' => '\d+'], normalizationContext: ['groups' => ['ot_read']]),
new Post(
securityPostDenormalize: 'is_granted("OT", object)',
securityPostDenormalizeMessage: "Vous n'avez pas l'accès à cette ressource.",
validationContext: ['groups' => [OTAddSequencedGroup::class]],
),
new Put(
denormalizationContext: ['groups' => ['ot_update_write', 'ot:attachments', 'attachment:write', 'attachment:subcontractingProductionCenter', 'attachment:customFieldValues', 'attachment_custom_field_value:field', 'custom_field_value:write']],
securityPostDenormalize: 'is_granted("OT", object)',
securityPostDenormalizeMessage: "Vous n'avez pas l'accès à cette ressource.",
validationContext: ['groups' => ['Default', OTDefaultSequencedGroup::class, 'attachment:valid:create', 'ot:address:update']]
),
new Patch(
requirements: ['id' => '\d+'],
denormalizationContext: ['groups' => ['ot_update_write', 'ot:attachments', 'attachment:write', 'attachment:subcontractingProductionCenter', 'attachment:customFieldValues', 'attachment_custom_field_value:field', 'custom_field_value:write']],
securityPostDenormalize: 'is_granted("OT", object)',
securityPostDenormalizeMessage: "Vous n'avez pas l'accès à cette ressource.",
validationContext: ['groups' => ['Default', OTDefaultSequencedGroup::class, 'attachment:valid:create', 'ot:address:update']]
),
],
normalizationContext: ['groups' => ['ot_read']],
denormalizationContext: ['groups' => ['ot_write']],
validationContext: ['groups' => OTDefaultSequencedGroup::class],
security: 'is_granted("ROLE_USER")'
)]
#[Patch(
uriTemplate: '/o_ts/{id}/reactivated',
controller: OTReactivated::class,
openapiContext: ['summary' => 'Work Order reactivated'],
securityPostDenormalize: 'is_granted("OT", object)',
securityPostDenormalizeMessage: "Vous n'avez pas l'accès à cette ressource.",
name: 'api_o_ts_reactivated',
)]
So far so good, the route is here, and the call work as expected. Except in the response. The generated iri is not correct. It seems that the ApiPlatform converter thinks the object id is “-id-/reactivated”, but its not.
Look yourself.
The call (image) :
The beginning of the response:
{
"@context": "\/contexts\/OT",
"@id": "\/o_ts\/2\/reactivated",
"@type": "OT",
"id": 2
}
The @id should be : “/o_ts/2”
I know, the documentation says that it’s not recommanded but the API is already done and in production … So I can’t just change the routes …
So, does anybody have a solution to my problem?
I tried to add uriVariables like this :
#[Patch(
uriTemplate: '/o_ts/{id}/reactivated',
uriVariables: ['id' => new Link(fromClass: self::class, identifiers: ['id'])],
controller: OTClose::class,
openapiContext: ['summary' => 'Work Order reactivated'],
securityPostDenormalize: 'is_granted("OT", object)',
securityPostDenormalizeMessage: "Vous n'avez pas l'accès à cette ressource.",
name: 'api_o_ts_reactivated',
)]