Skip to content

Laravel - Serialization not working on relations with a CamelCase name #7344

@Lurivar

Description

@Lurivar

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions