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

properties of subresource not serialized after update to APIP 2.4 #2641

Closed
remoteclient opened this issue Mar 24, 2019 · 22 comments
Closed

properties of subresource not serialized after update to APIP 2.4 #2641

remoteclient opened this issue Mar 24, 2019 · 22 comments
Labels

Comments

@remoteclient
Copy link
Contributor

remoteclient commented Mar 24, 2019

I have a pet owner:

<?xml version="1.0" encoding="UTF-8" ?>
<resources xmlns="https://api-platform.com/schema/metadata"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="https://api-platform.com/schema/metadata
                               https://api-platform.com/schema/metadata/metadata-2.0.xsd"
>
    <resource class="MWS\NutritionCalculatorBundle\Entity\PetOwner">
        <attribute name="access_control">is_granted('ROLE_MWS_NC_GUEST')</attribute>
        <attribute name="client_items_per_page">true</attribute>
        <attribute name="items_per_page_parameter_name">itemsPerPage</attribute>
        <property name="pets">
            <subresource resourceClass="MWS\NutritionCalculatorBundle\Entity\Pet" maxDepth="1" collection="true"/>
        </property>
        <attribute name="normalization_context">
            <attribute name="groups">
                <attribute>mws_nc_pet_owner_read</attribute>
            </attribute>
        </attribute>
        <attribute name="denormalization_context">
            <attribute name="groups">
                <attribute>mws_nc_pet_owner_write</attribute>
            </attribute>
        </attribute>
        <collectionOperations>
            <collectionOperation name="get">
                <attribute name="method">GET</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_NC_LIST')</attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_nc_pet_owner_read</attribute>
                    </attribute>
                </attribute>
            </collectionOperation>
            <collectionOperation name="post">
                <attribute name="method">POST</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_NC_CREATE')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_nc_pet_owner_write</attribute>
                    </attribute>
                </attribute>
                <attribute name="validation_groups">
                    <attribute>PetOwner</attribute>
                </attribute>
            </collectionOperation>
        </collectionOperations>
        <itemOperations>
            <itemOperation name="get">
                <attribute name="method">GET</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_NC_VIEW')</attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_nc_pet_owner_read</attribute>
                    </attribute>
                </attribute>
            </itemOperation>
            <itemOperation name="put">
                <attribute name="method">PUT</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_NC_EDIT')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_nc_pet_owner_write</attribute>
                    </attribute>
                </attribute>
                <attribute name="validation_groups">
                    <attribute>PetOwner</attribute>
                </attribute>
            </itemOperation>
            <itemOperation name="delete">
                <attribute name="method">DELETE</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_NC_DELETE')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_nc_pet_owner_delete</attribute>
                    </attribute>
                </attribute>
            </itemOperation>
        </itemOperations>
    </resource>
</resources>

And there is a subresource (pets belong to the pet owner):

<?xml version="1.0" encoding="UTF-8" ?>
<resources xmlns="https://api-platform.com/schema/metadata"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="https://api-platform.com/schema/metadata
                               https://api-platform.com/schema/metadata/metadata-2.0.xsd"
>
    <resource class="MWS\NutritionCalculatorBundle\Entity\Pet">
        <attribute name="access_control">is_granted('ROLE_MWS_NC_GUEST')</attribute>
        <attribute name="pagination_client_enabled">true</attribute>
        <attribute name="normalization_context">
            <attribute name="groups">
                <attribute>mws_nc_pet_read</attribute>
            </attribute>
        </attribute>
        <attribute name="denormalization_context">
            <attribute name="groups">
                <attribute>mws_nc_pet_write</attribute>
            </attribute>
        </attribute>
        <collectionOperations>
            <collectionOperation name="api_pet_owners_pets_get_subresource">
                <attribute name="method">GET</attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute>mws_nc_pet_list</attribute>
                    </attribute>
                </attribute>
            </collectionOperation>
            <collectionOperation name="sex_type_choices">
                <attribute name="method">GET</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_NC_LIST')</attribute>
                <attribute name="path">pets/sex_type_choices</attribute>
                <attribute name="controller">MWS\NutritionCalculatorBundle\Controller\SexTypeChoices</attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_nc_sex_type_choices_read</attribute>
                    </attribute>
                </attribute>
            </collectionOperation>
            <collectionOperation name="protein_albumin_total_choices">
                <attribute name="method">GET</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_NC_LIST')</attribute>
                <attribute name="path">/pets/protein_albumin_total_choices</attribute>
                <attribute name="controller">MWS\NutritionCalculatorBundle\Controller\ProteinAlbuminTotalChoices</attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_nc_protein_albumin_total_choices_read</attribute>
                    </attribute>
                </attribute>
            </collectionOperation>
        </collectionOperations>
        <itemOperations/>
        <subresourceOperations/>
    </resource>
</resources>

The serialization groups of the pet entity is:

<?xml version="1.0" encoding="UTF-8" ?>

<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
                                http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
    <class name="MWS\NutritionCalculatorBundle\Entity\Pet">
        <attribute name="name">
            <group>mws_nc_pet_list</group>
            <group>mws_nc_recipe_cat_view</group>
            <group>mws_nc_recipe_dog_view</group>
        </attribute>
        <attribute name="sex">
            <group>mws_nc_pet_list</group>
        </attribute>
        <attribute name="birthday">
            <group>mws_nc_pet_list</group>
        </attribute>
        <attribute name="species">
            <group>mws_nc_pet_list</group>
        </attribute>
        <attribute name="breed">
            <group>mws_nc_pet_list</group>
        </attribute>
        <attribute name="diagnosis">
            <group>mws_nc_pet_list</group>
        </attribute>
        <attribute name="currentWeight">
            <group>mws_nc_pet_list</group>
        </attribute>
        <attribute name="targetWeight">
            <group>mws_nc_pet_list</group>
        </attribute>
        <attribute name="petOwner">
            <group>mws_nc_pet_list</group>
        </attribute>
        <attribute name="sexTypeChoices">
            <group>mws_nc_sex_type_choices_read</group>
        </attribute>
        <attribute name="proteinAlbuminTotalChoices">
            <group>mws_nc_protein_albumin_total_choices_read</group>
        </attribute>
    </class>
</serializer>

After the update to APIP 2.4 I only get the iris and the types of the pets. The properties are not serialized anymore. Now the serialization group of the entity level (mws_nc_pet_read) is assigned to serialization instead of the one of the operation level (mws_nc_pet_list).

So what I get ist:

{
  "@context": "/api/contexts/Pet",
  "@id": "/api/pet_owners/1/pets",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/api/dogs/1",
      "@type": "Dog"
    },
    {
      "@id": "/api/dogs/2",
      "@type": "Dog"
    },
    {
      "@id": "/api/cats/3",
      "@type": "Cat"
    }
  ],
  "hydra:totalItems": 3
}

Before the update I got:

{
  "@context": "/api/contexts/Pet",
  "@id": "/api/pet_owners/1/pets",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/api/dogs/1",
      "@type": "Dog",
      "breed": null,
      "name": "Romy",
      "sex": 0,
      "birthday": "2018-05-21T14:25:33+02:00",
      "diagnosis": "DOG_DIAGNOSIS_GROWTH",
      "currentWeight": 10,
      "targetWeight": 10,
      "petOwner": "/api/pet_owners/1",
      "species": "Hund"
    },
    {
      "@id": "/api/dogs/2",
      "@type": "Dog",
      "breed": "/api/dog_breeds/2",
      "name": "Anton",
      "sex": 1,
      "birthday": "2014-08-14T00:00:00+02:00",
      "diagnosis": "DOG_DIAGNOSIS_SENIOR",
      "currentWeight": 25,
      "targetWeight": 20,
      "petOwner": "/api/pet_owners/1",
      "species": "Hund"
    },
    {
      "@id": "/api/cats/3",
      "@type": "Cat",
      "breed": null,
      "name": "Mimi",
      "sex": 0,
      "birthday": "2006-09-19T17:42:14+02:00",
      "diagnosis": "CAT_DIAGNOSIS_SENIOR",
      "currentWeight": 5,
      "targetWeight": 5,
      "petOwner": "/api/pet_owners/1",
      "species": "Katze"
    }
  ],
  "hydra:totalItems": 3
}

Another one is that a change one serialization group it took effect immediately in dev environment. Now I have to clear the cache to see the changes.

@soyuka
Copy link
Member

soyuka commented Mar 25, 2019

Another one is that a change one serialization group it took effect immediately in dev environment. Now I have to clear the cache to see the changes.

🙈 set metadata_cache to false in api platform's configuration. I'm not sure to know how to solve your issue though, I'd need to reproduce the issue.

@nckenn
Copy link

nckenn commented Mar 26, 2019

same issue i have to clear the cache to see the changes, any workaround? i already set metadata_cache to false, but still the same.

@soyuka
Copy link
Member

soyuka commented Mar 26, 2019

We reverted the change in #2648, in the meantime you need to disable the metadata cache: https://api-platform.com/docs/core/performance/#enabling-the-metadata-cache

@nckenn
Copy link

nckenn commented Mar 26, 2019

We reverted the change in #2648, in the meantime you need to disable the metadata cache: https://api-platform.com/docs/core/performance/#enabling-the-metadata-cache

may i know how to disable metadata_cache? i did to add api_platform.metadata_cache: false in my parameters, but still the same, need to clear cache.

@soyuka
Copy link
Member

soyuka commented Mar 26, 2019

normally just set the parameter to false:

# api/config/config.yaml
parameters:
    api_platform.metadata_cache: false

@teohhanhui
Copy link
Contributor

Can you provide a failing Behat test? I think it'd be easier to understand the issue that way. 😄

@soyuka
Copy link
Member

soyuka commented Mar 27, 2019

This should be fixed in 2.4.1, let us know if that's not the case.

@soyuka soyuka closed this as completed Mar 27, 2019
@DougHayward
Copy link

DougHayward commented Mar 27, 2019

Doesn't appear to be fixed in 2.4.1

  • Installing api-platform/core (v2.4.1): Downloading (100%)
  • Installing api-platform/api-pack (dev-master f756ff0): Loading from cache

@teohhanhui
Copy link
Contributor

This should be fixed in 2.4.1, let us know if that's not the case.

@soyuka The cache:clear issue is fixed, but this issue is mainly not about that.

@teohhanhui teohhanhui reopened this Mar 27, 2019
@alanpoulain
Copy link
Member

WDYM by "property[exists]=false is also broken"?

@teohhanhui
Copy link
Contributor

@BonnieDoug Please open another issue for unrelated topics to avoid confusing the discussion here.

Anyway, we have Behat tests for that: https://github.com/api-platform/core/blob/v2.4.1/features/doctrine/exists_filter.feature

@DougHayward
Copy link

DougHayward commented Mar 27, 2019

@alanpoulain @teohhanhui Apologies, my code was broken not the property[exists] filter. Edited my original comment.

However, the serialization of sub-resources is still broken in 2.4.0 and 2.4.1, works fine in 2.3.6.

@teohhanhui
Copy link
Contributor

Could you help us by adding a Behat test for that? :)

@remoteclient
Copy link
Contributor Author

remoteclient commented Mar 27, 2019

This is not fixed by the latest version of apip (2.4.1). The problem with the serialization group on subresources still exists. The other one was still an observation.

@teohhanhui
Copy link
Contributor

@remoteclient

Could you help us by adding a Behat test for that? :)

@remoteclient
Copy link
Contributor Author

remoteclient commented Mar 28, 2019

Shame on me that I never did tests since now. This takes a little bit.
@BonnieDoug as you can confirm the bug.... Can you help here?
I am also still on slack. so if there are any questions, feel free to ping me directly (nic is also remoteclient).

@DougHayward
Copy link

@remoteclient sorry I'm currently completely snowed under trying to complete my current project before I leave my current company in 3 days... Once I've finished and moved to the new company I'll certainly get involved here.

@teohhanhui
Copy link
Contributor

@remoteclient @BonnieDoug Could you check if #2679 fixes your problems?

@remoteclient
Copy link
Contributor Author

@teohhanhui I did a composer update and cleared the cache. I am now on APIP 2.4.2. If this includes the patch, the issue seems not to be fixed.

@teohhanhui
Copy link
Contributor

teohhanhui commented Apr 4, 2019

@remoteclient You have to use the correct branch from my fork, as the PR is not merged yet. :)

@teohhanhui
Copy link
Contributor

@remoteclient The issue is actually api-platform/docs#779

@remoteclient
Copy link
Contributor Author

remoteclient commented Apr 4, 2019

So the issue seems to be fixed. There was a change in the behavior in apip 2.2 but due to a bug it was working like before. After changing <collectionOperations> to subresourceOperations on the subresource it is working like before.

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

No branches or pull requests

6 participants