swagger-api / swagger-api/swagger-codegen

Jaxrs-di does not generate correct @SecurityRequirement scopes

Open
#9,498 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Using v3 of the swagger codegen lib and providing a yaml like so

Generates code like this for the listPets operation

@GET
  @Produces({"application/json"})
  @Operation(
      summary = "List all pets",
      description = "",
      security = {
        @SecurityRequirement(
            name = "OAuth2",
            scopes = {""})
      },
      tags = {"pets"})
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "An paged array of pets",
            content = @Content(schema = @Schema(implementation = Pets.class))),
        @ApiResponse(
            responseCode = "200",
            description = "unexpected error",
            content = @Content(schema = @Schema(implementation = Error.class)))
      })
  public Response listPets(
      @Parameter(
              description = "How many items to return at one time (max 100)",
              schema =
                  @Schema(
                      allowableValues = {},
                      minimum = "1",
                      maximum = "10"))
          @QueryParam("limit")
          Integer limit,
      @Context SecurityContext securityContext)
      throws NotFoundException {
    return delegate.listPets(limit, securityContext);
  }

(using Oas3 annotations)

Note that the scopes are empty.

Swagger-codegen version

io.swagger.codegen.v3:swagger-codegen-cli:3.0.8

Swagger declaration file content or url
swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
host: localhost:8080
basePath: /api
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
securityDefinitions:
  OAuth2:
    type: oauth2
    flow: accessCode
    authorizationUrl: https://example.com/oauth/authorize
    tokenUrl: https://example.com/oauth/token
    scopes:
      read: Grants read access
      write: Grants write access
      admin: Grants read and write access to administrative information
paths:
  /pets:
    get:
      security:
        - OAuth2: [read, write]
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          type: integer
          format: int32
          minimum: 1
          maximum: 10
      responses:
        "200":
          description: An paged array of pets
          headers:
            x-next:
              type: string
              description: A link to the next page of responses
          schema:
            $ref: '#/definitions/Pets'
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/Error'
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        "201":
          description: Null response
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/Error'
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          type: string
      responses:
        "200":
          description: Expected response to a valid request
          schema:
            $ref: '#/definitions/Pets'
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/Error'
definitions:
  Pet:
    required:
      - id
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
  Pets:
    type: array
    items:
      $ref: '#/definitions/Pet'
  Error:
    required:
      - code
      - message
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
Command line used for generation

Using the gradle plugin which passes these options to the cli tool

swaggerSources {
    create("mySources") {
        setInputFile(file("swagger/index.yaml"))

        code.language = "jaxrs-di" //jersey2 server stub with dependency injection via jsr310 interface
        code.additionalProperties = mapOf(
                "modelPackage" to "myModels",
                "apiPackage" to "myApis",
                "useOas2" to "false",
                "java8" to "true",
                "hideGenerationTimestamp" to "true",
                "dateLibrary" to "java8", // Date library to use
                "useTags"         to "true",  // Use tags for the naming
                "interfaceOnly"   to "true",   // Generating the Controller API interface and the models only
                "serializableModel" to "true"
        )

        code.components = listOf("models", "apis")
        code.dependsOn(this.validation)
    }
}
Steps to reproduce
git clone https://github.com/swagger-api/swagger-codegen
cd swagger-codegen
mvn clean package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
   -i https://gist.githubusercontent.com/mustafashabib/a8facc68c2b74b242adfac3aec94b0a2/raw/a0dad0bc7919fce8f0c4b6c97ac2d06c7a692433/petstore-with-oauth2.yaml \
   -l jaxrs-di \
-o generated
Related issues/PRs
Suggest a fix/enhancement

I tried providing my own generator but was unable to figure out exactly where the SecurityRequirements were being pulled in. They appeared to be done in the fromSecurity method here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the jaxrs-di generation using the Maven build and CLI command shown in the issue, then inspect fromSecurity in generators/.../DefaultCodegenConfig.java, the location identified by the reporter. Compare the generated @SecurityRequirement scopes with the YAML's read and write values; done means the generated list preserves those scopes instead of emitting an empty string.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.