Skip to content

Commit f482260

Browse files
committed
Merge 4.1
2 parents f15681f + 71d3664 commit f482260

File tree

10 files changed

+53
-8
lines changed

10 files changed

+53
-8
lines changed

ApiPlatformProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
223223
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
224224
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
225+
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
225226
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
226227
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
227228
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
@@ -776,6 +777,7 @@ public function register(): void
776777
contactEmail: $config->get('api-platform.swagger_ui.contact.email', ''),
777778
licenseName: $config->get('api-platform.swagger_ui.license.name', ''),
778779
licenseUrl: $config->get('api-platform.swagger_ui.license.url', ''),
780+
persistAuthorization: $config->get('api-platform.swagger_ui.persist_authorization', false),
779781
httpAuth: $config->get('api-platform.swagger_ui.http_auth', []),
780782
);
781783
});
@@ -1020,6 +1022,7 @@ public function register(): void
10201022
$list->insert($app->make(DateTimeZoneNormalizer::class), -915);
10211023
$list->insert($app->make(DateIntervalNormalizer::class), -915);
10221024
$list->insert($app->make(DateTimeNormalizer::class), -910);
1025+
$list->insert($app->make(BackedEnumNormalizer::class), -910);
10231026
$list->insert($app->make(ObjectNormalizer::class), -1000);
10241027
$list->insert($app->make(ItemNormalizer::class), -895);
10251028
$list->insert($app->make(OpenApiNormalizer::class), -780);

Controller/ApiPlatformController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public function __invoke(Request $request): Response
6969
}
7070

7171
if (null === $operation->canRead()) {
72-
$operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe()); // @phpstan-ignore-line
72+
$operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
7373
}
7474

7575
if (null === $operation->canDeserialize()) {
76-
$operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true)); // @phpstan-ignore-line
76+
$operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true));
7777
}
7878

7979
$body = $this->provider->provide($operation, $uriVariables, $context);

Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ public function __construct(
4646
*/
4747
public function create(string $resourceClass, string $property, array $options = []): ApiProperty
4848
{
49+
if (!is_a($resourceClass, Model::class, true)) {
50+
return $this->decorated?->create($resourceClass, $property, $options) ?? new ApiProperty();
51+
}
52+
4953
try {
5054
$refl = new \ReflectionClass($resourceClass);
5155
$model = $refl->newInstanceWithoutConstructor();
5256
} catch (\ReflectionException) {
5357
return $this->decorated?->create($resourceClass, $property, $options) ?? new ApiProperty();
5458
}
5559

56-
if (!$model instanceof Model) {
57-
return $this->decorated?->create($resourceClass, $property, $options) ?? new ApiProperty();
58-
}
59-
6060
try {
6161
$propertyMetadata = $this->decorated?->create($resourceClass, $property, $options) ?? new ApiProperty();
6262
} catch (PropertyNotFoundException) {

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();

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
],

public/init-swagger-ui.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ window.onload = function() {
4545
spec: data.spec,
4646
dom_id: '#swagger-ui',
4747
validatorUrl: null,
48+
persistAuthorization: data.persistAuthorization,
4849
oauth2RedirectUrl: data.oauth.redirectUrl,
4950
presets: [
5051
SwaggerUIBundle.presets.apis,

workbench/app/Enums/BookStatus.php

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+
}

workbench/app/Models/Book.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use ApiPlatform\Laravel\Eloquent\Filter\OrFilter;
2323
use ApiPlatform\Laravel\Eloquent\Filter\PartialSearchFilter;
2424
use ApiPlatform\Laravel\Eloquent\Filter\RangeFilter;
25+
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
2526
use ApiPlatform\Metadata\ApiResource;
2627
use ApiPlatform\Metadata\Delete;
2728
use ApiPlatform\Metadata\Get;
@@ -88,10 +89,11 @@ class Book extends Model
8889
use HasFactory;
8990
use HasUlids;
9091

91-
protected $visible = ['name', 'author', 'isbn', 'publication_date', 'is_available', 'published'];
92-
protected $fillable = ['name', 'publication_date', 'isbn', 'is_available', 'published'];
92+
protected $visible = ['name', 'author', 'isbn', 'status', 'publication_date', 'is_available', 'published'];
93+
protected $fillable = ['name', 'status', 'publication_date', 'isbn', 'is_available', 'published'];
9394
protected $casts = [
9495
'is_available' => 'boolean',
96+
'status' => BookStatus::class,
9597
];
9698

9799
public function author(): BelongsTo

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
'published' => fake()->boolean(100),
4345
];

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
@@ -35,6 +35,7 @@ public function up(): void
3535
$table->boolean('is_available')->default(true);
3636
$table->text('internal_note')->nullable();
3737
$table->boolean('published')->nullable();
38+
$table->enum('status', ['PUBLISHED', 'DRAFT'])->default('PUBLISHED');
3839
$table->integer('author_id')->unsigned();
3940
$table->foreign('author_id')->references('id')->on('authors');
4041
$table->timestamps();

0 commit comments

Comments
 (0)