Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #1551
Closed
Open
Issue created 6 years ago by Administrator@rootContributor
  • New related issue

  • Report abuse to administrator

  • New related issue

  • Report abuse to administrator

[spring] Invalid default values for arrays and maps on API method declarations

Closed

[spring] Invalid default values for arrays and maps on API method declarations

Created by: rubms

Description

When parameters of type array are added to an operation, the generated Spring code includes an invalid defaultValue in the Spring MVC parameter annotations. For instance, an array of strings query parameter would generate a Spring code like the following:

@Valid @RequestParam(value = "stringArrayParam", required = false, default="new ArrayList<>()") List<String> stringArrayParam

That generated defaultValue is invalid: the Spring MVC application will throw an error when a request is performed with no value specified for the parameter.

openapi-generator version

Reproduced with openapi-generator v3.3.3 and previous versions.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: "Spec for testing the default value of array parameters"
  version: 1.0.0
  title: Array parameters test case.
paths:
  /pets:
    get:
      operationId: getPets
      summary: Pets.
      description: "Get pets."
      parameters:
      - name: stringArrayParam
        in: query
        description: "Test array query parameter"
        required: false
        schema:
          type: array
          items:
            type: string
      tags:
      - Pets
      responses:
        200:
          description: Successful request.
components:
  schemas:
    ExampleObject:
      type: object
      properties:
        arrayProperty:
          type: array
          items:
            type: string
Command line used for generation

java -jar openapi-generator-cli-3.3.3.jar generate -g spring -i test.yml -o test

Additionally, the configuration property library is set to spring-mvc:

{
	"library": "spring-mvc"
}
Steps to reproduce
  1. Generate a Spring-MVC server application from the previous specification.
  2. Run the application.
  3. Throw a GET request to the /pets path (getPets operation) leaving the stringArrayParam query parameter blank or unspecified.

The server will throw the following error:

Related issues/PRs

This is related to #890, though not the same issue.

Suggest a fix/enhancement

The problem is that there are 2 different cases with respect to default values:

  • The default value specified by the user, that should be included as default value in the Spring MVC API method annotations.
  • The default variable initializer, which may be affected by the default value specified by the user but differs in data-types like arrays or maps.

However the Spring code generator uses the same value (the default variable initializer, in my opinion) for both: initializing variables and specifying Spring MVC annotations.

The Spring MVC code generator must use a different "default value" for both cases.

  1. Oh no!

    You are trying to upload something other than an image. Please upload a .png, .jpg, .jpeg, .gif, .bmp, .tiff or .ico.

    Incoming!

    Drop your designs to start your upload.
Tasks
0

No tasks are currently assigned. Use tasks to break down this issue into smaller parts.

Linked items
0

Link issues together to show that they're related. Learn more.

Activity


Please register or sign in to reply
0 Assignees
None
Assign to
Labels
2
Issue: Bug Server: Spring
2
Issue: Bug Server: Spring
    Assign labels
  • Manage project labels

Milestone
4.0.0
4.0.0 (expired)
Due date
None
None
None
Time tracking
No estimate or time spent
Confidentiality
Not confidential
Not confidential

You are going to turn on confidentiality. Only project members with at least the Reporter role, the author, and assignees can view or be notified about this issue.

Lock issue
Unlocked
1
1 participant
Administrator
Reference: OpenAPITools/openapi-generator#1551

Menu

Explore Projects Groups Snippets