Closed rickgoemans closed 1 year ago
For additional informatie, it does not matter wether the parent class is abstract
or a normal class and wether the child models have properties (or other traits) or not.
This is an issue with swagger-php.
Its an edge case since neither Model
nor HasId
have a /** @OA\Schema() */
annotation. That means swagger-pph will try to merge annotations rather than inheritance, where you end up with something like
allOf:
-
$ref: '#/components/schemas/Model'
``
Looks like the merge code does not lookup properties recursively through used traits in the (not schema) parent class.
Looks like there is also a bug involved because even if you annotate both `HasId` and `Model` with `/** @OA\Schema() */` the `Model` class is missing a `allOf` ref to `HasId`.
This is an issue with swagger-php. Its an edge case since neither
Model
norHasId
have a/** @OA\Schema() */
annotation. That means swagger-pph will try to merge annotations rather than inheritance, where you end up with something likeallOf: - $ref: '#/components/schemas/Model' `` Looks like the merge code does not lookup properties recursively through used traits in the (not schema) parent class. Looks like there is also a bug involved because even if you annotate both `HasId` and `Model` with `/** @OA\Schema() */` the `Model` class is missing a `allOf` ref to `HasId`.
Thanks for the clarification and opening an issue at the source repo! Let's see what they're suggesting or coming up with related to a fix.
@rickgoemans There is a PR ready that addresses your issue: https://github.com/zircote/swagger-php/pull/1331
Would you be able to try this and let me know if this fixes things for you? It sure does fix your reproducer :)
It is fixed, thanks for the update and the fix!
I just noticed that if you use a trait in both the parent (in my example the abstract Model
) and the children (in my example Address
), the properties in the traits of ONLY the parent class are loaded.
Since the title of this issue does not fully cover the new situation, should I create a new issue?
My new setup would include another trait called HasTimestamps
which contains two properties (created_at
and updated_at
) and is used in the Address
model/schema.
Here's the trait:
<?php
namespace ...
use Carbon\Carbon;
use OpenApi\Annotations as OA;
trait HasTimestamps
{
/**
* @OA\Property(
* format="date-time",
* type="string",
* readOnly=true,
* )
*/
public Carbon $created_at;
/**
* @OA\Property(
* format="date-time",
* type="string",
* readOnly=true,
* )
*/
public Carbon $updated_at;
}
Not a new issue, but since this is purely swagger-php related we should move this over to https://github.com/zircote/swagger-php/issues/1325 and close this one.
composer show | grep l5-swagger
)php -v
): 8.1.10Description:
I have a small setup for models that prevent me from writing duplicate properties, but when I add properties via a trait to the parent (abstract) model class, they are not generated in the children models.
Steps To Reproduce:
Make the following 3 files (of course correct the namespace to your situation);
Here's the result of the JSON file:
Here's the result of Swagger UI:
If you then include the
HasId
trait within theAddress
model, it will be shown correctly.Of course I can move the
id
property to theModel
class, but the general thought is that I have some properties that cannot be placed within just one file. Anybody else experiencing similar issues and/or maybe know a good solution? I can make a very complex class extending structure but that is not a realistic solution.