Skip to content

Identifier is ignored for interface (and maybe inherited resources) #2609

Closed
@Nek-

Description

@Nek-

Related to #2086 for some context.
Consider the following:

# Example extracted from ApiPlatform test suite
# tests/Fixtures/TestBundle/Resources/config/api_resources.yml
resources:
    ApiPlatform\Core\Tests\Fixtures\TestBundle\OtherResources\ResourceInterface:
        itemOperations:
            get:
                method: 'GET'

        collectionOperations:
            get:
                method: 'GET'

        # Add this legit config
        properties:
            foo:
                identifier: true

Output will be without id while it's actually possible to retrieve the id since it is specified.

There are 2 problems leading to this conclusion:

  1. The ResourceClassResolver does not check inheritance. A quick fix (idk if it work for any situation ATM) would be this:
diff --git a/src/Api/ResourceClassResolver.php b/src/Api/ResourceClassResolver.php
index f75f5822..a0a45722 100644
--- a/src/Api/ResourceClassResolver.php
+++ b/src/Api/ResourceClassResolver.php
@@ -50,6 +50,7 @@ final class ResourceClassResolver implements ResourceClassResolverInterface
         if (
             null === $type
             || ((!$strict || $resourceClass === $type) && $isResourceClass = $this->isResourceClass($type))
+            || is_subclass_of($value, $resourceClass)
         ) {
             return $resourceClass;
         }
  1. The IriConverter uses only the actual class of the item to match ressource instead of also checking inheritance

There's maybe a bunch of other issues I'm not aware of, but fixing this should fix the main part of it.

This is probably related to another issue The following test does not check the id and guess what, it's actually wrong !

# features/main/table_inheritance.feature:294
   Scenario: Get the parent interface collection
     When I send a "GET" request to "/resource_interfaces"
     Then the response status code should be 200
     And the response should be in JSON
     And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
     And the JSON should be valid according to this schema:
     """
     {
       "type": "object",
       "properties": {
         "hydra:member": {
           "type": "array",
           "items": {
             "type": "object",
             "properties": {
               "@type": {
                 "type": "string",
                 "pattern": "^ResourceInterface$"
               },
               "@id": {
                 "type": "string",
                 "pattern": "^_:"
               },
               "foo": {
                 "type": "string",
                 "required": "true"
               }
             }
           },
           "minItems": 1
         }
       },
       "required": ["hydra:member"]
     }
     """

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions