How to have multiple resources with the same base class name in ApiPlatform

33 views Asked by At

I have two resource classes, representing different things with different properties. I want to name them the same, but put them in different namespaces. For example, I have a Foo\Item and a Bar\Item:

<?php

namespace App\ApiResource\Foo;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;

#[ApiResource(
    operations: [
        new Get(),
    ],
    routePrefix: 'foo',
    uriTemplate: '/item/{key}',
)]
class Item
{
    #[ApiProperty(openapiContext: ['example' => 'foo key'])]
    public string $key;

    #[ApiProperty(openapiContext: ['example' => 'foo description'])]
    public string $description;
}
<?php

namespace App\ApiResource\Bar;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;

#[ApiResource(
    operations: [
        new Get(),
    ],
    routePrefix: 'bar',
    uriTemplate: '/item/{id}',
)]
class Item
{
    #[ApiProperty(openapiContext: ['example' => 'bar id'])]
    public string $id;

    #[ApiProperty(openapiContext: ['example' => 'bar name'])]
    public string $name;
}

This works fine from an operational standpoint, all of my endpoints work as expected. However, since ApiPlatform uses the resource's base class name as the OpenApi schema name, the autogen documentation is incomplete -- it contains only the alphabetically first item:

{
   "components" : {
      "schemas" : {
         "Item" : {
            "deprecated" : false,
            "description" : "",
            "properties" : {
               "id" : {
                  "example" : "bar id",
                  "type" : "string"
               },
               "name" : {
                  "example" : "bar name",
                  "type" : "string"
               }
            },
            "type" : "object"
         },

This results in both Item resources having documented endpoints but only one of them having a documented schema: enter image description here

Questions:

  • Am I out of luck here? Will I have to rename my resource classes to be unique across all namespaces, like Foo\FooItem and Bar\BarItem, and subsequently have the docs reflect that?
  • Is there perhaps some way to instruct ApiPlatform to separate/group namespaces, such that all the /api/foo/* endpoints belong to one schema group and all the /api/bar/* endpoints belong to a different one, and they each get their own set of docs?
  • Should this be considered a bug? If this is not supported, and resource names need to be unique, should ApiPlatform have detected these conflicting names and issued an error?
0

There are 0 answers