Skip to content

[Bug]: fields specified in with method of Resources are not included in the generated spec #955

@elnurvl

Description

@elnurvl

What happened?

Some resources may have defined a with method to include additional fields in responses:
https://laravel.com/docs/12.x/eloquent-resources#top-level-meta-data

The generated spec misses those fields. Is this intended?

How to reproduce the bug

<?php

namespace App\Http\Resources\V1;

use App\Models\Task;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @mixin Task
 */
class TaskResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'user' => new UserResource($this->user),
            'title' => $this->title,
            'status' => $this->status,
            'priority' => $this->priority,
            /** @format date */
            'due_date' => $this->due_date?->format('Y-m-d'),
            'assignee' => new UserResource($this->assignee),
            'tags' => TagResource::collection($this->tags),
        ];
    }

    /**
     * @return array<string, mixed>
     */
    public function with($request): array
    {
        return [
            'data' => [
                'description' => $this->description,
                'created_at' => $this->created_at?->toRfc3339String(),
                'updated_at' => $this->updated_at?->toRfc3339String(),
                'metadata' => $this->metadata,
            ],
        ];
    }
}

Package Version

0.12.34

PHP Version

8.4.12

Laravel Version

12.28.1

Which operating systems does with happen with?

macOS

Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions