OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] incline+oneOf schema create field type onto itself
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug 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?
Description
When declaring a oneOf field with inline schema; the inline generated type is based onto title
If a schema with this name is already existing/defined it is ignored and then finally it produce the final class coming from the oneOf declaration to reference itself
The following warning is present: Inline schema created as Field_Type_1. To have complete control of the model name, set the title field or use the modelNameMapping option (e.g. --model-name-mappings Field_Type_1=NewModel,ModelA=NewModelA in CLI) or inlineSchemaNameMapping option (--inline-schema-name-mappings Field_Type_1=NewModel,ModelA=NewModelA in CLI).
So seems to an expected behavior but it conduct onto the current issue.
openapi-generator version
Tested with 7.3.0 and current master
OpenAPI declaration file content
This is a curated sample: the allOf and oneOf are more complex but required with full OAS content
openapi: 3.0.3
info:
description: POC about oneOf auto generated schema issue
title: oneOf POC
version: "1.0"
license:
name: MIT
url: "https://foo.org"
contact:
email: support@foo.org
name: support at foo dot org
paths:
'/foo':
get:
tags:
- foo
summary: Get a foo
description: foo
operationId: foo_GetFoo
responses:
"200":
$ref: "#/components/responses/getFooResponse"
tags:
- description: foo
name: foo
components:
responses:
getFooResponse:
description: A successful response.
content:
application/json:
schema:
$ref: "#/components/schemas/FooResponse"
schemas:
FooResponse:
description: foo
type: object
title: Foo API
properties:
response_field:
description: an array of fields
type: array
items:
$ref: "#/components/schemas/ResponseField"
ResponseField:
type: object
description: foo
allOf:
- $ref: "#/components/schemas/FieldTypes"
FieldTypes:
type: object
oneOf:
- type: object
title: Field Type 1
properties:
my_field:
$ref: "#/components/schemas/FieldType1"
FieldType1:
description: Field Type 1 description
type: object
title: Field Type 1
properties:
property1:
description: property 1 for field type 1
type: string
Command line used for generation
java -cp modules/openapi-generator-cli/target/openapi-generator-cli.jar org.openapitools.codegen.OpenAPIGenerator generate -g java -i oneOfField-poc.yaml -o /tmp/openapi-generator/generated
Steps to reproduce
Execute the above command
Related issues/PRs
Generate file
The generated FieldType1 class is declaring myField variable of type FieldType1 (so it own class)
public class FieldType1 {
...
private FieldType1 myField;
...
}
FieldType1 class should contains the explicitly defined schema (the last schema from sample above) with the property1 field of type string
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Java generator command against the provided oneOfField-poc.yaml sample and inspect the generated FieldType1 class. Trace how the inline oneOf schema and the existing FieldType1 schema are named and resolved. Done means the generated myField no longer references FieldType1 itself and the class contains property1 as a string.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100