OpenAPITools / OpenAPITools/openapi-generator

[BUG] Java generator does not generate oneOf interface

Open
#11,715 8 comments 10 reactions 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

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?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

the java generator on openapi-generator-maven-plugin 5.4.0 doesn't compile with an openapi which contain an oneOf instructions.
The interfaces OneOf are not generated.

Compilation failure: Compilation failure:
[ERROR] /Users/XXX/git/service/contract/target/generated-sources/openapi/src/main/java/blabla/model/Person.java:[48,23] error: cannot find symbol
[ERROR]   symbol:   class OneOfCatDog
[ERROR]   location: class Person

I tried with previous versions and the only one which compile is the 4.3.1.
Unfortunately the generated interface contain an empty JsonSubTypes annotation : the mapper cannot map correctly the object

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2022-02-25T12:39:36.701392+01:00[Europe/Paris]")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "pet_type", visible = true)
@JsonSubTypes({
})

public interface PersonPetOneOf  {
    public String getPetType();
}
openapi-generator version

version : 5.4.0

OpenAPI declaration file content or url
openapi: "3.0.3"
info:
  title: "notification"
  version: "1.0"
tags:
  - name: notify
servers:
  - url: /person
paths:
  /person:
    post:
      security:
        - basicAuth: []  
      tags:
        - notify
      operationId: aaa
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Person'
      responses:
        '200':
          description : "retour ok"
         
components:
  securitySchemes:
    basicAuth:    
      type: http
      scheme: basic
  schemas:
    Pet:
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
    Dog:    
      allOf:  
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            bark:
              type: boolean
            breed:
              type: string
              enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:     
      allOf: 
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            hunts:
              type: boolean
            age:
              type: integer
    Person:
      properties:
        name:
          type : string
        pet:
          oneOf:
             - $ref: '#/components/schemas/Cat'
             - $ref: '#/components/schemas/Dog'
          discriminator:
            propertyName: pet_type
Generation Details

java : version 11
maven : version 3.5.3
pom.xml

<plugin>
	<groupId>org.openapitools</groupId>
	<artifactId>openapi-generator-maven-plugin</artifactId>
	<version>5.4.0</version>
	<!-- <version>4.3.1</version> -->
	<executions>
		<execution>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<inputSpec>${project.basedir}/src/main/resources/static/openapi/notifier-v1.yaml</inputSpec>
				<generatorName>java</generatorName>
				<apiPackage>blabla.api</apiPackage>
				<modelPackage>blabla.model</modelPackage>
				<library>resttemplate</library>
				<configOptions>
					<generateModels>true</generateModels>
					<dateLibrary>java8</dateLibrary>
				</configOptions>
			</configuration>
		</execution>
	</executions>
</plugin>
Steps to reproduce

mvn clean install

Related issues/PRs
Suggest a fix

The java generator should generate the OneOf Interface and the JsonSubTypes annotation should contains the object mapping.

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 with the pom.xml configuration and supplied OpenAPI declaration, then run mvn clean install to reproduce the generated-source failure. Inspect target/generated-sources/openapi/src/main/java/blabla/model/Person.java and the generated PersonPetOneOf interface. Done means the Java generator produces the interface, includes the object mappings in JsonSubTypes, and the generated project compiles.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.