Skip to content
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

Not possible to create Many-to-One embedded entities that have constructor #2395

Closed
maks-rafalko opened this issue Dec 19, 2018 · 5 comments
Closed

Comments

@maks-rafalko
Copy link
Contributor

Let's consider the example from Behat tests of this repository.

We have VoDummyCar that has a collection of VoDummyInspection[] entities (with constructor public function __construct(bool $accepted, VoDummyCar $car...).

We already have separate tests that create VoDummyCar and VoDymmyInspection for particular car

POST /vo_dummy_car

And I send a "POST" request to "/vo_dummy_cars" with body:
"""
{
"mileage": 1500,
"bodyType": "suv",
"make": "CustomCar",
"insuranceCompany": {
"name": "Safe Drive Company"
},
"drivers": [
{
"firstName": "John",
"lastName": "Doe"
}
]
}
"""

POST /vo_dummy_inspections

And I send a "POST" request to "/vo_dummy_inspections" with body:
"""
{
"accepted": true,
"car": "/vo_dummy_cars/1"
}

What does not work?

Someone might want to create VoDummyInspection entities at the same time when VoDummyCar is being created, providing a nested array.

It turned out, that such request does not work with ApiPlatform:

{
    "mileage": 1500,
    "bodyType": "suv",
    "make": "CustomCar",
    "insuranceCompany": {
        "name": "Safe Drive Company"
    },
    "drivers": [],
    "inspections": [
        {
            "performed": "2018-08-24 00:00:00",
            "accepted": false
        }
    ]
}

Expected result

Running the query above, I expect new VoDummyCar to be created as well as new VoDummyInspections to be created for this VoDummyCar (in this case - just one entity).

Actual Result

  1. When I modify existing tests - nothing happens. inspections key from JSON is ignored (I have added addInspection and removeInspection methods to make this property writable, but it didn't help).

  2. In my project, I get an error something like Cannot create an instance of VoDummyInspection from serialized data because its constructor requires parameter \"car\" to be present.


Any suggestions about possible implementation?

@karser
Copy link
Contributor

karser commented Dec 19, 2018

  1. This is a serializer issue, an array of objects is not deserialized properly. My PR fixes it [Hackday][Serializer] Deserialization ignores argument type hint from phpdoc for array in constructor argument symfony/symfony#29513
    It's merged but not tagged yet, so use branches, e.g 4.1.x-dev or 4.2.x-dev, etc.
  2. I used to have a similar issue but it's gone when I switched to api-platform: dev-master

@maks-rafalko
Copy link
Contributor Author

maks-rafalko commented Dec 19, 2018

Thanks for taking the time and commenting. However, this is not the same issue.

If I rewrite the bug with entities from your PR, the issue is with the not setting Zoo to Animals constructor

class Animal {
    /** @var Zoo */
    private $zoo;

    /** @var string */
    private $name;

    public function __construct(Zoo $zoo, string $name) {...}
}

and POST /zoos

{
    "animals": [
        {"name": "Bug"}
    ]
}

So the issue is that api-platform can't deserialize Animal and pass Zoo instance to when one-to-many is used.

@soyuka
Copy link
Member

soyuka commented Feb 23, 2021

This looks really specific though and I'd create a custom DataPersister for such need. Are you sure that the normalizer doesn't work properly with Dtos? About constructors the support has been added in recent version, lmk if I need to reopen this. Thanks for your support all these years though :).

@soyuka soyuka closed this as completed Feb 23, 2021
@maks-rafalko
Copy link
Contributor Author

Having the same issue right now in 2022, and found my own issue :)

@soyuka probably you have a new recommendations now with new versions of APIP for this problem?

@NikDevPHP
Copy link

NikDevPHP commented Jan 12, 2023

Hello @soyuka any updates on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants