Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] Add Attribute Sorter to AbstractObjectNormalizer #59063

Copy link
Copy link
Open
@Hanmac

Description

@Hanmac
Issue body actions

Description

If I have for example this classes:

class A 
{
  public function __construct(
    #[SerializedName("PropA")]
    protected ?string $propA = null,
    #[SerializedName("PropB")]
    protected ?string $propB = null,
  )
}

and a second, slightly different class with the same attributes, but in a different order

class B
{
  public function __construct(
    #[SerializedName("PropB")]
    protected ?string $propB = null,
    #[SerializedName("PropA")]
    protected ?string $propA = null,
  )
}

For security, I didn't want to make the properties public, so I needed to add getter/setter functions.
Because they are the same, I thought about using a Trait.

But that didn't work because ObjectNormalizer returns the Attributes in the order of the functions it found.

My idea is defining an Interface where the User can create their own Sort Functions

public function sortAttributes($string $object, array $attributes, ?string $format = null, array $context = []): array

So you could make the sort of the attributes depending on the object or the class of the object.

I don't know what kind of implementations (like sort by constructor params?) should be predefined

See also #27441

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.