Skip to main content
Version: Latest
FeatureAIv1.4.3open-dxp/headless-bundle

Response Entities

A response entity describes what an endpoint answers with: one public property per field.

class StoreItem implements ResponseEntityInterface
{
public string $name;
public ?string $city = null;
}

That is all you need as long as the source object has a getName() and a getCity(). Paths work too: source: 'address.zip' walks getAddress()->getZip(), and get, is and has are all tried.

Resolution order​

Each property is filled in this order:

source → sourceResolver → transformer → responseMappingClass

Use a source resolver when what the getter returns is not yet something responseMappingClass can map. It runs once on the whole value, before a collection is split into items:

#[ApiTransformerProperty(
sourceResolver: FieldCollectionSourceResolver::class,
responseMappingClass: OpeningHour::class,
isCollection: true,
)]
public array $openingHours;

The bundle ships resolvers for the usual containers: Fieldcollection, ImageGallery, Doctrine collections, json strings, timestamps. Write your own when the list has to be fetched first, from an id or from $context['entity'].

Use a transformer to bring a single value into its final shape. It runs per item and works on any class, so it can be reused. Pass it the source object with source: self::SOURCE_ENTITY:

#[ApiTransformerProperty(source: self::SOURCE_ENTITY, transformer: ElementUrlTransformer::class)]
public ?string $frontendPath;

source: self::SOURCE_ENTITY skips the source resolver. Using both on one property does nothing.

The two attributes​

ApiTransformerPropertyApiResponseEntityProperty
sourceyesyes
sourceResolveryesyes
transformeryesno
nested entityresponseMappingClassclass
collectionisCollection: trueproperty typed as array
several typesarray plus discriminatorno

For several types, pass an array of classes and a discriminator. The denormalizer picks by that field and the specification gets a oneOf. An array of classes counts as a collection unless isCollection says otherwise.

Always name the bundle class in responseMappingClass, even when your project overrides it. The name passes through response_entity_mapping anyway, so you keep one switch point instead of two.

Overriding an entity of a bundle​

The class alone has no effect. It needs the mapping entry as well:

class EcommerceProduct extends \OpenDxp\Bundle\HeadlessEcommerceBundle\Domain\Product\ResponseEntity\EcommerceProduct
{
public ?bool $isNew = null;
}
opendxp_headless:
response_entity_mapping:
OpenDxp\...\ResponseEntity\EcommerceProduct: App\Domain\Product\ResponseEntity\EcommerceProduct

make:response-entity --extends writes both. The parent is fully qualified because an override usually shares its short name, and importing that name would be a fatal error.

See Response Entity Mapping.

Adding a type to a route of a bundle​

Some routes of a bundle answer with one class per type. /assets/{id}/metadata is one of them. Its response_class maps each metadata type to a class, and the field type decides which class applies.

If your project registers its own asset metadata type, add an entry for it. The bundle keeps its own entries and adds yours:

opendxp_headless:
api_routes:
asset_metadata:
action_response:
response_class:
isbn: App\Domain\Asset\ResponseEntity\IsbnAssetMetadata

The class extends AbstractOpenDxpAssetMetadata and declares the type and the data:

use OpenApi\Attributes as OA;
use OpenDxp\Bundle\HeadlessBundle\Domain\Asset\ResponseEntity\AbstractOpenDxpAssetMetadata;

class IsbnAssetMetadata extends AbstractOpenDxpAssetMetadata
{
#[OA\Property(enum: ['isbn'])]
public ?string $type = null;

public ?string $data = null;
}

The API documentation lists the class next to the classes of the bundle. A type without an entry fails with the message The response class has no entry for the type "isbn".

OpenAPI​

A typed property needs no #[OA\Property]. The describer builds the schema from the attribute: it registers the mapping class, turns isCollection into an array with items, an array of classes into oneOf, and keeps nullable.

It gives up on one case: a property typed array or mixed without a mapping class. The specification then has an array without items, and a generated client makes any[] of it. Describe those yourself:

#[ApiTransformerProperty(transformer: AssetThumbnailTransformer::class, isCollection: true)]
#[OA\Property(type: 'array', items: new OA\Items(properties: [
new OA\Property(property: 'video', ref: '#/components/schemas/CmsEditableStandardizedHeadlessVideo'),
]))]
public ?array $videos = null;

headless_lint_response_entities finds these, and overrides missing their mapping entry. See Development Tools.