Skip to content

fix(laravel): read property type before serialization #7332

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

Open
wants to merge 1 commit into
base: 4.1
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ public function register(): void
return new CachePropertyMetadataFactory(
new SchemaPropertyMetadataFactory(
$app->make(ResourceClassResolverInterface::class),
new PropertyInfoPropertyMetadataFactory(
$app->make(PropertyInfoExtractorInterface::class),
new SerializerPropertyMetadataFactory(
$app->make(SerializerClassMetadataFactory::class),
new SerializerPropertyMetadataFactory(
$app->make(SerializerClassMetadataFactory::class),
new PropertyInfoPropertyMetadataFactory(
$app->make(PropertyInfoExtractorInterface::class),
new AttributePropertyMetadataFactory(
new EloquentAttributePropertyMetadataFactory(
new EloquentPropertyMetadataFactory(
Expand Down
9 changes: 9 additions & 0 deletions src/Laravel/Tests/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,13 @@ public function testResourceWithOptionModel(): void
'@type' => 'Collection',
]);
}

public function testCustomRelation(): void
{
$response = $this->get('/api/home', headers: ['accept' => ['application/ld+json']]);
$home = $response->json();
$this->assertArrayHasKey('order', $home);
$this->assertArrayHasKey('id', $home['order']);
$this->assertArrayHasKey('number', $home['order']);
}
}
49 changes: 49 additions & 0 deletions src/Laravel/workbench/app/ApiResource/Home.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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 Workbench\App\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
use Symfony\Component\Serializer\Annotation\Groups;
use Workbench\App\Models\Order;
use Workbench\Database\Factories\OrderFactory;

#[ApiResource(
operations: [
new Get(
uriTemplate: '/home',
normalizationContext: ['groups' => ['home:read']],
provider: [self::class, 'provide'],
),
],
)]
class Home
{
#[ApiProperty(identifier: true)]
public int $id = 1;

#[Groups(['home:read'])]
public ?Order $order = null;

public static function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$order = OrderFactory::new()->create();
$home = new self();
$home->order = $order;

return $home;
}
}
32 changes: 32 additions & 0 deletions src/Laravel/workbench/app/Models/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 Workbench\App\Models;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource()]
#[ApiProperty(property: 'id', serialize: [new Groups(['home:read'])])]
#[ApiProperty(property: 'number', serialize: [new Groups(['home:read'])])]
class Order extends Model
{
use HasFactory;

protected $fillable = [
'number',
];
}
29 changes: 29 additions & 0 deletions src/Laravel/workbench/database/factories/OrderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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 Workbench\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Workbench\App\Models\Order;

class OrderFactory extends Factory
{
protected $model = Order::class;

public function definition()
{
return [
'number' => $this->faker->randomNumber(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('orders', function (Blueprint $table): void {
$table->id();
$table->integer('number');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('orders');
}
};
Loading