OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][SPRING] Default values for arrays ignored in spring annotation defaultValue

Open
#11,957 5 comments 0 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
  • [x ] Have you provided a full/minimal spec to reproduce the issue?
  • [ x] Have you validated the input using an OpenAPI validator (example)?
  • [ x] Have you tested with the latest master to confirm the issue still exists?
  • [ x] Have you searched for related issues/PRs?
  • [ x] What's the actual output vs expected output?
  • [- ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Code generation for spring server does not generate default value for array of primitive type (for array of complex type it is complicated, probably impossible)

Example below:


openapi: '3.0.2'
info:
  title: Search API
  version: '1.0'
servers:
  - url: http://localhost:8080/api/v1
tags:
  - name: "search"
paths:
  /search:
    get:
      tags:
        - search
      description: ""
      operationId: "search"
      parameters:
        - in: query
          name: p
          description: Page
          schema:
            type: integer
            default: 0
            minimum: 0
        - in: query
          name: orderBy
          description: Filter by status not equal (AND condition applied)
          schema:
            type: array
            default: ["updatedAt:DESC", "createdAt:DESC"]
            items:
              type: string
              enum: [ "createdAt:ASC", "createdAt:DESC",
              "updatedAt:ASC", "updatedAt:DESC" ]
      responses:
        204:
          description: "Custom response"

Generated:

    @Operation(
        operationId = "search",
        tags = { "search" },
        responses = {
            @ApiResponse(responseCode = "204", description = "Custom response")
        }
    )
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/search"
    )
    default ResponseEntity<Void> search(
        @Min(0) @Parameter(name = "p", description = "Page") @Valid @RequestParam(value = "p", required = false, defaultValue = "0") Integer p,
        @Parameter(name = "orderBy", description = "Filter by status not equal (AND condition applied)") @Valid @RequestParam(value = "orderBy", required = false) List<String> orderBy
    ) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

Expected something like: (as said here):

    default ResponseEntity<Void> search(
        @Min(0) @Parameter(name = "p", description = "Page") @Valid @RequestParam(value = "p", required = false, defaultValue = "0") Integer p,
        @Parameter(name = "orderBy", description = "Filter by status not equal (AND condition applied)") @Valid @RequestParam(value = "orderBy", required = false, defaultValue = "updatedAt:DESC,createdAt:DESC") List<String> orderBy
    ) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

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 by reproducing the supplied OpenAPI example with the Spring server generator and inspect how array query parameters are rendered into @RequestParam annotations. Compare the generated output with the expected comma-separated defaultValue, including the primitive-array case. Done means the generated method includes the array default and existing generation tests cover the behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.