zircote / swagger-php

A php swagger annotation and parsing library
http://zircote.github.io/swagger-php/
Apache License 2.0
5.04k stars 931 forks source link

Call to undefined method [SomeAnnotation]::isRoot() #1415

Closed eigan closed 1 year ago

eigan commented 1 year ago
Error: Call to undefined method Symfony\Component\Serializer\Annotation\DiscriminatorMap::isRoot()

/vendor/zircote/swagger-php/src/Analysis.php:341
/vendor/zircote/swagger-php/src/Processors/ExpandClasses.php:35
/vendor/zircote/swagger-php/src/Analysis.php:425
/vendor/zircote/swagger-php/src/Generator.php:458

This can happen if you have a different annotation on an @OA\Schema.

/**
 * @OA\Schema(title="Some DTO", required={"title", "data_type"})
 * @Serializer\DiscriminatorMap(typeProperty="data_type", mapping={
 *     "foo"="bar",
 * })
 */
abstract class SomeDto {}

The line in Analysis seems to just assume that all annotations on the class has the isRoot method.

if ($annotation->isRoot(OA\Schema::class) && !$annotation->_context->is('generated')) {

Seems to be related to https://github.com/zircote/swagger-php/commit/e449670473d7ecaff75dff744e8e9be68a79a44c https://github.com/zircote/swagger-php/pull/1403

DerManoMann commented 1 year ago

Have you tried upgrading swagger-php ? I tried creating a testcase and it looks like the latest code should be ok with this.

eigan commented 1 year ago

Sorry, I should have provided better example.

<?php
require "vendor/autoload.php";

use OpenApi\Annotations as OA;

/**
 * @Annotation
 */
class CustomAnnotation {}

/**
 * @OA\Schema
 * @CustomAnnotation
 */
class SomeParent {}

/**
 * @OA\Schema
 */
class Child extends SomeParent {}

$generator = new \OpenApi\Generator();

$generator->generate([
    __FILE__
]);
DerManoMann commented 1 year ago

Interesting - things change depending on whether there is a namespace used or not :waffle:

eigan commented 1 year ago

Thank you for the quick fix! 🙂

akalineskou commented 1 year ago

FYI this is also happening in https://github.com/zircote/swagger-php/blob/47922894170ca3c51703230e0d9417e22edcefd4/src/Processors/AugmentSchemas.php#L27 also

Call to undefined method OpenApi\\Attributes\\JsonContent::isRoot() Using the latest 4.7.1 version.

Adding the same $schema instanceof OA\Schema check seems to get rid of the error, but I'm not sure if that is the correct solution. I take this back, it this does not work, JsonContent is an instance of OA\Schema but still does not have the isRoot function, not sure why in this PR https://github.com/zircote/swagger-php/pull/1417 it works... I've added localy method_exists($schema, 'isRoot') just to have it pass

DerManoMann commented 1 year ago

I am not sure what is going on there - JsonContent definitly shoudl have AbstractAnnotation as parent somewhere and that has the isRoot method.

akalineskou commented 1 year ago

@DerManoMann You are correct, the function should be there but isn't... something went haywire with my vendor folder, re-installed everything and it works.