Skip to content

Serialization broken since refactor to Types #91

Closed
@bertramakers

Description

@bertramakers

Hi @tobyzerner . I was processing your comments on my PR/issues, which mentioned quite a lot of breaking changes.
While refactoring our app to be compatible with the latest version again, I ran into the following issue.

Before the refactor to Types, I had a field like this:

Str::make('nzcEmissionDomain')
  ->writable()
  ->nullable()
  ->default(null)
  ->enum(array_column(NzcEmissionDomain::cases(), 'value')
  ->serialize(fn (?NzcEmissionDomain $emissionDomain): ?string => $emissionDomain?->value)

Note that NzcEmissionDomain is a string-backed enum in our application.

I refactored this particular field to this:

Attribute::make('nzcEmissionDomain')
  ->type(Str::make()->enum(array_column(NzcEmissionDomain::cases(), 'value')))
  ->writable()
  ->nullable()
  ->default(null)
  ->serialize(fn (?NzcEmissionDomain $emissionDomain): ?string => $emissionDomain?->value)

However, when I now include a value for it in a create/update request, I get the following Error:

Object of class App\\V2\\Domain\\Measures\\NzcEmissionDomain could not be converted to string

This is caused by Attribute calling the serialize() method on Str first before calling the custom serialization function (here). And the Str::serialize() method tries to cast the value to a string (here), which will fail if it's a type that cannot be cast automatically. The purpose of the custom serialize method seemed to me to prevent this, so I would assume that it should be called first instead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions