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 oneof Classes are not generated

See original GitHub issue
Description

(Thanks for fantastic tool!)

I expect OneOfGeographicLocationGeographicLocationRef.java be created.

Actual: no such file is generated

openapi-generator version

Tried with

  • 4.3.1
  • openapi-generator-cli-5.0.0-20200813.162255-527.jar
OpenAPI declaration file content or url

Swagger.json.txt

Generation Details
openapi-generator  generate -I Swagger.json.txt -g jaxrs-cxf-client 
Steps to reproduce

Run above command, you’ll get src/gen/java/org/openapitools/model/GeographicLocation.java which contains

import org.openapitools.model.OneOfGeographicLocationGeographicLocationRef;

But no such class was generated.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:10
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sarumontcommented, Nov 18, 2020

@seybi87 I just got back to this today and need a compiling JAR. I think this will work (about to test it, and I’ll report back here):

package com.devsupply.dana.model;

import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * OrgAllOf
 */
public class OneOfUserApiKey {
  private User user;
  private ApiKey apiKey;

  public void setUser(User user) {
    user(user);
  }

  public void setApiKey(ApiKey apiKey) {
    apiKey(apiKey);
  }

  public OneOfUserApiKey user(User user) {
    this.user = user;
    this.apiKey = null;
    return this;
  }

  public OneOfUserApiKey apiKey(ApiKey apiKey) {
    this.user = null;
    this.apiKey = apiKey;
    return this;
  }

  @javax.annotation.Nullable
  @JsonProperty("apiKey")
  public ApiKey getApiKey() {
    return this.apiKey;
  }

  @javax.annotation.Nullable
  @JsonProperty("user")
  public User getUser() {
    return this.user;
  }

  /**
   * Return true if this Org_allOf object is equal to o.
   */
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    OneOfUserApiKey other = (OneOfUserApiKey) o;
    return Objects.equals(this.user, other.user) &&
        Objects.equals(this.apiKey, other.apiKey);
  }

  @Override
  public int hashCode() {
    return Objects.hash(user, apiKey);
  }


  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class OneOfUserApiKey {\n");
    sb.append("    user: ").append(toIndentedString(user)).append("\n");
    sb.append("    apiKey: ").append(toIndentedString(apiKey)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }

}

Deserialization would have non-deterministic behavior if both apiKey and user were present in the JSON document, though. Not sure how to handle that for a proper PR here. I suppose a JsonDeserializer would have access to the entire tree at the time of deserialization, so it could throw. Thoughts on that part @wing328 ?

0reactions
wing328commented, Feb 23, 2022

java (library: jersey2, native, okhttp-gson) has better support of oneOf and anyOf.

We welcome contributions to plot the implementation to other java clients (jaxrs-cxf-client)

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG][Java] OneOf-Class not generated #10880 - GitHub
A workaround seems to be to rewrite the spec file to introduce an explicit wrapper class. The class can use oneOf successfully. For...
Read more >
OpenAPI models not being generated when using oneOf
I have oneOf generation working for java using openapi-generator 5.4.0 but I do add a discriminator field (like requestType: type: string enum: ...
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 >
Documentation for the spring Generator
Generates a Java SpringBoot Server application using the SpringDoc integration. #CONFIG OPTIONS. These options may be applied as additional- ...
Read more >
Swagger OpenAPI Code Gen "oneOf" : How to generate...
TLDR: Swagger offers a feature named 'oneOf'. The resulting java classes created do not seem to be correct. Details: I am creating the ......
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