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]: Why I cant see in swagger properties from model? #2301

Open
scion4581 opened this issue Jun 18, 2024 · 7 comments
Open

[Question]: Why I cant see in swagger properties from model? #2301

scion4581 opened this issue Jun 18, 2024 · 7 comments
Labels

Comments

@scion4581
Copy link

Version

4.27.0

Question

Hi!
Cant get this to work so much time. I'm feeling the truth is somewhere nearby
Simple example, test controller, and test model. But I cant see properties on the response in swagger, its like it doesnt see attributes on the model property. I must to point NelmioApiDocBundle path where are all my classes with attributes in some way?

Controller
Model
Swagger

Will be grateful for any help, thx

Additional context

No response

@DjordyKoert
Copy link
Collaborator

can you share the generated json

@scion4581
Copy link
Author

@DjordyKoert thx for response

following nelmio dump output

{
    "openapi": "3.0.0",
    "info": {
        "title": "Test App",
        "description": "This is test app api",
        "version": "1.0.0"
    },
    "paths": {
        "/api/test": {
            "post": {
                "tags": [
                    "test"
                ],
                "summary": "Test summary",
                "description": "Test description",
                "operationId": "post_app_test",
                "requestBody": {
                    "description": "User login credentials",
                    "content": {
                        "application/json": {
                            "schema": {
                                "properties": {
                                    "test": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Test success response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/TestModel"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "TestModel": {
                "type": "object"
            }
        }
    }
}

See, at the bottom TestModel is empty, no fields descriptions :(

Even when I'm making syntax errors in #[OA\Problablabla(type: 'string', maxLength: 255)].
There are no errors at all, that's why I am thinking NelmioApiDocBundle doenst even see this class.

What could it be?

@DjordyKoert
Copy link
Collaborator

Hmm not sure.

  • Does removing the getters & setters make a difference (because the property itself is public)?
  • Can you try removing the docblock?

@scion4581
Copy link
Author

Nope, nothing changed...:(

@Dropelikeit
Copy link

Dropelikeit commented Jul 4, 2024

Interesting bug, because I have the same problem, but only with private and protected properties. When I change the visibility of the property to public, it shows up in the swagger interface.
I'm using the attributes feature instead of annotations, but the even if I wanted to switch, the annotations don't work for me at all. I am not sure why.
I created a new project with Symfony and only this package as a test, there the behavior is the same (latest Symfony version and latest version of this package). So I could already rule out that it was a problem with my project.
I have tested from version 4.28 - 4.18, everywhere I had the same behavior.

DTO:

namespace App\Controller;

use OpenApi\Attributes as SWG;

final class TestDto
{
    public function __construct(
        #[SWG\Property(type: 'integer', example: 12)]
        private int $age
    )
    {

    }
}

Controller:

namespace App\Controller;

use Nelmio\ApiDocBundle\Annotation\Model;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Attribute\Route;
use OpenApi\Attributes as SWG;

final class TestController extends AbstractController
{
    #[SWG\Response(
        response: 200,
        description: 'Returns all available countries',
        content: new Model(type: TestDto::class),
    )]
    #[Route('/api/test', name: 'test')]
    public function index()
    {

    }
}

JSON-OUTPUT:

{
    "openapi": "3.0.0",
    "info": {
        "title": "My App",
        "description": "This is an awesome app!",
        "version": "0.1.0"
    },
    "paths": {
        "/api/test": {
            "get": {
                "operationId": "get_test",
                "responses": {
                    "200": {
                        "description": "Returns all available countries",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/TestDto"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "TestDto": {
                "type": "object"
            }
        }
    },
    "tags": [
        {
            "name": "Location",
            "description": "Location"
        }
    ]
}

PHP 8.2
Package: 4.28

Do you have any ideas on how I can solve the problem?

@darius-v
Copy link

darius-v commented Dec 18, 2024

I was looking into request json instead of response json and thinking that field does not appear but it actually was in doc.

@DerManoMann
Copy link
Contributor

Not sure about the way the model stuff works, but would adding a OA\Schema attribute to the model/DTO help?

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

5 participants