-
-
Notifications
You must be signed in to change notification settings - Fork 932
Closed
Description
API Platform version(s) affected: 4.1.20
Description
In Laravel, relations whose names are in camelCase will not serialize properly.
How to reproduce
#[ApiResource(
normalizationContext: ['groups' => ['proof:read']],
denormalizationContext: ['groups' => ['proof:write']],
middleware: ['auth:sanctum', AuthorizeOwnResourceAccess::class],
operations: [
new Post(
middleware: ['auth:sanctum', AuthorizeOwnResourceAccess::class]
),
new Get(),
new GetCollection(),
new Patch(),
new Delete(),
new Put(),
]
)]
#[Groups(['proof:read', 'proof:write'])]
class Proof extends Model
{
public function test(): BelongsTo
{
return $this->belongsTo(ProofStatusMessage::class, 'proof_status_message_id');
}
public function test2(): BelongsTo
{
return $this->belongsTo(ProofStatusMessage::class, 'proof_status_message_id');
}
public function testTruc(): BelongsTo
{
return $this->belongsTo(ProofStatusMessage::class, 'proof_status_message_id');
}
public function testTruc2(): BelongsTo
{
return $this->belongsTo(ProofStatusMessage::class, 'proof_status_message_id');
}
public function statusMessage(): BelongsTo
{
return $this->belongsTo(ProofStatusMessage::class, 'proof_status_message_id');
}
}
---------
#[ApiResource(
normalizationContext: ['groups' => ['proofStatusMessage:read']],
denormalizationContext: ['groups' => ['proofStatusMessage:write']],
middleware: ['auth:sanctum', AuthorizeOwnResourceAccess::class]
)]
#[ApiProperty(property: 'id', serialize: new Groups(['proof:read']))]
#[ApiProperty(property: 'label', serialize: new Groups(['proof:read']))]
#[ApiProperty(property: 'content', serialize: new Groups(['proof:read']))]
#[Groups(['proofStatusMessage:read', 'proofStatusMessage:write'])]
class ProofStatusMessage extends Model
{
}
In this example, only the relations named test and test2 will be correctly serialized. The other ones won't appear at all unless adding them in an ApiProperty attribute this way #[ApiProperty(property: 'proofType', serialize: new Groups(['proof:read']))]
but even then only the id will be serialized, the label and content will never show up.
Possible Solution
Additional Context
Edit : To add to this, it also works when using snake_case instead of camelCase for the name, and it's returned as camelCase in the json anyway.
Eg :
// This works
public function status_message(): BelongsTo
{
return $this->belongsTo(ProofStatusMessage::class, 'proof_status_message_id');
}
// This does not work
public function statusMessage(): BelongsTo
{
return $this->belongsTo(ProofStatusMessage::class, 'proof_status_message_id');
}
Metadata
Metadata
Assignees
Labels
No labels