diff --git a/src/JsonLd/ContextBuilder.php b/src/JsonLd/ContextBuilder.php index 82f90e6f21b..7d3a4aa3fd2 100644 --- a/src/JsonLd/ContextBuilder.php +++ b/src/JsonLd/ContextBuilder.php @@ -14,6 +14,7 @@ namespace ApiPlatform\JsonLd; use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait; +use ApiPlatform\JsonLd\Serializer\PotentialActionNormalizer; use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\HttpOperation; use ApiPlatform\Metadata\IriConverterInterface; @@ -88,7 +89,18 @@ public function getResourceContext(string $resourceClass, int $referenceType = U // Every operation resolves to this same /contexts/{shortName} URI, so union their groups. $sharedGroups = $this->getOperationsGroups($resourceMetadataCollection, $operation->getShortName()); - return $this->buildOperationContext($operation, $resourceClass, $referenceType, $sharedGroups); + $context = $this->buildOperationContext($operation, $resourceClass, $referenceType, $sharedGroups); + foreach ($resourceMetadataCollection as $resource) { + foreach ($resource->getOperations() ?? [] as $resourceOperation) { + if (true === ($resourceOperation->getExtraProperties()[PotentialActionNormalizer::EXTRA_PROPERTY] ?? false)) { + $context['potentialAction'] = PotentialActionNormalizer::JSONLD_CONTEXT; + + return $context; + } + } + } + + return $context; } /** diff --git a/src/JsonLd/Serializer/PotentialActionNormalizer.php b/src/JsonLd/Serializer/PotentialActionNormalizer.php new file mode 100644 index 00000000000..1eb5b6055f3 --- /dev/null +++ b/src/JsonLd/Serializer/PotentialActionNormalizer.php @@ -0,0 +1,193 @@ + + * + * 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\JsonLd\Serializer; + +use ApiPlatform\Metadata\HttpOperation; +use ApiPlatform\Metadata\IdentifiersExtractorInterface; +use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; +use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; +use ApiPlatform\Metadata\UrlGeneratorInterface; +use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface; +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Symfony\Component\Serializer\SerializerAwareInterface; +use Symfony\Component\Serializer\SerializerInterface; + +/** + * Advertises the operations flagged with the "potential_action" extra property as schema.org potential actions. + * + * @see https://schema.org/docs/actions.html + */ +final class PotentialActionNormalizer implements NormalizerInterface, NormalizerAwareInterface, SerializerAwareInterface +{ + public const EXTRA_PROPERTY = 'potential_action'; + + public const JSONLD_CONTEXT = [ + '@id' => 'https://schema.org/potentialAction', + '@context' => ['@vocab' => 'https://schema.org/', 'schema' => 'https://schema.org/'], + ]; + + private const DEFAULT_TYPES = [ + 'POST' => 'schema:CreateAction', + 'PUT' => 'schema:ReplaceAction', + 'PATCH' => 'schema:UpdateAction', + 'DELETE' => 'schema:DeleteAction', + ]; + + public function __construct( + private readonly NormalizerInterface $decorated, + private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, + private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, + private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, + private readonly IdentifiersExtractorInterface $identifiersExtractor, + private readonly UrlGeneratorInterface $urlGenerator, + private readonly ?ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, + private readonly ?NameConverterInterface $nameConverter = null, + ) { + } + + public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null + { + $normalizedData = $this->decorated->normalize($data, $format, $context); + + $operation = $context['operation'] ?? null; + if (!\is_array($normalizedData) || !$operation instanceof HttpOperation || 'GET' !== $operation->getMethod() || null === $operation->getClass() || isset($context['api_sub_level']) || isset($context['api_collection_sub_level'])) { + return $normalizedData; + } + + $isCollection = is_iterable($data); + $actions = []; + foreach ($this->resourceMetadataCollectionFactory->create($operation->getClass()) as $resource) { + foreach ($resource->getOperations() ?? [] as $actionOperation) { + if (!$actionOperation instanceof HttpOperation || true !== ($actionOperation->getExtraProperties()[self::EXTRA_PROPERTY] ?? false) || $isCollection === (bool) $actionOperation->getUriVariables()) { + continue; + } + + if (null !== $target = $this->getTarget($isCollection ? null : $data, $actionOperation)) { + array_push($actions, ...$this->getActions($actionOperation, $target)); + } + } + } + + if ($actions) { + $normalizedData['potentialAction'] = $actions; + } + + return $normalizedData; + } + + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool + { + return $this->decorated->supportsNormalization($data, $format, $context); + } + + public function getSupportedTypes(?string $format): array + { + return $this->decorated->getSupportedTypes($format); + } + + public function setNormalizer(NormalizerInterface $normalizer): void + { + if ($this->decorated instanceof NormalizerAwareInterface) { + $this->decorated->setNormalizer($normalizer); + } + } + + public function setSerializer(SerializerInterface $serializer): void + { + if ($this->decorated instanceof SerializerAwareInterface) { + $this->decorated->setSerializer($serializer); + } + } + + private function getTarget(?object $item, HttpOperation $operation): ?array + { + try { + $identifiers = $item ? $this->identifiersExtractor->getIdentifiersFromItem($item, $operation) : []; + $url = $this->urlGenerator->generate($operation->getRouteName() ?? $operation->getName(), $identifiers, $operation->getUrlGenerationStrategy() ?? UrlGeneratorInterface::ABS_PATH); + } catch (\InvalidArgumentException|\RuntimeException) { + // the item does not carry the operation's URI variables + return null; + } + + $target = ['@type' => 'EntryPoint', 'urlTemplate' => $url, 'httpMethod' => $operation->getMethod()]; + if ($encodingType = $this->getMimeTypes($operation->getInputFormats())) { + $target['encodingType'] = $encodingType; + } + if ($contentType = $this->getMimeTypes($operation->getOutputFormats())) { + $target['contentType'] = $contentType; + } + + return $target; + } + + /** + * @return list> + */ + private function getActions(HttpOperation $operation, array $target): array + { + $inputClass = $operation->getInput()['class'] ?? $operation->getClass(); + $mapping = $inputClass ? $this->classDiscriminatorResolver?->getMappingForClass($inputClass) : null; + + if ('@type' === $mapping?->getTypeProperty()) { + $actions = []; + foreach ($mapping->getTypesMapping() as $type => $class) { + $actions[] = ['@type' => $type, 'target' => $target] + $this->getInputAnnotations($class, $operation); + } + + return $actions; + } + + $hydraTypes = array_filter((array) ($operation->getHydraContext()['@type'] ?? []), static fn ($type): bool => \is_string($type) && !str_starts_with($type, 'hydra:')); + $type = reset($hydraTypes) ?: self::DEFAULT_TYPES[$operation->getMethod()] ?? 'schema:Action'; + + return [['@type' => $type, 'target' => $target] + ($inputClass ? $this->getInputAnnotations($inputClass, $operation) : [])]; + } + + /** + * @return array + */ + private function getInputAnnotations(string $class, HttpOperation $operation): array + { + $options = ($groups = $operation->getDenormalizationContext()['groups'] ?? null) ? ['serializer_groups' => (array) $groups] : []; + + $annotations = []; + foreach ($this->propertyNameCollectionFactory->create($class, $options) as $property) { + $propertyMetadata = $this->propertyMetadataFactory->create($class, $property, $options); + if ($propertyMetadata->isRequired() && false !== $propertyMetadata->isWritable()) { + $annotations[($this->nameConverter?->normalize($property, $class, ItemNormalizer::FORMAT) ?? $property).'-input'] = 'required'; + } + } + + return $annotations; + } + + /** + * @param array>|null $formats + * + * @return string|list|null + */ + private function getMimeTypes(?array $formats): string|array|null + { + $mimeTypes = array_values(array_unique(array_merge(...array_map(static fn ($mimeTypes): array => (array) $mimeTypes, array_values($formats ?? []))))); + + return match (\count($mimeTypes)) { + 0 => null, + 1 => $mimeTypes[0], + default => $mimeTypes, + }; + } +} diff --git a/src/Laravel/ApiPlatformProvider.php b/src/Laravel/ApiPlatformProvider.php index 0aa864d60df..99f50f585d8 100644 --- a/src/Laravel/ApiPlatformProvider.php +++ b/src/Laravel/ApiPlatformProvider.php @@ -76,6 +76,7 @@ use ApiPlatform\JsonLd\Serializer\ItemDenormalizer as JsonLdItemDenormalizer; use ApiPlatform\JsonLd\Serializer\ItemNormalizer as JsonLdItemNormalizer; use ApiPlatform\JsonLd\Serializer\ObjectNormalizer as JsonLdObjectNormalizer; +use ApiPlatform\JsonLd\Serializer\PotentialActionNormalizer as JsonLdPotentialActionNormalizer; use ApiPlatform\JsonSchema\DefinitionNameFactory; use ApiPlatform\JsonSchema\DefinitionNameFactoryInterface; use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory; @@ -217,6 +218,7 @@ use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Serializer\Encoder\CsvEncoder; +use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader; @@ -1146,15 +1148,26 @@ public function register(): void }); $this->app->singleton('api_platform_normalizer_list', static function (Application $app) { + $withPotentialActions = static fn (NormalizerInterface $normalizer): JsonLdPotentialActionNormalizer => new JsonLdPotentialActionNormalizer( + $normalizer, + $app->make(ResourceMetadataCollectionFactoryInterface::class), + $app->make(PropertyNameCollectionFactoryInterface::class), + $app->make(PropertyMetadataFactoryInterface::class), + $app->make(IdentifiersExtractorInterface::class), + $app->make(UrlGeneratorInterface::class), + new ClassDiscriminatorFromClassMetadata($app->make(ClassMetadataFactoryInterface::class)), + $app->make(NameConverterInterface::class), + ); + $list = new \SplPriorityQueue(); $list->insert($app->make(HydraEntrypointNormalizer::class), -800); - $list->insert($app->make(HydraPartialCollectionViewNormalizer::class), -800); + $list->insert($withPotentialActions($app->make(HydraPartialCollectionViewNormalizer::class)), -800); $list->insert($app->make(HydraCollectionObjectNormalizer::class), -984); $list->insert($app->make(HalCollectionNormalizer::class), -800); $list->insert($app->make(HalEntrypointNormalizer::class), -985); $list->insert($app->make(HalObjectNormalizer::class), -995); $list->insert($app->make(HalItemNormalizer::class), -890); - $list->insert($app->make(JsonLdItemNormalizer::class), -890); + $list->insert($withPotentialActions($app->make(JsonLdItemNormalizer::class)), -890); $list->insert($app->make(JsonLdItemDenormalizer::class), -889); $list->insert($app->make(JsonLdObjectNormalizer::class), -995); $list->insert($app->make(ArrayDenormalizer::class), -990); diff --git a/src/Symfony/Bundle/Resources/config/hydra.php b/src/Symfony/Bundle/Resources/config/hydra.php index 1ce0d98c9d5..113af370ffa 100644 --- a/src/Symfony/Bundle/Resources/config/hydra.php +++ b/src/Symfony/Bundle/Resources/config/hydra.php @@ -22,6 +22,7 @@ use ApiPlatform\Hydra\Serializer\EntrypointNormalizer; use ApiPlatform\Hydra\Serializer\HydraPrefixNameConverter; use ApiPlatform\Hydra\Serializer\PartialCollectionViewNormalizer; +use ApiPlatform\JsonLd\Serializer\PotentialActionNormalizer; return static function (ContainerConfigurator $container) { $services = $container->services(); @@ -104,6 +105,19 @@ '%api_platform.serializer.default_context%', ]); + $services->set('api_platform.hydra.normalizer.collection.potential_action', PotentialActionNormalizer::class) + ->decorate('api_platform.hydra.normalizer.collection', null, 0) + ->args([ + service('api_platform.hydra.normalizer.collection.potential_action.inner'), + service('api_platform.metadata.resource.metadata_collection_factory'), + service('api_platform.metadata.property.name_collection_factory'), + service('api_platform.metadata.property.metadata_factory'), + service('api_platform.api.identifiers_extractor'), + service('api_platform.router'), + service('serializer.mapping.class_discriminator_resolver')->ignoreOnInvalid(), + service('api_platform.name_converter')->ignoreOnInvalid(), + ]); + $services->set('api_platform.hydra.name_converter.hydra_prefix', HydraPrefixNameConverter::class) ->decorate('api_platform.name_converter', null, 0) ->args([ diff --git a/src/Symfony/Bundle/Resources/config/jsonld.php b/src/Symfony/Bundle/Resources/config/jsonld.php index 368295f0ea8..470251499fa 100644 --- a/src/Symfony/Bundle/Resources/config/jsonld.php +++ b/src/Symfony/Bundle/Resources/config/jsonld.php @@ -18,6 +18,7 @@ use ApiPlatform\JsonLd\Serializer\ItemDenormalizer; use ApiPlatform\JsonLd\Serializer\ItemNormalizer; use ApiPlatform\JsonLd\Serializer\ObjectNormalizer; +use ApiPlatform\JsonLd\Serializer\PotentialActionNormalizer; use ApiPlatform\Serializer\JsonEncoder; use ApiPlatform\Symfony\Validator\Serializer\ValidationExceptionNormalizer; @@ -56,6 +57,19 @@ ->tag('serializer.normalizer', ['priority' => -890]) ->tag('kernel.reset', ['method' => '?reset']); + $services->set('api_platform.jsonld.normalizer.item.potential_action', PotentialActionNormalizer::class) + ->decorate('api_platform.jsonld.normalizer.item', null, 0) + ->args([ + service('api_platform.jsonld.normalizer.item.potential_action.inner'), + service('api_platform.metadata.resource.metadata_collection_factory'), + service('api_platform.metadata.property.name_collection_factory'), + service('api_platform.metadata.property.metadata_factory'), + service('api_platform.api.identifiers_extractor'), + service('api_platform.router'), + service('serializer.mapping.class_discriminator_resolver')->ignoreOnInvalid(), + service('api_platform.name_converter')->ignoreOnInvalid(), + ]); + $services->set('api_platform.jsonld.denormalizer.item', ItemDenormalizer::class) ->args([ service('api_platform.metadata.resource.metadata_collection_factory'), diff --git a/tests/Fixtures/TestBundle/ApiResource/PotentialAction/ActionableBook.php b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/ActionableBook.php new file mode 100644 index 00000000000..d82e120fef6 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/ActionableBook.php @@ -0,0 +1,88 @@ + + * + * 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\Tests\Fixtures\TestBundle\ApiResource\PotentialAction; + +use ApiPlatform\Metadata\ApiProperty; +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Metadata\Link; +use ApiPlatform\Metadata\Operation; +use ApiPlatform\Metadata\Post; +use Symfony\Component\Validator\Constraints as Assert; + +#[ApiResource( + operations: [ + new Get(uriTemplate: '/actionable_books/{id}', provider: [self::class, 'provide']), + new GetCollection(uriTemplate: '/actionable_books', provider: [self::class, 'provideCollection']), + new Post(uriTemplate: '/actionable_books', processor: [self::class, 'process'], extraProperties: ['potential_action' => true]), + new Post( + uriTemplate: '/actionable_books/{id}/reviews', + uriVariables: ['id' => new Link(fromClass: self::class)], + input: BookReviewAction::class, + output: false, + provider: [self::class, 'provide'], + processor: [self::class, 'process'], + extraProperties: ['potential_action' => true], + ), + new Post( + uriTemplate: '/actionable_books/{id}/archive', + uriVariables: ['id' => new Link(fromClass: self::class)], + input: ArchiveBookInput::class, + output: false, + hydraContext: ['@type' => ['hydra:Operation', 'schema:ArchiveAction']], + provider: [self::class, 'provide'], + processor: [self::class, 'process'], + extraProperties: ['potential_action' => true], + ), + new Post( + uriTemplate: '/actionable_books/{id}/restore', + uriVariables: ['id' => new Link(fromClass: self::class)], + input: ArchiveBookInput::class, + output: false, + provider: [self::class, 'provide'], + processor: [self::class, 'process'], + ), + ] +)] +final class ActionableBook +{ + #[ApiProperty(identifier: true)] + public ?int $id = null; + + #[Assert\NotBlank] + public ?string $title = null; + + public static function provide(Operation $operation, array $uriVariables = [], array $context = []): self + { + $book = new self(); + $book->id = (int) $uriVariables['id']; + $book->title = 'Candide'; + + return $book; + } + + /** + * @return list + */ + public static function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): array + { + return [self::provide($operation, ['id' => 1])]; + } + + public static function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed + { + return $data; + } +} diff --git a/tests/Fixtures/TestBundle/ApiResource/PotentialAction/ArchiveBookInput.php b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/ArchiveBookInput.php new file mode 100644 index 00000000000..68063aa9073 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/ArchiveBookInput.php @@ -0,0 +1,22 @@ + + * + * 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\Tests\Fixtures\TestBundle\ApiResource\PotentialAction; + +use Symfony\Component\Validator\Constraints as Assert; + +final class ArchiveBookInput +{ + #[Assert\NotBlank] + public ?string $reason = null; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/PotentialAction/BookLikeAction.php b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/BookLikeAction.php new file mode 100644 index 00000000000..36cf59a29c1 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/BookLikeAction.php @@ -0,0 +1,18 @@ + + * + * 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\Tests\Fixtures\TestBundle\ApiResource\PotentialAction; + +final class BookLikeAction extends BookReviewAction +{ +} diff --git a/tests/Fixtures/TestBundle/ApiResource/PotentialAction/BookReviewAction.php b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/BookReviewAction.php new file mode 100644 index 00000000000..b7bfb946c77 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/BookReviewAction.php @@ -0,0 +1,24 @@ + + * + * 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\Tests\Fixtures\TestBundle\ApiResource\PotentialAction; + +use Symfony\Component\Serializer\Attribute\DiscriminatorMap; + +#[DiscriminatorMap(typeProperty: '@type', mapping: [ + 'LikeAction' => BookLikeAction::class, + 'ShareAction' => BookShareAction::class, +])] +abstract class BookReviewAction +{ +} diff --git a/tests/Fixtures/TestBundle/ApiResource/PotentialAction/BookShareAction.php b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/BookShareAction.php new file mode 100644 index 00000000000..09dcb0d5545 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/PotentialAction/BookShareAction.php @@ -0,0 +1,22 @@ + + * + * 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\Tests\Fixtures\TestBundle\ApiResource\PotentialAction; + +use Symfony\Component\Validator\Constraints as Assert; + +final class BookShareAction extends BookReviewAction +{ + #[Assert\NotBlank] + public ?string $recipient = null; +} diff --git a/tests/Functional/JsonLd/PotentialActionTest.php b/tests/Functional/JsonLd/PotentialActionTest.php new file mode 100644 index 00000000000..9343cef4f19 --- /dev/null +++ b/tests/Functional/JsonLd/PotentialActionTest.php @@ -0,0 +1,81 @@ + + * + * 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\Tests\Functional\JsonLd; + +use ApiPlatform\Test\ApiTestCase; +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\PotentialAction\ActionableBook; +use ApiPlatform\Tests\SetupClassResourcesTrait; + +final class PotentialActionTest extends ApiTestCase +{ + use SetupClassResourcesTrait; + + protected static ?bool $alwaysBootKernel = false; + + /** + * @return class-string[] + */ + public static function getResources(): array + { + return [ActionableBook::class]; + } + + public function testItemAdvertisesItemLevelActions(): void + { + $response = self::createClient()->request('GET', '/actionable_books/1', ['headers' => ['Accept' => 'application/ld+json']]); + $this->assertResponseIsSuccessful(); + + $actions = $response->toArray()['potentialAction']; + $this->assertSame(['LikeAction', 'ShareAction', 'schema:ArchiveAction'], array_column($actions, '@type')); + + $this->assertSame('EntryPoint', $actions[0]['target']['@type']); + $this->assertSame('/actionable_books/1/reviews', $actions[0]['target']['urlTemplate']); + $this->assertSame('POST', $actions[0]['target']['httpMethod']); + $this->assertContains('application/ld+json', (array) $actions[0]['target']['encodingType']); + $this->assertContains('application/ld+json', (array) $actions[0]['target']['contentType']); + $this->assertArrayNotHasKey('recipient-input', $actions[0]); + + $this->assertSame('/actionable_books/1/reviews', $actions[1]['target']['urlTemplate']); + $this->assertSame('required', $actions[1]['recipient-input']); + + $this->assertSame('/actionable_books/1/archive', $actions[2]['target']['urlTemplate']); + $this->assertSame('required', $actions[2]['reason-input']); + } + + public function testCollectionAdvertisesCollectionLevelActions(): void + { + $response = self::createClient()->request('GET', '/actionable_books', ['headers' => ['Accept' => 'application/ld+json']]); + $this->assertResponseIsSuccessful(); + + $json = $response->toArray(); + $this->assertCount(1, $json['potentialAction']); + $this->assertSame('schema:CreateAction', $json['potentialAction'][0]['@type']); + $this->assertSame('/actionable_books', $json['potentialAction'][0]['target']['urlTemplate']); + $this->assertSame('POST', $json['potentialAction'][0]['target']['httpMethod']); + $this->assertSame('required', $json['potentialAction'][0]['title-input']); + + $this->assertArrayNotHasKey('potentialAction', $json['hydra:member'][0]); + } + + public function testContextScopesActionsToSchemaOrg(): void + { + $response = self::createClient()->request('GET', '/contexts/ActionableBook', ['headers' => ['Accept' => 'application/ld+json']]); + $this->assertResponseIsSuccessful(); + + $this->assertSame([ + '@id' => 'https://schema.org/potentialAction', + '@context' => ['@vocab' => 'https://schema.org/', 'schema' => 'https://schema.org/'], + ], $response->toArray()['@context']['potentialAction']); + } +}