Skip to content

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

Open

Description

@humbleCraftman

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);

    }

Activity

  1. mbaroody commented on Mar 26, 2022

    @mbaroody

    Having a similar issue with default: []. Expecting @RequestParam(value = "orderBy", required = false, defaultValue = ""), which would serialize to an empty List.

  2. borsch commented on Mar 29, 2022

    @borsch
    Member

    @humbleCraftman @mbaroody PR with required change has been merged to master. Feel free to build/get latest openapi-generator and use

  3. humbleCraftman commented on Mar 29, 2022

    @humbleCraftman
    Author

    Thanks for the reactivity

  4. elliebas commented on Mar 27, 2023

    @elliebas

    Hi, would it be possible to fix it for a Kotlin code generator as well? Thanks

  5. gorjan-mishevski commented on Dec 27, 2023

    @gorjan-mishevski

    +1 for the Kotlin generation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions