OpenAPITools / OpenAPITools/openapi-generator

[Bug] Java Syntax Error for additionalProperties

Open
#8,055 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

For object schemas with additionalProperties the java generator produces code with a syntax error:

public MultiPart putHeadersItem(String key, List<String> headersItem) {
    if (this.headers == null) {
      this.headers = ; // <- HERE IS THE SYNTAX ERROR
    }
    this.headers.put(key, headersItem);
    return this;
  }
openapi-generator version

I using the maven plugin openapi-generator-maven-plugin in version 5.0.0-beta3

OpenAPI declaration file content or url

bug-api.yaml to reproduce the problem:

---
openapi: 3.0.1
servers:
  - url: "/rest/1.0"
info:
  title: Bug API
  version: "1.0"
paths:
  "/files":
    post:
      tags:
        - Files
      summary: Upload files
      description: Upload files
      operationId: addFiles
      requestBody:
        content:
          multipart/form-data:
            schema:
              "$ref": "#/components/schemas/MultiPart"
      responses:
        '201':
          description: Files successfully uploaded.
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
components:
  schemas:
    MultiPart:
      type: object
      properties:
        headers:
          type: object
          properties:
            empty:
              type: boolean
          additionalProperties:
            type: array
            items:
              type: string

pom.xml using the openapi-generator-maven-plugin in version 5.0.0-beta3 with java generator and native library:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>openapi-generator-bug</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <!-- for openapi generator -->
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-annotations</artifactId>
      <version>1.6.2</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.findbugs</groupId>
      <artifactId>jsr305</artifactId>
      <version>3.0.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.12.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-jsr310</artifactId>
      <version>2.12.0</version>
    </dependency>
    <dependency>
      <groupId>org.openapitools</groupId>
      <artifactId>jackson-databind-nullable</artifactId>
      <version>0.2.1</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- set Java 11 -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>5.0.0-beta3</version>
        <executions>
          <execution>
            <id>generate-core-api</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/bug-api.yaml</inputSpec>
              <generatorName>java</generatorName>
              <packageName>bug</packageName>
              <apiPackage>bug.api</apiPackage>
              <modelPackage>bug.model</modelPackage>
              <invokerPackage>bug</invokerPackage>
              <library>native</library>
              <configOptions>
                <sourceFolder>src/java</sourceFolder>
                <java8>true</java8>
                <dateLibrary>java8</dateLibrary>
              </configOptions>
              <generateApiTests>false</generateApiTests>
              <generateApiDocumentation>false</generateApiDocumentation>
              <generateModelTests>false</generateModelTests>
              <generateModelDocumentation>false</generateModelDocumentation>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

If you execute mvn clean compile, it generates the MultiPart.java in target/generated-sources/openapi/src/java/bug/model/MultiPart.java containing the syntax error mentioned above.

Command line used for generation
mvn clean compile
Suggest a fix/enhancement

I'm not into the mustache templates, but I guess fixing the template is probably enough.

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

Start with bug-api.yaml and run mvn clean compile to reproduce the generated target/generated-sources/openapi/src/java/bug/model/MultiPart.java output. Inspect the Java generator templates responsible for additionalProperties and verify that the generated MultiPart.java contains valid Java and compiles without the syntax error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.