cnizzardini / cakephp-swagger-bake

Automatically generate OpenAPI, Swagger, and Redoc documentation from your existing CakePHP code.
http://cakephpswaggerbake.cnizz.com/
MIT License
59 stars 20 forks source link

OpenApiResponse annotation doesn't work properly with schemaType: 'array' and ref. #508

Closed nomahideki closed 1 year ago

nomahideki commented 1 year ago

In case you specify OpenApiResponse annotation with with schemaType: 'array' and ref setting like this

class SomeController extends AppController
{
    #[OpenApiResponse(schemaType: 'array', ref: '#/components/schemas/SomeData')]
    public function index()
-----

swagger.json will be created in malformed pattern for response schema. Causing "(no example available)" on Swagger UI example column.

Wrong one

    "paths": {
        "\/api\/some": {
            "get": {
                "responses": {
                    "200": {
                        "content": {
                            "application\/json": {
                                "schema": {
                                    "allOf": [
                                        {
                                            "$ref": "#\/components\/schemas\/SomeData"
                                        }
                                    ],
                                    "type": "array"
                                }
                            }
                        }
                    }
                }
            }

Corrected swagger.json

    "paths": {
        "\/api\/some": {
            "get": {
                "responses": {
                    "200": {
                        "content": {
                            "application\/json": {
                                "schema": {
                                    "items": {
                                        "$ref": "#\/components\/schemas\/EventMaster"
                                    },
                                    "type": "array"
                                }
                            }
                        }
                    }
                }
            }

To Reproduce

  1. Define schema in swagger.yml

    components
    schemas
    SomeData
      type: object
      allOf:
        - $ref: '#/components/schemas/Data'
      properties:
        details:
          type: array
          items:
            $ref: '#/components/schemas/DataDetail'

    Schema Data/DataDetail is derived from other source.

  2. Define response schema using OpenApiResponse annotation #[OpenApiResponse(schemaType: 'array', ref: '#/components/schemas/SomeData')]

  3. bin/cake swagger bake

Expected behavior

Response schema in swagger.json will have

                                "schema": {
                                    "allOf": [
                                        {
                                            "$ref": "#\/components\/schemas\/SomeData"
                                        }
                                    ],
                                    "type": "array"

Version and Platform (please complete the following information):

Additional context I'll try to fix it myself.

cnizzardini commented 1 year ago

Yes I think you're right on this. Not sure how this got through for so long.

P.s. I cleaned up the format of this issue. You can use back ticks ``` followed by the language as closing and opening tags to format code in github issues. It makes it cleaner and easier for folks to read. No big deal, just letting you know.

nomahideki commented 1 year ago

Thanks for rewriting things! I'll do it better next time.

cnizzardini commented 1 year ago

Thanks for the contribution. I will release this before end of the week.

cnizzardini commented 1 year ago

https://github.com/cnizzardini/cakephp-swagger-bake/releases/tag/v2.5.4