question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Java generator does not generate oneOf interface

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

2reactions
KirillKurdyukovcommented, Jul 7, 2022
0reactions
Raviraelcommented, Nov 24, 2022

Same issue on version 6.2.1 with rest-assured library. Does work correctly on 4.3.1, so huge downgrade is an option for anyone needing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use OpenAPI "oneOf" property with openapi-generator ...
Currently, openapi-generator doesn't support oneOf . This is a capability that had been newly introduced with OpenAPI v3 (FYI, only v2 and below...
Read more >
Java Generated Code | Protocol Buffers - Google Developers
The protocol buffer compiler generates a class called Foo , which implements the Message interface. The class is declared final ; no further...
Read more >
oneOf, anyOf, allOf, not - Swagger
OAS 3 This guide is for OpenAPI 3.0. oneOf, anyOf, allOf, not. OpenAPI 3.0 provides several keywords which you can use to combine...
Read more >
Open API Server Implementation Using OpenAPI Generator
Learn how to generate a Spring-based server stub from a YAML specification using the OpenAPI generator's Maven plugin.
Read more >
Roadmap - OpenAPI Generator
The sections are not intended to represent exclusive focus during ... oneOf, callbacks, etc; Consider opt-in telemetry about generators ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found