[BUG][JAVA] Bug generating attribute with multiple types in OAS 3.1

未关闭
#25,756 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Inf1mum 已经在做这个了。

开始于 2026年11月19日。

  • #25761 来自 @Inf1mum —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java, openapi, yaml
领域
tooling

调研方向

Start in OpenAPINormalizer where the log line about multiple types is emitted, and trace how the 3.1 type array reaches the composed-schema naming that produces AnyOfstring. Reproduce with the spec in the issue via the Java or Spring generator, then inspect generated Company.java and confirm it compiles. A linked pull request #25761 is already open, so check that before starting; done means multi-type properties map to a generated type or Object without dangling imports.

由索引模型根据 Issue 内容生成。

描述

Issue: Bug
Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

In an OpenAPI 3.1 spec, a property declared with multiple non-null types (type: [array, string], i.e. "a list of strings or a single string") is generated as AnyOfstring<String>. No AnyOfstring class is generated, so the output does not compile:

import org.example.model.AnyOfstring;   // class does not exist
...
private AnyOfstring<String> tradingNames = new ArrayList<>();

During generation the normalizer logs:

[ERROR] Type array not yet supported in openapi-normalizer to process OpenAPI 3.1 spec with multiple types.
[ERROR] Please report the issue via https://github.com/OpenAPITools/openapi-generator/issues/new/.

Expected: compilable code. Since the property can be either a JSON array or a string, a reasonable mapping would be Object (any type), as is done for schemas without a type. An alternative would be a proper anyOf wrapper that is actually generated.

Actual: a reference to a non-existent generic type AnyOfstring<String>, initialized with new ArrayList<>().

The issue shows up with a real vendor spec (a published OpenAPI 3.1 API description we cannot modify), where fields like this are common. A related case with the same root cause: type: [object, array] logs Type object not yet supported in openapi-normalizer ... and is generated as Map<String, Object>. That compiles, but deserialization fails at runtime when the payload is an array.

openapi-generator version

7.27.0 (latest release).

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Multi-type property repro
  version: 1.0.0
paths:
  /companies/{id}:
    get:
      operationId: getCompany
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Company'
components:
  schemas:
    Company:
      type: object
      properties:
        name:
          type: string
        tradingNames:
          description: A list for most countries, a single string for some.
          type:
            - array
            - string
          items:
            type: string
Generation Details

Maven plugin (org.openapitools:openapi-generator-maven-plugin:7.27.0), spec validation enabled (it passes), default normalizer settings:

<execution>
  <id>spring</id>
  <goals><goal>generate</goal></goals>
  <configuration>
    <inputSpec>${project.basedir}/multi-type.yaml</inputSpec>
    <generatorName>spring</generatorName>
    <library>spring-http-interface</library>
    <output>${project.build.directory}/spring</output>
    <modelPackage>org.example.model</modelPackage>
    <apiPackage>org.example.api</apiPackage>
    <configOptions><interfaceOnly>true</interfaceOnly></configOptions>
  </configuration>
</execution>
<execution>
  <id>java</id>
  <goals><goal>generate</goal></goals>
  <configuration>
    <inputSpec>${project.basedir}/multi-type.yaml</inputSpec>
    <generatorName>java</generatorName>
    <library>native</library>
    <output>${project.build.directory}/java</output>
    <modelPackage>org.example.model</modelPackage>
    <apiPackage>org.example.api</apiPackage>
  </configuration>
</execution>
Steps to reproduce
  1. Save the spec above as multi-type.yaml next to a pom.xml containing the plugin executions above.
  2. Run mvn generate-sources.
  3. Look at the normalizer errors in the log.
  4. Open target/spring/.../model/Company.java or target/java/.../model/Company.java: the tradingNames field is AnyOfstring<String> and import org.example.model.AnyOfstring; is present, but no AnyOfstring.java is generated. Compiling the generated sources fails with cannot find symbol: class AnyOfstring.
Related issues/PRs

No exact duplicate found.

Suggest a fix

In OpenAPINormalizer, the code path that logs Type ... not yet supported in openapi-normalizer to process OpenAPI 3.1 spec with multiple types leaves the schema as is, and later the composed-name logic produces AnyOf<type> without generating the class. Possible fixes:

  • when a 3.1 type array contains more than one non-null type, fall back to an any-type schema (generated as Object); or
  • convert it to an equivalent anyOf ([{type: array, items: ...}, {type: string}]) and go through the regular composed-schema handling.

Either way, the generator should never reference a model class it does not generate.

主要语言
Java
星标
26.9k
派生
7.8k
平均合并
2 天 23 分钟
30 天内合并 PR
177

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

OpenAPITools/openapi-generator 的其他 Issue

查看 OpenAPITools/openapi-generator 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。