Skip to content

Commit 421d97e

Browse files
authored
feat(laravel): add support for backed enum normalizers (#6911)
Closes: #6906
1 parent 36cee39 commit 421d97e

File tree

7 files changed

+45
-2
lines changed

7 files changed

+45
-2
lines changed

src/Laravel/ApiPlatformProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
222222
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
223223
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
224+
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
224225
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
225226
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
226227
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
@@ -1016,6 +1017,7 @@ public function register(): void
10161017
$list->insert($app->make(DateTimeZoneNormalizer::class), -915);
10171018
$list->insert($app->make(DateIntervalNormalizer::class), -915);
10181019
$list->insert($app->make(DateTimeNormalizer::class), -910);
1020+
$list->insert($app->make(BackedEnumNormalizer::class), -910);
10191021
$list->insert($app->make(ObjectNormalizer::class), -1000);
10201022
$list->insert($app->make(ItemNormalizer::class), -895);
10211023
$list->insert($app->make(OpenApiNormalizer::class), -780);

src/Laravel/Tests/EloquentTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Laravel\Tests;
1515

1616
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
17+
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
1718
use Illuminate\Foundation\Testing\RefreshDatabase;
1819
use Orchestra\Testbench\Concerns\WithWorkbench;
1920
use Orchestra\Testbench\TestCase;
@@ -27,6 +28,19 @@ class EloquentTest extends TestCase
2728
use RefreshDatabase;
2829
use WithWorkbench;
2930

31+
public function testBackedEnumsNormalization(): void
32+
{
33+
BookFactory::new([
34+
'status' => BookStatus::DRAFT,
35+
])->has(AuthorFactory::new())->count(10)->create();
36+
37+
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
38+
$book = $response->json()['member'][0];
39+
40+
$this->assertArrayHasKey('status', $book);
41+
$this->assertSame('DRAFT', $book['status']);
42+
}
43+
3044
public function testSearchFilter(): void
3145
{
3246
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

src/Laravel/Tests/GraphQlTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Laravel\Tests;
1515

1616
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
17+
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
1718
use Illuminate\Config\Repository;
1819
use Illuminate\Foundation\Application;
1920
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -133,6 +134,7 @@ public function testCreateBook(): void
133134
'name' => fake()->name(),
134135
'author' => 'api/authors/'.$author->id,
135136
'isbn' => fake()->isbn13(),
137+
'status' => BookStatus::PUBLISHED,
136138
'isAvailable' => 1 === random_int(0, 1),
137139
],
138140
],
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Laravel\workbench\app\Enums;
15+
16+
enum BookStatus: string
17+
{
18+
case PUBLISHED = 'PUBLISHED';
19+
case DRAFT = 'DRAFT';
20+
}

src/Laravel/workbench/app/Models/Book.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Laravel\Eloquent\Filter\OrFilter;
2222
use ApiPlatform\Laravel\Eloquent\Filter\PartialSearchFilter;
2323
use ApiPlatform\Laravel\Eloquent\Filter\RangeFilter;
24+
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
2425
use ApiPlatform\Metadata\ApiResource;
2526
use ApiPlatform\Metadata\Delete;
2627
use ApiPlatform\Metadata\Get;
@@ -86,10 +87,11 @@ class Book extends Model
8687
use HasFactory;
8788
use HasUlids;
8889

89-
protected $visible = ['name', 'author', 'isbn', 'publication_date', 'is_available'];
90-
protected $fillable = ['name', 'is_available'];
90+
protected $visible = ['name', 'author', 'isbn', 'status', 'publication_date', 'is_available'];
91+
protected $fillable = ['name', 'status', 'is_available'];
9192
protected $casts = [
9293
'is_available' => 'boolean',
94+
'status' => BookStatus::class,
9395
];
9496

9597
public function author(): BelongsTo

src/Laravel/workbench/database/factories/BookFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Workbench\Database\Factories;
1515

16+
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
1617
use Illuminate\Database\Eloquent\Factories\Factory;
1718
use Symfony\Component\Uid\Ulid;
1819
use Workbench\App\Models\Book;
@@ -38,6 +39,7 @@ public function definition(): array
3839
'isbn' => fake()->isbn13(),
3940
'publication_date' => fake()->optional()->date(),
4041
'is_available' => 1 === random_int(0, 1),
42+
'status' => BookStatus::PUBLISHED,
4143
'internal_note' => fake()->text(),
4244
];
4345
}

src/Laravel/workbench/database/migrations/2023_07_15_231244_create_book_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function up(): void
3434
$table->date('publication_date')->nullable();
3535
$table->boolean('is_available')->default(true);
3636
$table->text('internal_note')->nullable();
37+
$table->enum('status', ['PUBLISHED', 'DRAFT'])->default('PUBLISHED');
3738
$table->integer('author_id')->unsigned();
3839
$table->foreign('author_id')->references('id')->on('authors');
3940
$table->timestamps();

0 commit comments

Comments
 (0)