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

[Question]: Implementing generic classes discards the inferred type #2392

Open
mrazek-honza opened this issue Nov 12, 2024 · 6 comments
Open
Labels

Comments

@mrazek-honza
Copy link

Version

4.29.0

Question

Hello. I have a question regarding a use case that I encountered on our project. We have a response structure for paginated responses. The parent class is a generic and looks like this:

/**
 * @template TData
 */
class PaginatedResponse
{
    /**
     * @param list<TData> $items
     */
    public function __construct(
        /** @var list<TData> */
        #[OA\Property(
            description: 'Array of requested items',
        )]
        public array $items,
        #[OA\Property(
            description: 'Number of all available items matching requested parameters',
            example: 69
        )]
        public int $totalSize,
    ) {
    }
}

The child class then implements the defined generic:

/** @extends PaginatedResponse<CompanyResponse> */
final class CompanyListResponse extends PaginatedResponse
{
    public function __construct(
        /** @var list<CompanyResponse> $items */
        #[OA\Property(
            description: 'Array of requested Companies for specified or default page and page size',
        )]
        public array $items,
        int $totalSize,
    ) {
        parent::__construct(
            items: $this->items,
            totalSize: $totalSize,
        );
    }
}

This generates the desired schema, but there is a slight inconvenience in having to specify the @var annotation on every child class. When it is not specified, PHPStan (running on level 8) generates no errors, since the parent already specifies the type using the generic. However, the generated schema cannot infer the type properly and instead of generating

"items": {
   "$ref": "#/components/schemas/CompanyResponse"
}

it generates

"items": {}

My question is: Is there something wrong with our typing, or is this feature not supported by either this bundle, or the symfony property info components? Type hinting in the IDE works without specifying the @var tag in child components, so I suppose that PHPDocs are correct indeed.

Thank you for any answers.

Additional context

No response

@DjordyKoert
Copy link
Collaborator

I think this might be solved by #2349

You can try using this branch and seeing if this would solve your issue 👀

// composer.json
-"nelmio/api-doc-bundle": "*",
+"nelmio/api-doc-bundle": "dev-2212-type-info",

And then opt in to this new behaviour by updating your bundle config nelmio_api_doc.yaml:

nelmio_api_doc:
    experimental_type_info: true

NOTE: This is still a WIP so feedback is appreciated, I would love to hear if this would fix issues like these

@mrazek-honza
Copy link
Author

@DjordyKoert Thank you for so quick response. Sadly our project is running SF6.4 now, so I couldn't test it on the project itself.

I created a small sample application, where I generated the schema in initial commit and then tried to:

  1. Remove the var tag mrazek-honza/nelmio-property-type-test@5636902
  2. Remove the override of parent class property mrazek-honza/nelmio-property-type-test@ca65f2a

Sadly none of these helped, as you can see in these commits, the schema changes the same way.

I wonder if this is supported with property info utilities. I do not have insights into these, but if it is, I would love to help on this feature, since it is a valid use case in my eyes :)

@DjordyKoert
Copy link
Collaborator

DjordyKoert commented Nov 12, 2024

This is most likely a restriction in symfony/property-info (6.4).

You could (temporarily) install only symfony/property-info on ^7.1 together with #2392 (comment) and see if this 'fixes' it.

@DjordyKoert
Copy link
Collaborator

I just found this open issue aswell for symfony/property-info to support generics symfony/symfony#45071

@mrazek-honza
Copy link
Author

I see, your searching skills are better 😄 I looked quickly through the issues on property-info but didn't find it.

So if the issue gets implemented in the property-info component, it could be possible to reflect it in this bundle so that the type gets correctly inferred by generator?

@DjordyKoert
Copy link
Collaborator

Correct, but I mostly think this will be / is solved with the introduction of the TypeInfo component

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants