swagger-php icon indicating copy to clipboard operation
swagger-php copied to clipboard

The 'items' field for Array property's isn't populated by the DocBlock

Open CxDevLead opened this issue 3 years ago • 6 comments

The following constructor:

     /**
     * @param DateTimeImmutable[] $times
     * @param string[] $alerts
     */
    public function __construct(
        #[OA\Property]
        public readonly array $times,
        #[OA\Property]
        public readonly array $alerts,
    )

Will throw the error: @OA\Items() is required when @OA\Property() has type "array"

The fix is to manually populate the info, this works:

     /**
     * @param DateTimeImmutable[] $times
     * @param string[] $alerts
     */
    public function __construct(
        #[OA\Property(items: new OA\Items(ref: "#/components/schemas/DateTimeImmutable", type: "object"))]
        public readonly array $times,
        #[OA\Property(items: new OA\Items(type: "string"))]
        public readonly array $alerts,
    )

Is this a bug? Should the generator automatically populate the items info from the DocBlock generics?

CxDevLead avatar Nov 04 '22 16:11 CxDevLead

Not sure if it's a bug, but it could be better, agreed. I'll make sure to add their to the ongoing type detection revamp.

DerManoMann avatar Nov 05 '22 00:11 DerManoMann

Yeah I also think that will be more cleaner way to do it, if we can do like below,

#[Property()]
public array SchemaLineChartsResource $market;

Currently below is the working example

#[Property(items: new Items(ref: SchemaLineChartsResource::class))]
public array $market;

shahrukh4 avatar Jan 23 '24 07:01 shahrukh4

public array SchemaLineChartsResource $market;

That doesn't look like a valid PHP typehint to me, though?

DerManoMann avatar Jan 24 '24 02:01 DerManoMann

Yeah, you are right, but was looking for more cleaner way, wondering if we can do it.

shahrukh4 avatar Jan 24 '24 11:01 shahrukh4

Could be done, but the type system overhaul is going very slow (sorry). The only way to add those typehints would be phpdoc, I suppose:

    /** @var array<SchemaLineChartsResource> $market */
    public array $market;

DerManoMann avatar Jan 24 '24 21:01 DerManoMann

Just to keep things linked, I have a really similar issue generating documentation for api-platform/core:3.2.*:

  • https://github.com/api-platform/core/issues/6194

As this issue didn't appear when searching the error on Google, I'm going to add the full detailed error here:

User Warning: @OA\Items() parent type must be "array" in

Aerendir avatar Mar 04 '24 16:03 Aerendir