oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Generating iris server with securitySchemes is broken

Open zaibon opened this issue 9 months ago • 0 comments

When generting an iris server from a specification that uses any securitySchemes. The generate code tries to uses a method Set on the iris.Context that does not exists.

Example specification to reproduce the bug:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
  termsOfService: https://swagger.io/terms/
  contact:
    name: Swagger API Team
    email: [email protected]
    url: https://swagger.io
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
  - url: https://petstore.swagger.io/api
paths:
  /pets:
    get:
      summary: Returns all pets
      operationId: findPets
      parameters:
        - name: tags
          in: query
          description: tags to filter by
          required: false
          style: form
          schema:
            type: array
            items:
              type: string
        - name: limit
          in: query
          description: maximum number of results to return
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: pet response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
      security:
        - BearerAuth: ["pet:read"]
components:
  schemas:
    Pet:
      allOf:
        - $ref: '#/components/schemas/NewPet'
        - required:
            - id
          properties:
            id:
              type: integer
              format: int64
              description: Unique id of the pet

    NewPet:
      required:
        - name
      properties:
        name:
          type: string
          description: Name of the pet
        tag:
          type: string
          description: Type of the pet

    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
          description: Error code
        message:
          type: string
          description: Error message

  securitySchemes:
      BearerAuth:
        type: http
        scheme: bearer
        bearerFormat: JWT

Extract of the generated code that contains the error:

// FindPets converts iris context to params.
func (w *ServerInterfaceWrapper) FindPets(ctx iris.Context) {

	var err error

	ctx.Set(BearerAuthScopes, []string{"pet:read"})

	// Parameter object where we will unmarshal all parameters from the context
	var params FindPetsParams
	// ------------- Optional query parameter "tags" -------------

	err = runtime.BindQueryParameter("form", true, false, "tags", ctx.Request().URL.Query(), &params.Tags)
	if err != nil {
		ctx.StatusCode(http.StatusBadRequest)
		ctx.Writef("Invalid format for parameter tags: %s", err)
		return
	}

	// ------------- Optional query parameter "limit" -------------

	err = runtime.BindQueryParameter("form", true, false, "limit", ctx.Request().URL.Query(), &params.Limit)
	if err != nil {
		ctx.StatusCode(http.StatusBadRequest)
		ctx.Writef("Invalid format for parameter limit: %s", err)
		return
	}

	// Invoke the callback with all the unmarshaled arguments
	w.Handler.FindPets(ctx, params)
}

The line

ctx.Set(BearerAuthScopes, []string{"pet:read"})

is wrong.

I believe the proper way to do this would be to use:

ctx.Values().Set(BearerAuthScopes, []string{"pet:read"})

zaibon avatar May 20 '25 16:05 zaibon