Core: properties of subresource not serialized after update to APIP 2.4

Created on 24 Mar 2019  路  22Comments  路  Source: api-platform/core

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.

bug question

All 22 comments

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.

:see_no_evil: 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.

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.

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

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.

normally just set the parameter to false:

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

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

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

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

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.

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

@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

@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.

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

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.

@remoteclient

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

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).

@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.

@remoteclient @BonnieDoug Could you check if https://github.com/api-platform/core/pull/2679 fixes your problems?

@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.

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

@remoteclient The issue is actually https://github.com/api-platform/docs/pull/779

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.

Was this page helpful?
0 / 5 - 0 ratings