OpenAPITools / OpenAPITools/openapi-generator

[BUG][SPRING] Spring generator not creating query parameter objects with @ParameterObject annotation

Open
#12,709 1 comment 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If I generate spring code with springoc annotation provider from an OpenAPI spec where an operation is defined that contains an object as query parameter I expect this object being annotated with @ParameterObject or have a possibility to do so.

openapi-generator version
  • openapi-generator-maven-plugin 6.0.0
  • tried also with openapi-generator latest master and 7.0.x branch
OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Query object as @ParameterObject
  version: '1.0'
servers:
  - url: http://localhost:8080
tags:
  - name: "Book"

paths:
  /books:
    get:
      tags:
        - "Book"
      summary: Get all Books
      operationId: getAllBooks
      x-spring-paginated: true
      parameters:
        - name: BookFilter
          in: query
          schema:
            type: object
            properties:
              name:
                type: string
              author:
                type: string
      responses:
        '200':
          description: A list of Books
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Book"
components:
  schemas:
    Book:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        author:
          type: string
Generation Details

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \ -i above_file.yaml \ -g spring \ -o /generated-output

Steps to reproduce
  • run generation with the command stated above

Output is:


/**
     * GET /books : Get all Books
     *
     * @param bookFilter  (optional)
     * @return A list of Books (status code 200)
     */
    @Operation(
        operationId = "getAllBooks",
        summary = "Get all Books",
        tags = { "Book" },
        responses = {
            @ApiResponse(responseCode = "200", description = "A list of Books", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = Book.class))
            })
        }
    )
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/books",
        produces = { "application/json" }
    )
    default ResponseEntity<List<Book>> getAllBooks(
        @Parameter(name = "BookFilter", description = "") @Valid GetAllBooksBookFilterParameter bookFilter,
        @ParameterObject final Pageable pageable
    ) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "{ \"author\" : \"author\", \"name\" : \"name\", \"id\" : 0 }";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

Related issues/PRs
Suggest a fix

I suggest annotating the request parameter object with ParameterObject instead of Parameter like this:


/**
     * GET /books : Get all Books
     *
     * @param bookFilter  (optional)
     * @return A list of Books (status code 200)
     */
    @Operation(
        operationId = "getAllBooks",
        summary = "Get all Books",
        tags = { "Book" },
        responses = {
            @ApiResponse(responseCode = "200", description = "A list of Books", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = Book.class))
            })
        }
    )
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/books",
        produces = { "application/json" }
    )
    default ResponseEntity<List<Book>> getAllBooks(
        @ParameterObject @Valid GetAllBooksBookFilterParameter bookFilter,
        @ParameterObject final Pageable pageable
    ) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "{ \"author\" : \"author\", \"name\" : \"name\", \"id\" : 0 }";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

Would appreciate if someone could make a suggestion to make this possible or comment with any concerns about this.

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

Start with the Spring generator and reproduce the issue using the OpenAPI YAML and the provided openapi-generator-cli command. Compare the generated method parameter annotations for the query object with the expected output, then add or update coverage so the generated object uses @ParameterObject while Pageable remains supported.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.