-
-
Notifications
You must be signed in to change notification settings - Fork 936
Closed
Description
API Platform version(s) affected: 4.1.20
Description
When returning a DTO from a custom provider in API Platform for Laravel, none of the DTO’s properties are exposed in the JSON-LD response, even though they are annotated with #[Groups] matching the normalizationContext of the operation.
How to reproduce
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/slots/pickup',
provider: SlotPickupProvider::class,
output: SlotDto::class,
normalizationContext: [
'groups' => [
'read:pickup',
],
],
),
]
)]
#[ApiProperty(
property: 'id',
serialize: new Groups([
'read:pickup',
]),
)]
class Slot extends Model {}
final readonly class SlotDto
{
public function __construct(
#[Groups(['read:pickup'])]
public CarbonImmutable $date,
/**
* @var Slot[]
*/
#[Groups(['read:pickup'])]
public array $slots,
) {
}
}
Expected Result
{
"@context": "/contexts/Slot",
"@id": "/slots/pickup",
"@type": "Collection",
"totalItems": 10,
"member": [
{
"@id": "/.well-known/genid/e1f4019f1928036caeca",
"@type": "SlotDto",
"date": "2025-08-10",
"slots": [
{ "id": "id_of_slot", "start_time": "08:00", "end_time": "10:00" }
]
}
]
}
Actual Result
{
"@context": "/contexts/Slot",
"@id": "/slots/pickup",
"@type": "Collection",
"totalItems": 10,
"member": [
{
"@id": "/.well-known/genid/e1f4019f1928036caeca",
"@type": "SlotDto"
// no properties serialized
}
]
}
Notes:
The DTO is returned directly from the provider.
The properties have correct #[Groups] annotations matching the normalizationContext.
Works fine when returning an Eloquent model, but fails for DTOs.
Metadata
Metadata
Assignees
Labels
No labels