Skip to content

Commit

Permalink
feat(laravel): add support for backed enum normalizers
Browse files Browse the repository at this point in the history
Closes: api-platform#6906
Signed-off-by: Tobias Oitzinger <[email protected]>
  • Loading branch information
toitzi committed Jan 13, 2025
1 parent 36cee39 commit a37b3ba
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
Expand Down Expand Up @@ -1016,6 +1017,7 @@ public function register(): void
$list->insert($app->make(DateTimeZoneNormalizer::class), -915);
$list->insert($app->make(DateIntervalNormalizer::class), -915);
$list->insert($app->make(DateTimeNormalizer::class), -910);
$list->insert($app->make(BackedEnumNormalizer::class), -910);
$list->insert($app->make(ObjectNormalizer::class), -1000);
$list->insert($app->make(ItemNormalizer::class), -895);
$list->insert($app->make(OpenApiNormalizer::class), -780);
Expand Down
14 changes: 14 additions & 0 deletions src/Laravel/Tests/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Laravel\Tests;

use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
Expand All @@ -27,6 +28,19 @@ class EloquentTest extends TestCase
use RefreshDatabase;
use WithWorkbench;

public function testBackedEnumsNormalization(): void
{
BookFactory::new([
'status' => BookStatus::DRAFT,
])->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$book = $response->json()['member'][0];

$this->assertArrayHasKey('status', $book);
$this->assertSame('DRAFT', $book['status']);
}

public function testSearchFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
Expand Down
2 changes: 2 additions & 0 deletions src/Laravel/Tests/GraphQlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Laravel\Tests;

use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand Down Expand Up @@ -133,6 +134,7 @@ public function testCreateBook(): void
'name' => fake()->name(),
'author' => 'api/authors/'.$author->id,
'isbn' => fake()->isbn13(),
'status' => BookStatus::PUBLISHED,
'isAvailable' => 1 === random_int(0, 1),
],
],
Expand Down
20 changes: 20 additions & 0 deletions src/Laravel/workbench/app/Enums/BookStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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\Laravel\workbench\app\Enums;

enum BookStatus: string
{
case PUBLISHED = 'PUBLISHED';
case DRAFT = 'DRAFT';
}
6 changes: 4 additions & 2 deletions src/Laravel/workbench/app/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApiPlatform\Laravel\Eloquent\Filter\OrFilter;
use ApiPlatform\Laravel\Eloquent\Filter\PartialSearchFilter;
use ApiPlatform\Laravel\Eloquent\Filter\RangeFilter;
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
Expand Down Expand Up @@ -86,10 +87,11 @@ class Book extends Model
use HasFactory;
use HasUlids;

protected $visible = ['name', 'author', 'isbn', 'publication_date', 'is_available'];
protected $fillable = ['name', 'is_available'];
protected $visible = ['name', 'author', 'isbn', 'status', 'publication_date', 'is_available'];
protected $fillable = ['name', 'status', 'is_available'];
protected $casts = [
'is_available' => 'boolean',
'status' => BookStatus::class,
];

public function author(): BelongsTo
Expand Down
2 changes: 2 additions & 0 deletions src/Laravel/workbench/database/factories/BookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Workbench\Database\Factories;

use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
use Illuminate\Database\Eloquent\Factories\Factory;
use Symfony\Component\Uid\Ulid;
use Workbench\App\Models\Book;
Expand All @@ -38,6 +39,7 @@ public function definition(): array
'isbn' => fake()->isbn13(),
'publication_date' => fake()->optional()->date(),
'is_available' => 1 === random_int(0, 1),
'status' => BookStatus::PUBLISHED,
'internal_note' => fake()->text(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function up(): void
$table->date('publication_date')->nullable();
$table->boolean('is_available')->default(true);
$table->text('internal_note')->nullable();
$table->enum('status', ['PUBLISHED', 'DRAFT'])->default('PUBLISHED');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('authors');
$table->timestamps();
Expand Down

0 comments on commit a37b3ba

Please sign in to comment.