OpenAPITools / OpenAPITools/openapi-generator

[BUG] Multiple request-body content types generate a dangling request-model import

Open
#24,727 0 comments 1 reaction 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

Description
Expected behavior

The generated sources compile. DefaultApi.java uses MultiArticleImporter as its
JSON request body and does not import a model that was never generated.

Actual behavior

During generation, OpenAPI Generator creates an inline schema for the multipart body
and then skips its model because skipFormModel defaults to true:

Inline schema created as createMultiArticleImporter_request.
Model createMultiArticleImporter_request not generated since it's marked as unused
(due to form parameters) and skipFormModel (global property) set to true (default)

openapi-generator version

openapi-generator:7.24.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: OpenAPI Generator Bug Repro
  version: 1.0.0
paths:
  /api/multi_article_importer/:
    post:
      operationId: createMultiArticleImporter
      summary: Create a multi article importer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MultiArticleImporter'
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MultiArticleImporter'
components:
  schemas:
    MultiArticleImporter:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string

pom.xml:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>openapi-bug-repro</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <description>
    Minimal reproducer for openapi-generator bug:
    When a requestBody defines multiple content types (application/json + multipart/form-data),
    the generator emits an import for a *CreateRequest wrapper class that it never creates.
    See: https://github.com/OpenAPITools/openapi-generator/issues/...
  </description>

  <properties>
    <java.version>17</java.version>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <openapi-generator.version>7.24.0</openapi-generator.version>
  </properties>

  <dependencies>
    <!-- Required for generated Spring controller interfaces -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>6.1.14</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>6.1.14</version>
    </dependency>
    <!-- Required for @Valid / @NotNull annotations in generated code -->
    <dependency>
      <groupId>jakarta.validation</groupId>
      <artifactId>jakarta.validation-api</artifactId>
      <version>3.0.2</version>
    </dependency>
    <dependency>
      <groupId>jakarta.annotation</groupId>
      <artifactId>jakarta.annotation-api</artifactId>
      <version>2.1.1</version>
    </dependency>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>6.0.0</version>
      <scope>provided</scope>
    </dependency>
    <!-- Required for @Parameter / @Operation annotations in generated code -->
    <dependency>
      <groupId>io.swagger.core.v3</groupId>
      <artifactId>swagger-annotations</artifactId>
      <version>2.2.22</version>
    </dependency>
    <!-- Required for @JsonProperty in generated model classes -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.17.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.17.2</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>${openapi-generator.version}</version>
        <executions>
          <execution>
            <id>generate-api</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
              <generatorName>spring</generatorName>
              <apiPackage>org.example.api</apiPackage>
              <modelPackage>org.example.model</modelPackage>
              <configOptions>
                <!-- Generate only interfaces, no full Spring Boot app needed -->
                <interfaceOnly>true</interfaceOnly>
                <useSpringBoot3>true</useSpringBoot3>
                <useTags>true</useTags>
                <!-- Keep the reproducer focused on the dangling request-model import. -->
                <openApiNullable>false</openApiNullable>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
        <configuration>
          <source>17</source>
          <target>17</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Generation Details

error:

[ERROR] .../target/generated-sources/openapi/src/main/java/org/example/api/DefaultApi.java:[8,25]
error: cannot find symbol
symbol: class CreateMultiArticleImporterRequest
location: package org.example.model

the generated DefaultApi.java contains:

import org.example.model.CreateMultiArticleImporterRequest; // class is never generated

The import is unused, and no CreateMultiArticleImporterRequest.java exists anywhere
under target/generated-sources/.

Steps to reproduce

mvn clean compile

Related issues/PRs
Suggest a fix

Workarounds:

  • Remove the multipart/form-data content so the operation has only one request-body type.
  • Alternatively, set the global property skipFormModel to false. This generates the
    otherwise unused CreateMultiArticleImporterRequest model and makes the import resolvable.

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 by running mvn clean compile with src/main/resources/api.yaml and inspect the generated DefaultApi.java under target/generated-sources/openapi. Trace how the multipart request body produces the CreateMultiArticleImporterRequest import, then verify that the generated sources compile without an import for a missing model.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.