Skip to content

Conversation

@soyuka
Copy link
Member

@soyuka soyuka commented Sep 8, 2025

Needs test:

<?php

namespace App\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use App\Entity\Product\Product as EntityProduct;
use App\State\GetProductBySlugProvider;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[Get(
    types: ['https://schema.org/Product'],
    uriTemplate: '/products/{code}',
    uriVariables: ['code'],
    provider: GetProductBySlugProvider::class,
    // jsonStream: true
)]
class Product
{
    public string $id;
    #[ApiProperty(identifier: true)]
    public string $code;

    #[Map(source: 'averageRating', transform: [self::class, 'getAggregateRating'])]
    #[ApiProperty(genId: false, iris: ['https://schema.org/aggregateRating'])]
    public AggregateRating $aggregateRating;

    #[ApiProperty(property: 'name', iris: ['https://schema.org/name'])]
    public string $name;

    /**
     * @param EntityProduct $source
     */
    public static function getAggregateRating(mixed $value, object $source, ?object $target): AggregateRating
    {
        return new AggregateRating($value, \count($source->getReviews()));
    }
}

@soyuka soyuka force-pushed the fixes/jsonstream branch 2 times, most recently from 0f1360e to 73027e0 Compare September 9, 2025 11:44
@soyuka soyuka merged commit 5a8d4d2 into api-platform:4.2 Sep 9, 2025
111 of 114 checks passed
@soyuka soyuka deleted the fixes/jsonstream branch September 9, 2025 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant