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

OpenApiSchemaProperty support array of objects #545

Open
ChrisB85 opened this issue Apr 25, 2024 · 11 comments
Open

OpenApiSchemaProperty support array of objects #545

ChrisB85 opened this issue Apr 25, 2024 · 11 comments
Labels
enhancement New feature or request

Comments

@ChrisB85
Copy link

ChrisB85 commented Apr 25, 2024

Hi,

I'm reimplementing legacy API using CakePHP 5 and by the way of doing this I also want to document it with SwaggerBake.

Let's say I have controller action like:

#[OpenApiResponse(schemaType: 'array', schema: ArticlesCollectionElement::class)]
public function findArticles()
{
...
}

and the response is:

[
    {
        "Article": {
            "name": "some name",
            "id": 7204
        },
        "Producer": {
            "name": "Some name",
            "id": 325
        },
        "Language": {
            "symbol": "en",
            "id": 1
        }
    }
]

I'm using custom schema ArticlesCollectionElement as a OpenApiResponse schema:

#[OpenApiSchema(visibility: OpenApiSchema::VISIBLE_ALWAYS)]
class ArticlesCollectionElement extends DataTransferObject
{
    /**
     * @var ArticleProperties
     */
    #[OpenApiSchemaProperty(name: 'Article', type: 'object')]
    public ArticleProperties $Article;

    ...other properties

}

So here I would like to use object property of type ArticleProperties:

#[OpenApiSchema(visibility: OpenApiSchema::VISIBLE_ALWAYS)]
class ArticleProperties extends DataTransferObject
{
    #[OpenApiSchemaProperty(name: 'id', type: 'string', format: 'int32', example: '3214')]
    public string $id;
    #[OpenApiSchemaProperty(name: 'name', type: 'string', example: 'Some name')]
    public string $name;
}

I think I would need something like:

#[OpenApiSchemaProperty(name: 'Article', type: 'object', ref: ArticleProperties::class)]

The problem is that OpenApiSchemaProperty does not have ref param.
Am I missing something or this is just not possible to define object properties like that? Is there any workaround for this?

@ChrisB85 ChrisB85 changed the title OpenApiSchemaProperty schema question OpenApiSchemaProperty question Apr 25, 2024
@cnizzardini
Copy link
Owner

Am I understanding the main goal is to return a custom array of objects?

@cnizzardini
Copy link
Owner

I'd have to probably dig into this one for a bit. Off the top of my head your immediate workarounds (if this indeed is not possible) are:

Events: https://github.com/cnizzardini/cakephp-swagger-bake#event-system
CustomSchemaInterface: https://github.com/cnizzardini/cakephp-swagger-bake/blob/master/docs/attributes.md#schema

@ChrisB85
Copy link
Author

Am I understanding the main goal is to return a custom array of objects?

Yes exactly. I think it's the only thing missing to get full flexibility in documenting custom code. I'll see if I can do something with Events and CustomSchemaInterface.

Thanks!

@cnizzardini
Copy link
Owner

@ChrisB85 did this solve your needs?

@ChrisB85
Copy link
Author

ChrisB85 commented May 5, 2024

Unfortunately not. I tried to use events to modify the Swagger object but at some point I realized that It's just bad (to complicated) solution if I need to document many endpoints that way.

@cnizzardini
Copy link
Owner

Yeah I agree, I think your use-case is reasonable. I'll note this as a feature request.

@cnizzardini cnizzardini added the enhancement New feature or request label May 5, 2024
@cnizzardini cnizzardini changed the title OpenApiSchemaProperty question OpenApiSchemaProperty support array of objects May 5, 2024
@cnizzardini
Copy link
Owner

I think I've actually had this need in one of my applications, but out of sheer laziness have just left that portion of the schema not documented well.

@cnizzardini
Copy link
Owner

cnizzardini commented May 5, 2024

I'm curious @ChrisB85, if for a more reasonable short-term work around, can you use a mixture of $ref and OpenApiSchemaProperty. Something like this:

OpenAPI Schema:

#[OpenApiSchema(visibility: OpenApiSchema::VISIBLE_ALWAYS)]
class ArticleProperties extends DataTransferObject
{
    #[OpenApiSchemaProperty(name: 'id', type: 'string', format: 'int32', example: '3214')]
    public string $id;
    #[OpenApiSchemaProperty(name: 'name', type: 'string', example: 'Some name')]
    public string $name;
}

Controller:

#[OpenApiResponse(schemaType: 'array' ,ref: '#/components/schemas/ArticlesCollection')]
public function findArticles()
{
    // controller action code...
}

OpenAPI YAML:

components:
  schemas:
    ArticlesCollection:
      properties:
                  data:
                    items:
                      type: object
                      allOf:
                        - $ref: '#/components/schemas/ArticleProperties'
                    type: array

I think something like that may be possible. The idea being, document the object in your DTO, use OpenAPI YAML to document that its part of collection and then reference the YAML ref in your controller action.

@ChrisB85
Copy link
Author

ChrisB85 commented May 8, 2024

Thank you very much, I will try it :)

@ChrisB85
Copy link
Author

Sorry for a delay, today I finally had some time to use your sugesstion. Unfortunatelly the ArticleProperties schema is not included into swagger.json so I get result like:
image
I think I need some clever way to include it.

@cnizzardini
Copy link
Owner

Ah this makes sense, I think its only adding schemas when there is an associated route. You can try adding this attribute and setting visibility, but I am not sure it will work. We are in slightly uncharted territory:

https://github.com/cnizzardini/cakephp-swagger-bake/blob/master/docs/attributes.md#openapischema

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

No branches or pull requests

2 participants