openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[jaxrs-resteasy/anyOf] code generator doesn't generate good java classes for types based on anyOf

Open tnmtechnologies opened this issue 8 years ago • 5 comments

Description

When a declared type is based on an anyOf, the generated java class owns no attribute, just default methods from Object class. It leads to an error at runtime because the json value can't be bound to its bean class by jaxrs framework.

anyOf declaration seems to not be supported by code generator while it is part of OpenApi 3 standard.

openapi-generator version

openapi-generator-cli-3.1.0.jar

OpenAPI declaration file content or url

The file TS29571_CommonData.yaml contains this declared defintion RatType

     RatType:
       anyOf:
       - type: string
         enum:
           - NR
           - EUTRA
           - WLAN
           - VIRTUAL
       - type: string

which is used in file TS29518_Namf_MT.yaml

Command line used for generation

We suppose the file openapi-generator-cli-3.1.0.jar is in the current folder and yaml files in a folder named yaml

rm -fr target/samples.openapi && java -jar ./openapi-generator-cli-3.1.0.jar generate -i yaml/TS29518_Namf_MT.yaml -g jaxrs-resteasy -o target/samples.openapi

Steps to reproduce

The issue has been found at runtime. It means we should need to build a war or jar file and put in its given runtime environment. These steps will take time. A quickest solution to look to the issue is to open the generated class file RatType.java which looks like this:

package org.openapitools.model;

import java.util.Objects;
import java.util.ArrayList;
import javax.validation.constraints.*;
import io.swagger.annotations.*;


@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaResteasyServerCodegen", date = "2018-07-13T15:42:47.770312700+02:00[Europe/Paris]")
public class RatType   {
  


  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    RatType ratType = (RatType) o;
    return true;
  }

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

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class RatType {\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(java.lang.Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}

As we can see there is no attribute in this generated class. Some pieces of code miss.

Related issues/PRs
Suggest a fix/enhancement

The fix should generates the source code related to the attributes defined in the OpenApi file. yaml.zip

tnmtechnologies avatar Jul 13 '18 14:07 tnmtechnologies

Hi,

Is there any update on this issue?

Also, I think it may be a duplicate / related to #798

Thanks

mudged avatar Jan 22 '19 08:01 mudged

This is a common problem in all swagger generators. Basically, if you're using OAS, design your API to provide consistent responses.

dskvr avatar Feb 08 '19 17:02 dskvr

This is a common problem in all swagger generators. Basically, if you're using OAS, design your API to provide consistent responses.

Unfortunately this is not always possible. In this case, the API is defined by the 3GPP standards body as part of the 5G core standards.

jphorx avatar Apr 09 '19 17:04 jphorx

Same problem still in the year 2023. Like @jphorx I am also using 3GPP standards, where I cannot update the Specification-YAML. But I face the same issue when having "anyOf" like that example in the issue.

Retested with v7.1.0

@wing328 please help us here

maxl2287 avatar Nov 23 '23 21:11 maxl2287

Okay it is working for my use-case with:


              <openapiNormalizer>
                SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING=true
              </openapiNormalizer>

maxl2287 avatar Nov 23 '23 21:11 maxl2287