phpsymfonyapi-platform.com

How do I configure the identifier for a custom DTO operation without using annotations?


Created a very simple resource based on a DTO. All the logic is going to be provided via a custom DataProvider, and for this project no annotations are used.

The configuration for the resource is:

<resources xmlns="https://api-platform.com/schema/metadata">
    <resource class="App\Domain\Dto\ProductUpgradePrice">

        <collectionOperations>
            <collectionOperation name="get"/>
        </collectionOperations>

        <itemOperations>
            <itemOperation name="get"/>
        </itemOperations>

    </resource>
</resources>

The actual DTO is:

<?php declare(strict_types=1);

namespace App\Domain\Dto;

/** @psalm-immutable */
class ProductUpgradePrice
{

    public function __construct(
        public string $productId,
        public int $regularPrice,
        public int $upgradePrice
    ) {}

}

When I try to GET /api/product_upgrade_prices I get:

No identifiers defined for resource of type App\Domain\Dto\ProductUpgradePrice

Which makes sense.

The docs mention using the annotation @ApiProperty, but as I mentioned earlier, no annotations for this project. (I submitted a PR to update the docs since I posted the question, which was merged, so hopefully future users will not get in the same situation)

I've tried adding this to the resource configuration:

<property name="productId">
    <attribute name="identifier">true</attribute>
</property>

But got the same results.

How could I configure this without using annotations?


Solution

  • Looking at the metadata XSD, in line 113 you can see this attribute for the property element:

    <xsd:attribute type="xsd:boolean" name="identifier"/>
    

    Which mean you should use this instead:

    <property identifier="true" name="id" />