Skip to content

IBX-8190: Update REST new resource #2682

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 30 commits into
base: 5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4a8a47a
routes_rest.yaml: Fix greet controller
adriendupuis Mar 25, 2025
d6a064f
Start rewrite custom rest example
adriendupuis Mar 26, 2025
3b616c4
Start rewrite custom rest example
adriendupuis Mar 26, 2025
296ca50
clean-up
adriendupuis Mar 27, 2025
2ce1141
PHP & JS CS Fixes
adriendupuis Mar 27, 2025
685ad5d
Enhance output
adriendupuis Mar 28, 2025
4d8252e
PHP & JS CS Fixes
adriendupuis Mar 28, 2025
0b04132
Enhance output
adriendupuis Mar 31, 2025
6300ece
Denormalize input
adriendupuis Mar 31, 2025
90e482d
PHP & JS CS Fixes
adriendupuis Mar 31, 2025
1e76897
Add REST controller to doc
adriendupuis Mar 31, 2025
72c0614
PHP & JS CS Fixes
adriendupuis Mar 31, 2025
8d101f7
Add REST controller to doc
adriendupuis Mar 31, 2025
749ee2e
PHP & JS CS Fixes
adriendupuis Mar 31, 2025
8746a41
Start to update creating_new_rest_resource.md
adriendupuis Mar 31, 2025
b3f0c11
Restore RestLocation.php
adriendupuis Apr 2, 2025
97d32ae
Restore ValueObjectVisitorDispatcher.php
adriendupuis Apr 2, 2025
14a1c78
phpstan-baseline.neon: rm ValueObjectVisitor\Greeting err
adriendupuis Apr 2, 2025
cd9c883
DefaultController: Hard code root node name
adriendupuis Apr 2, 2025
530d80a
GreetingInputDenormalizer.php
adriendupuis Apr 2, 2025
379a4f1
GreetingNormalizer: Fix type hint & PHPDoc
adriendupuis Apr 2, 2025
5a80a79
PHP & JS CS Fixes
adriendupuis Apr 2, 2025
fbcd437
Merge branch '5.0' into update-rest
adriendupuis Jul 22, 2025
bbc851f
Fix GreetingInputDenormalizer::denormalize
adriendupuis Jul 22, 2025
a64de31
Fix GreetingInputDenormalizer::supportsDenormalization
adriendupuis Jul 22, 2025
c75a1ab
Fix GreetingInputDenormalizer::getSupportedTypes
adriendupuis Jul 22, 2025
0e82f5f
Fix GreetingNormalizer::normalize
adriendupuis Jul 22, 2025
e826328
Fix GreetingNormalizer::supportsNormalization
adriendupuis Jul 22, 2025
4ef6d43
Fix GreetingNormalizer::getSupportedTypes
adriendupuis Jul 22, 2025
3da7b10
creating_new_rest_resource.md: Complete TODOs
adriendupuis Jul 22, 2025
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
2 changes: 1 addition & 1 deletion code_samples/api/rest_api/config/routes_rest.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
app.rest.greeting:
path: '/greet'
controller: App\Rest\Controller\DefaultController::helloWorld
controller: App\Rest\Controller\DefaultController::greet
methods: [GET,POST]
defaults:
csrf_protection: false
13 changes: 4 additions & 9 deletions code_samples/api/rest_api/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ services:
parent: Ibexa\Rest\Server\Controller
autowire: true
autoconfigure: true
tags: [ 'controller.service_arguments' ]
tags: [ 'controller.service_arguments', 'ibexa.api_platform.resource' ]

App\Rest\ValueObjectVisitor\Greeting:
parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor
tags:
- { name: ibexa.rest.output.value_object.visitor, type: App\Rest\Values\Greeting }

App\Rest\InputParser\GreetingInput:
parent: Ibexa\Rest\Server\Common\Parser
tags:
- { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.GreetingInput }
App\Rest\Serializer\:
resource: '../src/Rest/Serializer/'
tags: ['ibexa.rest.serializer.normalizer']
279 changes: 269 additions & 10 deletions code_samples/api/rest_api/src/Rest/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,283 @@

namespace App\Rest\Controller;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use ApiPlatform\OpenApi\Factory\OpenApiFactory;
use ApiPlatform\OpenApi\Model;
use App\Rest\Values\Greeting;
use Ibexa\Rest\Message;
use Ibexa\Rest\Server\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\SerializerInterface;

#[Get(
uriTemplate: '/greet',
extraProperties: [OpenApiFactory::OVERRIDE_OPENAPI_RESPONSES => false],
openapi: new Model\Operation(
summary: 'Greet',
description: 'Greets a recipient with a salutation',
tags: [
'App',
],
parameters: [
new Model\Parameter(
name: 'Accept',
in: 'header',
required: false,
description: 'If set, the greeting is returned in XML or JSON format.',
schema: [
'type' => 'string',
],
example: 'application/vnd.ibexa.api.Greeting+json',
),
],
responses: [
Response::HTTP_OK => [
'description' => 'OK - Return a greeting',
'content' => [
'application/vnd.ibexa.api.Greeting+xml' => [
'schema' => [
'xml' => [
'name' => 'Greeting',
'wrapped' => false,
],
'properties' => [
'salutation' => [
'type' => 'string',
],
'recipient' => [
'type' => 'string',
],
'sentence' => [
'type' => 'string',
'description' => 'Composed sentence using salutation and recipient.',
],
],
],
'example' => [
'salutation' => 'Hello',
'recipient' => 'World',
'sentence' => 'Hello World',
],
],
'application/vnd.ibexa.api.Greeting+json' => [
'schema' => [
'type' => 'object',
'properties' => [
'Greeting' => [
'type' => 'object',
'properties' => [
'salutation' => [
'type' => 'string',
],
'recipient' => [
'type' => 'string',
],
'sentence' => [
'type' => 'string',
'description' => 'Composed sentence using salutation and recipient.',
],
],
],
],
],
'example' => [
'Greeting' => [
'salutation' => 'Hello',
'recipient' => 'World',
'sentence' => 'Hello World',
],
],
],
],
],
],
),
)]
#[Post(
uriTemplate: '/greet',
extraProperties: [OpenApiFactory::OVERRIDE_OPENAPI_RESPONSES => false],
openapi: new Model\Operation(
summary: 'Greet',
description: 'Greets a recipient with a salutation',
tags: [
'App',
],
parameters: [
new Model\Parameter(
name: 'Content-Type',
in: 'header',
required: false,
description: 'The greeting input schema encoded in XML or JSON.',
schema: [
'type' => 'string',
],
example: 'application/vnd.ibexa.api.GreetingInput+json',
),
new Model\Parameter(
name: 'Accept',
in: 'header',
required: false,
description: 'If set, the greeting is returned in XML or JSON format.',
schema: [
'type' => 'string',
],
example: 'application/vnd.ibexa.api.Greeting+json',
),
],
requestBody: new Model\RequestBody(
required: false,
content: new \ArrayObject([
'application/vnd.ibexa.api.GreetingInput+xml' => [
'schema' => [
'type' => 'object',
'xml' => [
'name' => 'GreetingInput',
'wrapped' => false,
],
'properties' => [
'salutation' => [
'type' => 'string',
'required' => false,
],
'recipient' => [
'type' => 'string',
'required' => false,
],
],
],
'example' => [
'salutation' => 'Good morning',
],
],
'application/vnd.ibexa.api.GreetingInput+json' => [
'schema' => [
'type' => 'object',
'properties' => [
'GreetingInput' => [
'type' => 'object',
'properties' => [
'salutation' => [
'type' => 'string',
'required' => false,
],
'recipient' => [
'type' => 'string',
'required' => false,
],
],
],
],
],
'example' => [
'GreetingInput' => [
'salutation' => 'Good day',
'recipient' => 'Earth',
],
],
],
]),
),
responses: [
Response::HTTP_OK => [
'description' => 'OK - Return a greeting',
'content' => [
'application/vnd.ibexa.api.Greeting+xml' => [
'schema' => [
'xml' => [
'name' => 'Greeting',
'wrapped' => false,
],
'properties' => [
'salutation' => [
'type' => 'string',
],
'recipient' => [
'type' => 'string',
],
'sentence' => [
'type' => 'string',
'description' => 'Composed sentence using salutation and recipient.',
],
],
],
'example' => [
'salutation' => 'Good morning',
'recipient' => 'World',
'sentence' => 'Good Morning World',
],
],
'application/vnd.ibexa.api.Greeting+json' => [
'schema' => [
'type' => 'object',
'properties' => [
'Greeting' => [
'type' => 'object',
'properties' => [
'salutation' => [
'type' => 'string',
],
'recipient' => [
'type' => 'string',
],
'sentence' => [
'type' => 'string',
'description' => 'Composed sentence using salutation and recipient.',
],
],
],
],
],
'example' => [
'Greeting' => [
'salutation' => 'Good day',
'recipient' => 'Earth',
'sentence' => 'Good day Earth',
],
],
],
],
],
],
),
)]
class DefaultController extends Controller
{
public function greet(Request $request): Greeting
public const DEFAULT_FORMAT = 'xml';

public const AVAILABLE_FORMATS = ['json', 'xml'];

public function __construct(private SerializerInterface $serializer)
{
}

public function greet(Request $request): Response|Greeting
{
if ('POST' === $request->getMethod()) {
return $this->inputDispatcher->parse(
new Message(
['Content-Type' => $request->headers->get('Content-Type')],
$request->getContent()
)
);
$contentType = $request->headers->get('Content-Type');
if ($contentType) {
preg_match('@.*[/+](?P<format>[^/+]+)@', $contentType, $matches);
$format = empty($matches['format']) ? self::DEFAULT_FORMAT : $matches['format'];
$input = $request->getContent();
$greeting = $this->serializer->deserialize($input, Greeting::class, $format);
} else {
$greeting = new Greeting();
}

return new Greeting();
//return $greeting;

$accept = $request->headers->get('Accept', 'application/' . self::DEFAULT_FORMAT);
preg_match('@.*[/+](?P<format>[^/+]+)@', $accept, $matches);
$format = empty($matches['format']) ? self::DEFAULT_FORMAT : $matches['format'];
if (!in_array($format, self::AVAILABLE_FORMATS)) {
$format = self::DEFAULT_FORMAT;
}

$serialized = $this->serializer->serialize($greeting, $format, [
XmlEncoder::ROOT_NODE_NAME => 'Greeting',
]);

return new Response($serialized, Response::HTTP_OK, ['Content-Type' => "application/vnd.ibexa.api.Greeting+$format"]);
}
}
20 changes: 0 additions & 20 deletions code_samples/api/rest_api/src/Rest/InputParser/GreetingInput.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace App\Rest\Serializer;

use App\Rest\Values\Greeting;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

class GreetingInputDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface
{
use DenormalizerAwareTrait;

public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
{
if ('json' === $format) {
$data = $data[array_key_first($data)];
}
Comment on lines +16 to +18
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwrap JSON.
I could even test that 'GreetingInput' === array_key_first($data)

$data = array_change_key_case($data);

$salutation = $data['salutation'] ?? 'Hello';
$recipient = $data['recipient'] ?? 'World';

return new Greeting($salutation, $recipient);
}

public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
{
if (!is_array($data)) {
return false;
}

if ('json' === $format) {
$data = $data[array_key_first($data)];
}
$data = array_change_key_case($data);

return in_array($type, $this->getSupportedTypes($format), true) &&
(array_key_exists('salutation', $data) || array_key_exists('recipient', $data));
}

public function getSupportedTypes(?string $format): array
{
return [
Greeting::class => true,
];
}
}
Loading
Loading