[BUG] [JAVA] oneOf generates incorrect model for primitive types
See original GitHub issueBug 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
Using oneOf
will generate a model with incorrect wrong types.
Actual output:
I have three files of models: ClientIp.java, Request,java, Response.java. Request.java has field private ClientIp clientIp = null;
with type ClientIp
Expected output:
I have only two files of models: Request,java, Response.java. Request.java has field private String clientIp;
with string type
openapi-generator version
openapi-generator-cli 5.2.1 commit : 8403e59 built : 2021-08-16T12:52:51Z source : https://github.com/openapitools/openapi-generator docs : https://openapi-generator.tech/
openapi-generator-cli 5.3.0-SNAPSHOT commit : de95e2a built : 2021-09-22T15:38:29+04:00 source : https://github.com/openapitools/openapi-generator docs : https://openapi-generator.tech/
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: java codegen BUG
description: oneOf generates incorrect model for primitive types
version: 1.0.0
paths:
/openapi/example/path:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Request'
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Response'
components:
schemas:
ClientIp:
oneOf:
- type: string
format: ipv4
- type: string
format: ipv6
nullable: false
example: '127.0.0.1' # or '2001:0db8:85a3:0000:0000:8a2e:0370:7334' for IPv6
Response:
properties:
result:
type: string
Request:
properties:
result:
type: string
client_ip:
$ref: "#/components/schemas/ClientIp"
Generation Details
java -jar openapi-generator-cli.jar generate -g java -i oneOfbug.yaml -o .\gen\
Steps to reproduce
Generate a java client using the above yaml.
Related issues/PRs
it little bit similar to https://github.com/OpenAPITools/openapi-generator/issues/4130
Suggest a fix
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top GitHub Comments
@wing328 I’ve just researched and known that it’s happened for libraries:
resttemplate
, defaultokhttp-gson
(like in bug).With
native
library andjersey2
all is OK. Abstract-class (AbstractOpenApiSchema) is generated with constructor of String type parameter. So it seems that problem is in generation code for specific libraries.ive tried both native and jersey2 and the oneof class is not generated. Is there a workaround?
Using something like so :