如何在NelmioApiDocBundle的Article模型中定义details对象属性
details Property in Your Article Model Hey there! Let's get your details property set up correctly so it matches your desired response structure and works properly with NelmioApiDocBundle. I'll walk you through two practical approaches, plus fix a small type mismatch in your existing code.
First: Fix the getId() Return Type Mismatch
I spotted a quick inconsistency in your current code: your $id property is typed as integer|null, but the getId() method returns ?string. Let's correct that first to keep your types consistent:
/** * @return integer|null */ public function getId(): ?int { // Changed from ?string to ?int return $this->id; }
Approach 1: Use a DTO Class (Recommended for Type Safety)
The cleanest way to define nested object structures is to create a dedicated DTO (Data Transfer Object) for your details field. This gives you type safety and lets NelmioApiDoc automatically pick up the full structure for your documentation.
Step 1: Create the Details DTO
// src/DTO/Details.php namespace App\DTO; use Symfony\Component\Serializer\Annotation\Groups; class Details { /** * @var string|null * @Groups({"APIResponse"}) */ private $status; /** * @var int|null * @Groups({"APIResponse"}) */ private $time; // Add getters (and setters if you need to populate this data) public function getStatus(): ?string { return $this->status; } public function getTime(): ?int { return $this->time; } }
Step 2: Update the Article Model
Now reference this DTO in your Article class for the details property:
use App\DTO\Details; // Don't forget this import class Article { // ... existing properties ... /** * @var Details|null * @Groups({"APIResponse"}) */ private $details; // ... existing getters ... /** * @return Details|null */ public function getDetails(): ?Details { return $this->details; } }
NelmioApiDoc will automatically recognize the nested structure from the Details class, so your API documentation will show details as an object with status (string) and time (integer) fields.
Approach 2: Define the Structure with Annotations (No Extra Class)
If you don't want to create a separate DTO, you can use OpenAPI annotations to manually define the nested structure directly on the details property:
First, add the necessary use statement for the OpenAPI annotations:
use OpenApi\Annotations as OA;
Then update the details property in your Article class:
/** * @var array|null * @Groups({"APIResponse"}) * @OA\Property( * type="object", * @OA\Property(property="status", type="string"), * @OA\Property(property="time", type="integer") * ) */ private $details; // Update the getter to match the array type /** * @return array|null */ public function getDetails(): ?array { return $this->details; }
This tells NelmioApiDoc to render details as an object with the specified fields, even though the PHP type is array.
Either approach will get you your desired response structure:
{"id": 123, "title": "My Sample Article", "details": { "status": "published", "time" : 1699999200 }}
内容的提问来源于stack exchange,提问作者Klick

