[BUG][JAVA] Bug generating attribute with multiple types in OAS 3.1
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Save the spec above as
multi-type.yamlnext to apom.xmlcontaining the plugin executions above. - Run
mvn generate-sources. - Look at the normalizer errors in the log.
- Open
target/spring/.../model/Company.javaortarget/java/.../model/Company.java: thetradingNamesfield isAnyOfstring<String>andimport org.example.model.AnyOfstring;is present, but noAnyOfstring.javais generated. Compiling the generated sources fails withcannot 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
typearray contains more than one non-null type, fall back to an any-type schema (generated asObject); 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.
- Ngôn ngữ chính
- Java
- Star
- 26.9k
- Fork
- 7.8k
- Merge trung bình
- 2 ngày 23 phút
- Pull request đã merge (30 ngày)
- 177
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của OpenAPITools/openapi-generator
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
OpenAPITools/openapi-generator#25768 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Enhancement: Feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
OpenAPITools/openapi-generator#25753 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG][RUBY-NEXTGEN] A non-String value in a pattern-constrained attribute raises NoMethodErrorCó thể đã có người làm @Inf1mum đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
OpenAPITools/openapi-generator#25724 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG][RUBY-NEXTGEN] Response#to_hash is advertised and raises when the data is an ArrayCó thể đã có người làm @Inf1mum đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
OpenAPITools/openapi-generator#25723 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG][RUBY-NEXTGEN] A JSON string response raises ApiError because the body is parsed twiceCó thể đã có người làm @Inf1mum đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
OpenAPITools/openapi-generator#25719 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của OpenAPITools/openapi-generator
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
eclipse-tradista/tradista#371 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Failed to start Rescue missionĐang mởcritical
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
mars-sim/mars-sim#2199 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG-02] Endpoints /assistente/relatorio/*/preview retornam 404 ("Ver na tela" dos relatórios)Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
projeto-ong-amar/projeto-ong-amar-backend#26 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
primefaces/primefaces#15568 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
崩溃日志只按 CRLF 分行Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
PCL-Community/PCL-CE#3872 ·
Maintainer thường phản hồi trong vòng 1 ngày