OpenAPITools / OpenAPITools/openapi-generator

[BUG] [SPRING] Generator do not respect reserved words mappings for FormParams

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

There is an error in mustache template file modules/openapi-generator/src/main/resources/JavaSpring/formParams.mustache that causes a compilation error if you use a reserved keyword as the name of a form parameter - baseName is using instead of paramName

openapi-generator version

4.3.1 (latest stable release)

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: Example
  description: Example
  version: 1.0.0

paths:
  /example/api:
    post:
      summary: summary
      operationId: description
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                super:
                  type: string
                  format: binary
                package:
                  type: string
                  format: binary
      responses:
        200:
          description: response
Generation Details
    @ApiOperation(value = "summary", nickname = "description", notes = "", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "response") })
    @PostMapping(
        value = "/example/api",
        consumes = { "multipart/form-data" }
    )
    default ResponseEntity<Void> description(@ApiParam(value = "") @Valid @RequestPart(value = "super", required = false) MultipartFile super,@ApiParam(value = "") @Valid @RequestPart(value = "package", required = false) MultipartFile package) {
        return getDelegate().description(_super, _package);
    }

Which leads to a completion error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project open-api-reserved-words-mappings: Compilation failure: Compilation failure: 
[ERROR] .../api/ExampleApi.java:[48,110] <identifier> expected
[ERROR] .../api/ExampleApi.java:[48,191] <identifier> expected
[ERROR] .../api/ExampleApi.java:[48,192] = expected
Steps to reproduce
  1. Generate code using maven plugin with the following configuration:
<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>4.3.1</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <inputSpec>${project.basedir}/src/main/openapi/spec.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <generateSupportingFiles>false</generateSupportingFiles>
                <configOptions>
                    <library>spring-boot</library>
                    <interfaceOnly>true</interfaceOnly>
                    <skipDefaultInterface>true</skipDefaultInterface>
                    <hideGenerationTimestamp>true</hideGenerationTimestamp>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
Suggest a fix

paramName should be used instead if baseName

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

Inspect modules/openapi-generator/src/main/resources/JavaSpring/formParams.mustache and compare the reserved-word handling with the parameter name used in the generated method. Reproduce the issue with the supplied OpenAPI declaration and Maven configuration; done means generated form parameters use valid mapped names and the generated Java code compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.