Skip to content

[Serializer] @SerializedName flattening nested attributes #32080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
piotrooo opened this issue Jun 18, 2019 · 11 comments · Fixed by #43534
Closed

[Serializer] @SerializedName flattening nested attributes #32080

piotrooo opened this issue Jun 18, 2019 · 11 comments · Fixed by #43534
Labels
Feature Help wanted Issues and PRs which are looking for volunteers to complete them. Serializer

Comments

@piotrooo
Copy link

piotrooo commented Jun 18, 2019

Description
Sometimes I want to flatten a JSON object into POPHPO.
For example Jackson provide following feature in annotation: @JsonProperty("filter.key1").
@JsonProperty like annotation should handle nested values.

Example

Following JSON:

{
    "key1": "value1"
    "nested": {
          "sub_key1": "sub_value1"
          "sub_key2": "sub_value2"
     }
}

And unmarshalled object:

class Obj
{
    /**
     * @JsonProperty('key1')
     * @var string
     */
    private @name;
    /**
     * @JsonProperty('nested.sub_key1')
     * @var string
     */
    private @other;
    /**
     * @JsonProperty('nested.sub_key2')
     * @var string
     */
    private @anotherAgain;
}
@piotrooo
Copy link
Author

@javiereguiluz any info about this?

@javiereguiluz
Copy link
Member

I'm sorry but I don't know the Serializer component much, so we should wait for other people opinions.

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@piotrooo
Copy link
Author

Yeap, that would be nice to have something like that.

@carsonbot carsonbot removed the Stalled label Dec 19, 2020
@mesilov
Copy link

mesilov commented Feb 17, 2021

Yes, I need to denormalize some nested objects into flat array

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@derrabus
Copy link
Member

I think, this would be a useful addition, but somebody has to build it. 😃

@carsonbot carsonbot removed the Stalled label Aug 18, 2021
@derrabus derrabus added the Help wanted Issues and PRs which are looking for volunteers to complete them. label Aug 18, 2021
@boenner
Copy link
Contributor

boenner commented Sep 24, 2021

I tried tackling this in flatten-nested-attributes. Some things are still missing and it's not properly tested yet, so I didn't create a pull request. Could you take a look at the branch, @derrabus, to check if this is the right way to go?

Using the SerializedName annotation in SomeClass

/**
 * @SerializedName("First.Second.@attribute")
 */
private $foo

/**
 * @SerializedName("First.Second.Third")
 */
private $bar

together with the flatten_nested_attributes context option

$output = $serializer->deserialize(
    $input, 
    SomeClass::class, 
    'xml',
    ['flatten_nested_attributes' => true]
)

and

<First>
    <Second attribute="foo">
        <Third>bar</Third>
    </Second>
</First>

fills the annotated properties with foo and bar.

@derrabus
Copy link
Member

I think, a pull request is always better to discuss changes and the course of action. Don't hesitate to create one, even if it's not ready yet.

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@boenner
Copy link
Contributor

boenner commented Mar 30, 2022

Yep! The pull request for this is a bit stalled right now, but still active.

@carsonbot carsonbot removed the Stalled label Mar 30, 2022
@fabpot fabpot closed this as completed Oct 23, 2022
fabpot added a commit that referenced this issue Oct 23, 2022
…n nested attributes (boenner)

This PR was merged into the 6.2 branch.

Discussion
----------

[Serializer] Add `SerializedPath` annotation to flatten nested attributes

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #32080
| License       | MIT
| Doc PR        | todo

As suggested by `@derrabus` in #32080, I'm creating a PR for this.

In order to normalize/denormalize nested attributes, the ``@SerializedPath`` annotation can be used:

```php
class NestedDummy
{
    /**
     * `@SerializedPath`("[one][two][three]")
     */
    public $foo;

    /**
     * `@SerializedPath`("[one][four]")
     */
    public $bar;
}
```

with

```php
$data = [
    'one' => [
        'two' => [
            'three' => 'foo',
        ],
        'four' => 'bar',
    ],
];
$normalizer = new AbstractObjectNormalizerWithMetadata();
$normalizer->denormalize(
    $data,
    NestedDummy::class,
    'any'
);
```

The annotations needs to be used with a valid `PropertyPath` string for this to work.

Open todos:
- [x] tests for new feature
- [ ] update documentation

Commits
-------

08a1119 [Serializer] Add `SerializedPath` annotation to flatten nested attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Help wanted Issues and PRs which are looking for volunteers to complete them. Serializer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants