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

ref by class name ? #1311

Closed
momala454 opened this issue Sep 12, 2022 · 10 comments
Closed

ref by class name ? #1311

momala454 opened this issue Sep 12, 2022 · 10 comments

Comments

@momala454
Copy link
Contributor

momala454 commented Sep 12, 2022

Is it possible to reference a schema using the class name instead of the schema name ?

the following doesn't work obviously as it doesn't return something in the form of "#/components/schemas/foo" when doing Foo::class

(...)
new OA\Schema(
                        properties: [new OA\Property(
                            property: 'file_profile',
                            ref: Foo::class
                        )]
                    )


namespace helloWorld;
#[\OpenApi\Attributes\Schema()
class foo
{
}
@DerManoMann
Copy link
Collaborator

It's something that is not consistent yet, but using type instead of ref might work

@momala454
Copy link
Contributor Author

thanks

@momala454
Copy link
Contributor Author

momala454 commented Sep 13, 2022

With my use case, it seems to work with requestBody, but not with responses. I saw you're refactoring it in #1310, great !

new OA\Response(
                response: 409,
                description: 'Invalid parameter',
                content: new OA\MediaType(
                    mediaType: 'application/json',
                    schema: new OA\Schema(type: \MyNamespace\Class::class)
))

will show

"409": {
                        "description": "Invalid parameter",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "MyNamespace\\Class"
                                }
                            }
                        }
                    },

@likemusic
Copy link

@momala454 Make sure that \MyNamespace\Class has OA\Schema attribute.

@momala454
Copy link
Contributor Author

momala454 commented Sep 20, 2022

it doesn't have #[\OA\Schema] but it have an extended Attribute :

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
class ApiExceptionAttribute extends \OpenApi\Attributes\Schema
{}


#[ApiExceptionAttribute]
class Class{}

It doesn't work when I do it like this. Using "type" works only if I use directly \OpenApi\Attributes\Schema

@likemusic
Copy link

It doesn't work when I do it like this. Using "type" works only if I use directly \OpenApi\Attributes\Schema

@momala454 I have the same issue yesterday)

Look at here:

$schemas = $analysis->getAnnotationsOfType([OA\Schema::class, OAT\Schema::class], true);

Processor that expand classes for schemas do this just for OA\Schema and OAT\Schema (true flag at $analysis->getAnnotationsOfType() call) because many other annotation classes inherited from Schema class and processed separately by other processors.

image

At the moment I think that the best way to fix it would be add $exclude (array) parameter for getAnnotationsOfType() method to be able to extract schemas with child classes except already defined in package.

@momala454
Copy link
Contributor Author

momala454 commented Sep 20, 2022

Changing the true strict parameter to false in the class ExpandClasses is not enough to fix it, so any exclude parameter will not fix it neither I think

@likemusic
Copy link

likemusic commented Sep 21, 2022

@momala454 You are rigth. Problem was in another processor. See #1315

You can use it untill it woud be merged with fixes in composer.json like below:

    "require": {
        "zircote/swagger-php": "dev-fix-schemas-inheritance-v2 as 4.4.9"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "git@github.com:likemusic/swagger-php.git"
        }
    ]

@momala454
Copy link
Contributor Author

momala454 commented Sep 23, 2022

edit: nevermind my message before edit, I was modifying from the latest release, which doesn't use "OA\Schema" yet.
It works if I replace with AnnotationSchema
thanks

@DerManoMann
Copy link
Collaborator

The original issue was fixed in #1418 Any other issues should be raised in new, separate tickets.

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

No branches or pull requests

3 participants