Skip to content
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

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

Open
CxDevLead opened this issue Nov 4, 2022 · 6 comments
Open
Labels
enhancement type handling Type handling / mapping

Comments

@CxDevLead
Copy link

CxDevLead commented Nov 4, 2022

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?

@DerManoMann
Copy link
Collaborator

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.

@shahrukh4
Copy link

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;

@DerManoMann
Copy link
Collaborator

public array SchemaLineChartsResource $market;

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

@shahrukh4
Copy link

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

@DerManoMann
Copy link
Collaborator

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;

@Aerendir
Copy link

Aerendir commented Mar 4, 2024

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement type handling Type handling / mapping
Projects
None yet
Development

No branches or pull requests

4 participants