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

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
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
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,41 @@
<?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 = [])
{
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make node name case insensitive. Could be removed for more strictness.


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

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

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

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

return Greeting::class === $type &&
(array_key_exists('salutation', $data) || array_key_exists('recipient', $data));
}
}
Loading