Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
humbleCraftman opened this issue Mar 24, 2022 · 5 comments

Comments

@humbleCraftman
Copy link

humbleCraftman commented Mar 24, 2022

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

    }
@mbaroody
Copy link

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

@borsch
Copy link
Member

borsch commented Mar 29, 2022

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

@humbleCraftman
Copy link
Author

Thanks for the reactivity

@elliebas
Copy link

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

@gorjan-mishevski
Copy link

+1 for the Kotlin generation.

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

No branches or pull requests

5 participants