swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.77k stars 1.2k forks source link

Swag v2 with open api --v3.1 breaks model composition overrides #1917

Closed gabegibbturion closed 2 weeks ago

gabegibbturion commented 4 weeks ago

Versions Swaggo - github.com/swaggo/swag/v2 v2.0.0-rc3 Go - 1.21 Parse command - swag init --parseDependency --parseInternal --v3.1 --tags Imaging-Missions,Opportunities,Images,Tasking-Orders

Describe the bug When using an interface return type

type PaginationResult struct {
    Data  interface{} `json:"data"`
    Total int64       `json:"total"`
}

and two different handlers that implement its own version of the interface

// HANDLER 1 RETURN
//  @Success        200                         {object}    models.PaginationResult{data=[]models.TaskingOrder} "List of Tasking orders retrieved successfully"

// HANDLER 2 RETURN
//  @Success        200                         {object}    models.PaginationResult{data=[]models.Opportunity}  "List of Opportunities"

I get an issue where the tasking orders overrides the opportunities return, causing every version of PaginationResult to only return TaskingOrders. This was not an issue on the main branch but only seemed to arise when using v2. Is there something I am doing wrong? Is this a bug? I have more handlers, where taking Tasking Orders away just makes the rest default to only opportunity.

addshore commented 2 weeks ago

I'm also experiencing this, what was working in v1, and oon an openapi spec v2, does not work in v2 with --v3.1

I had something like this

// @Success 200 {object} PaginatedResult{data=[]Product}

Which would generate soetihng like this

                    "200": {
                        "description": "OK",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/PaginatedResult"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/Product"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    },

However in V2 with --3.1 it generates the following

          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    {
                      "$ref": "#/components/schemas/data"
                    }
                  ],
                  "properties": {
                    "data": {
                      "description": "Paginated data"
                    },
                    "limit": {
                      "description": "Requested elements per page",
                      "type": "integer"
                    },
                    "page": {
                      "description": "Requested page number",
                      "type": "integer"
                    },
                    "totalElements": {
                      "description": "Total elements in the database for given filters",
                      "type": "integer"
                    },
                    "totalPages": {
                      "description": "Total pages of elements given limit and filters",
                      "type": "integer"
                    }
                  },
                  "type": "object"
                }
              }
            },
            "description": "OK"
          },

Where the data schema is something that appears to get conflicted over multiple times, thus it is actually something like this

      "data": {
        "properties": {
          "data": {
            "items": {
              "$ref": "#/components/schemas/User"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
addshore commented 2 weeks ago

I did some reading and trying things out

Ultimately this now works

// @Success 200 {object} presentation.PaginatedResult[presentation.User]

Making use of generics https://github.com/swaggo/swag?tab=readme-ov-file#how-to-use-generics https://github.com/swaggo/swag/blob/master/testdata/generics_nested/api/api.go https://github.com/swaggo/swag/blob/master/testdata/generics_nested/web/handler.go

gabegibbturion commented 2 weeks ago

I did some reading and trying things out

Ultimately this now works

// @Success 200 {object} presentation.PaginatedResult[presentation.User]

Making use of generics https://github.com/swaggo/swag?tab=readme-ov-file#how-to-use-generics https://github.com/swaggo/swag/blob/master/testdata/generics_nested/api/api.go https://github.com/swaggo/swag/blob/master/testdata/generics_nested/web/handler.go

That worked thanks!