Skip to content

feat: json streamer #7225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,14 @@
"symfony/expression-language": "^6.4 || ^7.0",
"symfony/finder": "^6.4 || ^7.0",
"symfony/form": "^6.4 || ^7.0",
"symfony/framework-bundle": "^6.4 || ^7.0",
"symfony/framework-bundle": "7.4.x-dev",
"symfony/http-client": "^6.4 || ^7.0",
"symfony/intl": "^6.4 || ^7.0",
"symfony/json-streamer": "7.4.x-dev",
"symfony/maker-bundle": "^1.24",
"symfony/mercure-bundle": "*",
"symfony/messenger": "^6.4 || ^7.0",
"symfony/object-mapper": "^7.3",
"symfony/object-mapper": "7.4.x-dev",
"symfony/routing": "^6.4 || ^7.0",
"symfony/security-bundle": "^6.4 || ^7.0",
"symfony/security-core": "^6.4 || ^7.0",
Expand Down
43 changes: 43 additions & 0 deletions src/Hydra/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Hydra;

use Symfony\Component\JsonStreamer\Attribute\StreamedName;

/**
* @template T
*
* @internal
*/
class Collection
{
#[StreamedName('@context')]
public string $context = 'VIRTUAL';

#[StreamedName('@id')]
public CollectionId $id = CollectionId::VALUE;

#[StreamedName('@type')]
public string $type = 'Collection';

public float $totalItems;

public ?IriTemplate $search = null;
public ?PartialCollectionView $view = null;

/**
* @var list<T>
*/
public iterable $member;
}
19 changes: 19 additions & 0 deletions src/Hydra/CollectionId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Hydra;

enum CollectionId
{
case VALUE;
}
32 changes: 32 additions & 0 deletions src/Hydra/IriTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Hydra;

use Symfony\Component\JsonStreamer\Attribute\StreamedName;
use Symfony\Component\Serializer\Annotation\SerializedName;

final class IriTemplate
{
#[StreamedName('@type')]
#[SerializedName('@type')]
public string $type = 'IriTemplate';

public function __construct(
public string $variableRepresentation,
/** @var list<IriTemplateMapping> */
public array $mapping = [],
public ?string $template = null,
) {
}
}
31 changes: 31 additions & 0 deletions src/Hydra/IriTemplateMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Hydra;

use Symfony\Component\JsonStreamer\Attribute\StreamedName;
use Symfony\Component\Serializer\Annotation\SerializedName;

class IriTemplateMapping
{
#[StreamedName('@type')]
#[SerializedName('@type')]
public string $type = 'IriTemplateMapping';

public function __construct(
public string $variable,
public ?string $property,
public bool $required = false,
) {
}
}
36 changes: 36 additions & 0 deletions src/Hydra/PartialCollectionView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Hydra;

use Symfony\Component\JsonStreamer\Attribute\StreamedName;

class PartialCollectionView
{
#[StreamedName('@type')]
public string $type = 'PartialCollectionView';

public function __construct(
#[StreamedName('@id')]
public string $id,
#[StreamedName('first')]
public ?string $first = null,
#[StreamedName('last')]
public ?string $last = null,
#[StreamedName('previous')]
public ?string $previous = null,
#[StreamedName('next')]
public ?string $next = null,
) {
}
}
95 changes: 9 additions & 86 deletions src/Hydra/Serializer/CollectionFiltersNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\State\Util\StateOptionsTrait;
use ApiPlatform\Hydra\State\Util\SearchHelperTrait;
use Psr\Container\ContainerInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
Expand All @@ -35,6 +36,7 @@ final class CollectionFiltersNormalizer implements NormalizerInterface, Normaliz
{
use HydraPrefixTrait;
use StateOptionsTrait;
use SearchHelperTrait;
private ?ContainerInterface $filterLocator = null;

/**
Expand Down Expand Up @@ -105,7 +107,13 @@ public function normalize(mixed $object, ?string $format = null, array $context

if ($currentFilters || ($parameters && \count($parameters))) {
$hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
$data[$hydraPrefix.'search'] = $this->getSearch($resourceClass, $requestParts, $currentFilters, $parameters, $hydraPrefix);
['mapping' => $mapping, 'keys' => $keys] = $this->getSearchMappingAndKeys($operation, $resourceClass, $currentFilters, $parameters, [$this, 'getFilter']);
$data[$hydraPrefix.'search'] = [
'@type' => $hydraPrefix.'IriTemplate',
$hydraPrefix.'template' => \sprintf('%s{?%s}', $requestParts['path'], implode(',', $keys)),
$hydraPrefix.'variableRepresentation' => 'BasicRepresentation',
$hydraPrefix.'mapping' => $mapping,
];
}

return $data;
Expand All @@ -121,91 +129,6 @@ public function setNormalizer(NormalizerInterface $normalizer): void
}
}

/**
* Returns the content of the Hydra search property.
*
* @param FilterInterface[] $filters
*/
private function getSearch(string $resourceClass, array $parts, array $filters, ?Parameters $parameters, string $hydraPrefix): array
{
$variables = [];
$mapping = [];
foreach ($filters as $filter) {
foreach ($filter->getDescription($resourceClass) as $variable => $data) {
$variables[] = $variable;
$mapping[] = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $data['property'] ?? null, 'required' => $data['required'] ?? false];
}
}

foreach ($parameters ?? [] as $key => $parameter) {
// Each IriTemplateMapping maps a variable used in the template to a property
if (!$parameter instanceof QueryParameterInterface || false === $parameter->getHydra()) {
continue;
}

if (($filterId = $parameter->getFilter()) && \is_string($filterId) && ($filter = $this->getFilter($filterId))) {
$filterDescription = $filter->getDescription($resourceClass);

foreach ($filterDescription as $variable => $description) {
// // This is a practice induced by PHP and is not necessary when implementing URI template
if (str_ends_with((string) $variable, '[]')) {
continue;
}

if (!($descriptionProperty = $description['property'] ?? null)) {
continue;
}

if (($prop = $parameter->getProperty()) && $descriptionProperty !== $prop) {
continue;
}

// :property is a pattern allowed when defining parameters
$k = str_replace(':property', $descriptionProperty, $key);
$variable = str_replace($descriptionProperty, $k, $variable);
$variables[] = $variable;
$m = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $descriptionProperty];
if (null !== ($required = $parameter->getRequired() ?? $description['required'] ?? null)) {
$m['required'] = $required;
}
$mapping[] = $m;
}

if ($filterDescription) {
continue;
}
}

if (str_contains($key, ':property') && $parameter->getProperties()) {
$required = $parameter->getRequired();
foreach ($parameter->getProperties() as $prop) {
$k = str_replace(':property', $prop, $key);
$m = ['@type' => 'IriTemplateMapping', 'variable' => $k, 'property' => $prop];
$variables[] = $k;
if (null !== $required) {
$m['required'] = $required;
}
$mapping[] = $m;
}

continue;
}

if (!($property = $parameter->getProperty())) {
continue;
}

$m = ['@type' => 'IriTemplateMapping', 'variable' => $key, 'property' => $property];
$variables[] = $key;
if (null !== ($required = $parameter->getRequired())) {
$m['required'] = $required;
}
$mapping[] = $m;
}

return ['@type' => $hydraPrefix.'IriTemplate', $hydraPrefix.'template' => \sprintf('%s{?%s}', $parts['path'], implode(',', $variables)), $hydraPrefix.'variableRepresentation' => 'BasicRepresentation', $hydraPrefix.'mapping' => $mapping];
}

/**
* Gets a filter with a backward compatibility.
*/
Expand Down
Loading
Loading