wI2L / fizz

:lemon: Gin wrapper with OpenAPI 3 spec generation
https://pkg.go.dev/github.com/wI2L/fizz
MIT License
214 stars 52 forks source link

Given i declare two similar parameterized routes with different verbs, parameters are generated on a single verb #14

Closed FabienM closed 6 years ago

FabienM commented 6 years ago

I have following router declaration:

    appRoutes.GET("/:domain/:name/:version", []fizz.OperationOption{fizz.ID("GetApplication")}, hateoas.HandlerFindOneBy(appRepo))
    appRoutes.DELETE("/:domain/:name/:version", []fizz.OperationOption{fizz.ID("DeleteApplication")}, hateoas.HandlerRemoveOneBy(appRepo))
    appRoutes.PUT("/:domain/:name/:version", []fizz.OperationOption{fizz.ID("PutApplication")}, application.HandlerCreate(appRepo))

Generated spec embeds parameter declaration only on the PUT route:

  '/api/v1/applications/{domain}/{name}/{version}':
    get:
      tags:
        - applications
      operationId: GetApplication
      responses:
        '200':
          description: OK
    put:
      tags:
        - applications
      operationId: PutApplication
      parameters:
        - name: domain
          in: path
          required: true
          schema:
            type: string
        - name: name
          in: path
          required: true
          schema:
            type: string
        - name: version
          in: path
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                '':
                  type: integer
                  format: int32
                _createdAt:
                  type: string
                  format: date-time
                _links:
                  type: array
                  items:
                    $ref: '#/components/schemas/HateoasResourceLink'
                _updatedAt:
                  type: string
                  format: date-time
                manifest:
                  $ref: '#/components/schemas/PostgresJsonb'
    delete:
      tags:
        - applications
      operationId: DeleteApplication

And so OpenAPI parser claims some errors:

Semantic error at paths./api/v1/applications/{domain}/{name}/{version} Declared path parameter "domain" needs to be defined within every operation in the path (missing in "get", "delete"), or moved to the path-level parameters object Jump to line 71

Semantic error at paths./api/v1/applications/{domain}/{name}/{version} Declared path parameter "name" needs to be defined within every operation in the path (missing in "get", "delete"), or moved to the path-level parameters object Jump to line 71

Semantic error at paths./api/v1/applications/{domain}/{name}/{version} Declared path parameter "version" needs to be defined within every operation in the path (missing in "get", "delete"), or moved to the path-level parameters object Jump to line 71

wI2L commented 6 years ago

Based on the content of your previous issue https://github.com/wI2L/fizz/issues/13, it looks like your closure prototypes are func (c *gin.Context) T, so tonic does not send any input type informations to the generator, hence, it thinks there is no parameters for operations GET and DELETE.

The OpenAPI spec generator has no way to figure out the type of the parameters based solely on the path itself.

I suggest to fix the returned closures to accept an input struct that describe the path parameters you use in those operations.

type T struct {
    Domain string `path:"domain"`
    Name string `path:"name"`
    Version string `path:"version"`
}