OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Compilation error: element value must be a constant expression

Open
#2,540 4 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
  • Have you provided a full/minimal spec to reproduce the issue?
    yes

  • Have you validated the input using an OpenAPI validator (example)?
    yes

  • What's the version of OpenAPI Generator used?
    3.3.4

  • Have you search for related issues/PRs?
    yes

  • What's the actual output vs expected output?
    The generated java code is:

@RequestParam(value = "username", required = true, defaultValue=null)

And that code doesn't compile due to:

StuffApi.java:[53,123] element value must be a constant expression

In version 3.3.3 that same spec was working and was generating:

@RequestParam(value = "username", required = true, defaultValue="null")
  • [Optional] Bounty to sponsor the fix (example)
    no
Description

When migrating from openapi-generator-maven-plugin 3.3.3 to 3.3.4 the generated code doesn't compile anymore.

openapi-generator version

I'm using version 3.3.4 of openapi-generator.

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: Smple API
  contact:
    name: Benoit Heinrich
    email: benoit.heinrich@gmail.com
  version: 1.0.0

paths:
  /stuff:
    description: Manage stuff
    get:
      summary: Get some stuff
      operationId: getStuff
      parameters:
        - in: query
          name: username
          required: true
          schema:
            $ref: '#/components/schemas/Username'
        - in: query
          name: filename
          allowReserved: true
          required: false
          schema:
            $ref: '#/components/schemas/Filename'
      responses:
        200:
          description: worked
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/WorkedOperationStatus'
              example:
                status: 'success'
                message: 'It worked'
                comment: 'the work to be done is done!'
components:
  schemas:
    Workload:
      type: object
      required:
        - comment
      properties:
        comment:
          $ref: '#/components/schemas/Comment'

    Comment:
      description: A user comment
      type: string
      minLength: 1
      maxLength: 1000
      example:
        Some comment from user

    WorkedOperationStatus:
      type: object
      required:
        - status
        - message
      properties:
        status:
          type: string
          enum:
            - error
            - success
        message:
          type: string
        comment:
          $ref: '#/components/schemas/Comment'
      example:
        status: 'error'
        message: 'Unknown work type'
        comment: 'the work to be done is done!'

    Username:
      description: The name of the user doing the action.
      type: string
      minLength: 1
      maxLength: 60
      example:
        John

    Filename:
      description: The name of a configuration file.
      type: string
      pattern: '^[a-zA-Z0-9_-]+\.(csv|groovy)$'
      example:
        04_a_DOCUMENTS.csv
Command line used for generation

This happens using openapi-generator-maven-plugin, here is the plugin configuration:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir.parent}/api/src/main/resources/spec.yaml</inputSpec>
                            <generatorName>spring</generatorName>
                            <library>spring-boot</library>
                            <validateSpec>true</validateSpec>
                            <generateSupportingFiles>false</generateSupportingFiles>
                            <apiPackage>com.bh.sample</apiPackage>
                            <modelPackage>com.bh.sample</modelPackage>
                            <generateModels>false</generateModels>
                            <withXml>true</withXml>
                            <configOptions>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                                <delegatePattern>true</delegatePattern>
                                <useBeanValidation>true</useBeanValidation>
                                <java8>true</java8>
                                <reactive>false</reactive>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce

A sample project is available here: https://github.com/benoitheinrich/spring-cloud-contract-long-text

Run mvn clean install

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

Reproduce the regression from the linked sample project with mvn clean install and inspect the generated StuffApi.java, focusing on the @RequestParam for username. Compare the output from generator versions 3.3.3 and 3.3.4; done means the generated Java compiles and preserves the expected default-value representation.

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.