Skip to content

empty response array with unmapped entity #467

@remoteclient

Description

@remoteclient

Hey there,
I want to expose the symfony security role hierarchy to the client. The route should be /api/role_hierarchy. When the client gets the hierarchy, it can select one or more roles and assign them to one user.

The get started my goal is to expose a static array first and if this works, replace the static array with a appropriate function.

My api_resources.xml (relevant part is on the end):

<?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\UserBundle\Entity\AdminUser">
        <attribute name="access_control">is_granted('ROLE_MWS_USER_USER_GUEST')</attribute>
        <collectionOperations>
            <collectionOperation name="get">
                <attribute name="method">GET</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_USER_LIST')</attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_user_list</attribute>
                    </attribute>
                </attribute>
            </collectionOperation>
            <collectionOperation name="post">
                <attribute name="method">POST</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_USER_CREATE')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_user_create</attribute>
                    </attribute>
                </attribute>
                <attribute name="validation_groups">
                    <attribute>Registration</attribute>
                </attribute>
            </collectionOperation>
        </collectionOperations>
        <itemOperations>
            <itemOperation name="get">
                <attribute name="method">GET</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_USER_VIEW')</attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_user_view</attribute>
                    </attribute>
                </attribute>
            </itemOperation>
            <itemOperation name="put">
                <attribute name="method">PUT</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_USER_EDIT')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_user_edit</attribute>
                    </attribute>
                </attribute>
                <attribute name="validation_groups">
                    <attribute>Profile</attribute>
                </attribute>
            </itemOperation>
            <itemOperation name="delete">
                <attribute name="method">DELETE</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_USER_DELETE')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_user_delete</attribute>
                    </attribute>
                </attribute>
            </itemOperation>
        </itemOperations>
    </resource>

    <resource class="MWS\UserBundle\Entity\AdminGroup">
        <attribute name="access_control">is_granted('ROLE_MWS_USER_GROUP_GUEST')</attribute>
        <attribute name="validation_groups">
            <attribute>Registration</attribute>
        </attribute>
        <collectionOperations>
            <collectionOperation name="get">
                <attribute name="method">GET</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_GROUP_LIST')</attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_group_list</attribute>
                    </attribute>
                </attribute>
            </collectionOperation>
            <collectionOperation name="post">
                <attribute name="method">POST</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_GROUP_CREATE')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_group_create</attribute>
                    </attribute>
                </attribute>
            </collectionOperation>
        </collectionOperations>
        <itemOperations>
            <itemOperation name="get">
                <attribute name="method">GET</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_GROUP_VIEW')</attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_group_view</attribute>
                    </attribute>
                </attribute>
            </itemOperation>
            <itemOperation name="put">
                <attribute name="method">PUT</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_GROUP_EDIT')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_group_edit</attribute>
                    </attribute>
                </attribute>
            </itemOperation>
            <itemOperation name="delete">
                <attribute name="method">DELETE</attribute>
                <attribute name="access_control">is_granted('ROLE_MWS_USER_ADMIN_GROUP_DELETE')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute name="group">mws_user_admin_group_delete</attribute>
                    </attribute>
                </attribute>
            </itemOperation>
        </itemOperations>
    </resource>
    <resource class="MWS\UserBundle\Model\RolesHierarchy">
        <itemOperations/>
        <collectionOperations>
            <collectionOperation name="get">
                <attribute name="method">GET</attribute>
                <attribute name="path">/roles_hierarchy</attribute>
            </collectionOperation>
        </collectionOperations>
    </resource>
</resources>

My Entity:

<?php

namespace MWS\UserBundle\Model;

class RolesHierarchy
{
    private $id;

    private $roles;

    /**
     * @return integer
     */
    public function getId()
    {
        return 1;
    }

    /**
     * @return array
     */
    public function getRoles()
    {
        $this->roles = array(0=>array(0=>'string'), 1=>34);
        return $this->roles;
    }

}

And the entity xml file:

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="MWS\UserBundle\Model\RolesHierarchy">

        <field name="id" type="integer" />

        <field name="roles" type="array" />

    </entity>

</doctrine-mapping>

When I try to get the result via swagger-ui I always get an empty array as result:

{
  "@context": "/api/contexts/RolesHierarchy",
  "@id": "/api/roles_hierarchy",
  "@type": "hydra:Collection",
  "hydra:member": [],
  "hydra:totalItems": 0
}

Does anybody know how to get unmapped entities working?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions