From 0e3b8a701f525e56cddb8b5babd9726bb080e79a Mon Sep 17 00:00:00 2001 From: mattamon Date: Wed, 22 May 2024 09:13:45 +0200 Subject: [PATCH] Add responses and new way for default responses --- .../Response/Property/NoteCollection.php | 37 +++++++++ .../Element/CollectionController.php | 24 +++--- .../Controller/Element/CreateController.php | 15 ++-- src/Note/Repository/NoteRepository.php | 7 +- src/Note/Schema/Note.php | 33 ++++++++ .../Attributes/Response/DefaultResponses.php | 75 +++++++++++++++++++ src/Util/Constants/HttpResponseCodes.php | 33 ++++++++ 7 files changed, 201 insertions(+), 23 deletions(-) create mode 100644 src/Note/Attributes/Response/Property/NoteCollection.php create mode 100644 src/OpenApi/Attributes/Response/DefaultResponses.php create mode 100644 src/Util/Constants/HttpResponseCodes.php diff --git a/src/Note/Attributes/Response/Property/NoteCollection.php b/src/Note/Attributes/Response/Property/NoteCollection.php new file mode 100644 index 000000000..51fe0afe4 --- /dev/null +++ b/src/Note/Attributes/Response/Property/NoteCollection.php @@ -0,0 +1,37 @@ +noteResolver->getById($id); } public function listNotes(NoteElement $noteElement, NoteParameters $parameters): NoteListing diff --git a/src/Note/Schema/Note.php b/src/Note/Schema/Note.php index f012d452c..78a15b67f 100644 --- a/src/Note/Schema/Note.php +++ b/src/Note/Schema/Note.php @@ -16,28 +16,61 @@ namespace Pimcore\Bundle\StudioBackendBundle\Note\Schema; +use OpenApi\Attributes\AdditionalProperties; +use OpenApi\Attributes\Items; +use OpenApi\Attributes\Property; +use OpenApi\Attributes\Schema; use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface; use Pimcore\Bundle\StudioBackendBundle\Util\Traits\AdditionalAttributesTrait; /** * @internal */ +#[Schema( + title: 'Note', + type: 'object' +)] final class Note implements AdditionalAttributesInterface { use AdditionalAttributesTrait; public function __construct( + #[Property(description: 'id', type: 'integer', example: 666)] private readonly int $id, + #[Property(description: 'type', type: 'string', example: 'Type of note')] private readonly string $type, + #[Property(description: 'Id of element', type: 'integer', example: 667)] private readonly int $cId, + #[Property(description: 'Type of element', type: 'string', example: 'asset')] private readonly string $cType, + #[Property(description: 'Path of element', type: 'string', example: '/path/to/element')] private readonly string $cPath, + #[Property(description: 'Creation date of note', type: 'integer', example: 1634025600)] private readonly int $date, + #[Property(description: 'title', type: 'string', example: 'Title of note')] private readonly string $title, + #[Property(description: 'description', type: 'string', example: 'This is a description')] private readonly string $description, + #[Property(description: 'Locked', type: 'boolean', example: false)] private readonly bool $locked, + #[Property( + description: 'Data of note', + type: 'object', + example: 'Can be pretty much anything', + additionalProperties: new AdditionalProperties( + oneOf: [ + new Schema(type: 'string'), + new Schema(type: 'number'), + new Schema(type: 'boolean'), + new Schema(type: 'object'), + new Schema(type: 'array', items: new Items()), + ] + ) + )] private readonly array $data, + #[Property(description: 'User ID', type: 'integer', example: 1)] private readonly ?int $userId, + #[Property(description: 'Username', type: 'string', example: 'shaquille.oatmeal')] private readonly ?string $userName ) { diff --git a/src/OpenApi/Attributes/Response/DefaultResponses.php b/src/OpenApi/Attributes/Response/DefaultResponses.php new file mode 100644 index 000000000..579fecee8 --- /dev/null +++ b/src/OpenApi/Attributes/Response/DefaultResponses.php @@ -0,0 +1,75 @@ +generateDescription($codes); + + parent::__construct( + response: 'default', + description: $description, + content: new JsonContent( + oneOf: array_map(static function ($class) { + return new Schema(ref: $class); + }, Schemas::ERRORS), + ) + ); + } + + private function generateDescription(array $errorCodes): string + { + // merge the default error codes with the provided ones + $errorCodes = array_merge($this->defaultErrorCodes, $errorCodes); + + // Sort the array of enums by http status code + usort($errorCodes, static function($a, $b) { + return $a->value <=> $b->value; + }); + + // Generate description block of http codes + $errorCodes = array_map(function ($code) { + return sprintf('%s - %s', $code->value, $this->generateNiceName($code->name)); + }, $errorCodes); + + return implode('
', $errorCodes); + } + + private function generateNiceName(string $name): string { + return ucwords(str_replace('_', ' ', strtolower($name))); + } +} diff --git a/src/Util/Constants/HttpResponseCodes.php b/src/Util/Constants/HttpResponseCodes.php new file mode 100644 index 000000000..04fb1c7da --- /dev/null +++ b/src/Util/Constants/HttpResponseCodes.php @@ -0,0 +1,33 @@ +