OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
22.02k stars 6.61k forks source link

[BUG][Java] Empty looking object is generated for anyOf type #6485

Open H21lab opened 4 years ago

H21lab commented 4 years ago

Bug Report Checklist

Description

While generating the TS29573_N32_Handshake.yaml to create java client, the java class SecurityCapability is generated with no relevant methods and it looks empty (there are no method to be able to passing values inside). However in yaml file the object is defined in the following way:

    SecurityCapability:
      anyOf:
        - type: string
          enum:
            - TLS
            - PRINS
        - type: string
openapi-generator version

5.0.0-SNAPSHOT

OpenAPI declaration file content or url

https://github.com/jdegre/5GC_APIs/blob/master/TS29573_N32_Handshake.yaml

Command line used for generation

Generated by openapi-generator-maven-plugin by using following options:

    <plugins>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>5.0.0-SNAPSHOT</version>

                <configuration>
                    <generatorName>java</generatorName>
                    <output>${project.build.directory}/generated-sources</output>
                    <configOptions>
                    </configOptions>
               </configuration>

               <executions>
                   <execution>
                        <id>65</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>                      
                            <inputSpec>${project.basedir}/src/main/resources/TS29573_N32_Handshake.yaml</inputSpec>
                            <modelPackage>com.h21lab.TS29573_N32_Handshake.handler</modelPackage>
                            <apiPackage>com.h21lab.TS29573_N32_Handshake.model</apiPackage>
                            <invokerPackage>com.h21lab.TS29573_N32_Handshake.handler</invokerPackage>
                        </configuration>
                    </execution>
            </executions>
            </plugin>
        <plugins>
Steps to reproduce

Generator will produce the following class with no relevant methods or variables:

/*
 * N32 Handshake API
 * N32-c Handshake Service.  © 2020, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TSDSI, TTA, TTC).  All rights reserved. 
 *
 * The version of the OpenAPI document: 1.1.0.alpha-3
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

package com.h21lab.TS29573_N32_Handshake.handler;

import java.util.Objects;
import java.util.Arrays;

/**
 * SecurityCapability
 */
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-05-29T11:08:32.406367+02:00[Europe/Vienna]")
public class SecurityCapability {

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

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

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class SecurityCapability {\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    ");
  }

}
Related issues/PRs
Suggest a fix
wing328 commented 4 years ago

Please use the jersey2 library, which has better support for oneOf, anyOf.

H21lab commented 4 years ago

I have tested it by using

<generatorName>java</generatorName>
<library>jersey2</library>

After it was not possible to compile the project and I have to modify AbstractOpenApiSchema.java:

//package com.h21lab.TS29573_N32_Handshake.handler.model;
package com.h21lab.TS29573_N32_Handshake.handler;

And also the ApiClient.java:

//import com.h21lab.TS29573_N32_Handshake.handler.model.AbstractOpenApiSchema;
import com.h21lab.TS29573_N32_Handshake.handler.AbstractOpenApiSchema;

Then the client worked. (with jersey2, the difference is, there are no async method). Now there was method generated from anyOf type and possible to set value by securityCapability.setActualInstance("some string");

After also the response from the server was correctly processed and deserialized into SecNegotiateRspData.

black-snow commented 4 years ago

My issue might be related.

My schema has:

        fields:
          type: array
          items:
            anyOf:
              - $ref: '#/components/schemas/CircularField'
              - $ref: '#/components/schemas/Field'

And I both Field and CircularField get generated (java and resttemplate lib). But CircularField tries to import AnyOfCircularFieldField - but there is no such class.

Will https://github.com/OpenAPITools/openapi-generator/pull/7263 fix this?

oscar-besga-panel commented 6 days ago

I hate to report that this behaviour is also found in version7.9.0

I generate with

java -jar openapi-generator-cli-7.7.0.jar \
    generate \
    --input-spec TS29510_Nnrf_NFDiscovery.yaml \
    --generator-name jaxrs-jersey \
    --output temp/java \
    --config TS29510_Nnrf_NFDiscovery.json

With these options in the json file

{
    "apiPackage": "com.telefonica.iot.nrf.service.openapi.rel17.discovery.api",
    "modelPackage": "com.telefonica.iot.nrf.service.openapi.rel17.discovery.model",
    "dateLibrary":  "java8",
    "generateBuilders": true,
    "generateConstructorWithAllArgs": true
}

And this data definition in the specs

 NFType:
      description: NF types known to NRF
      anyOf:
        - type: string
          enum:
            - NRF
            - UDM
[...]
            - SMS_ROUTER
        - type: string

It generates a class without properties/internal_variables like this

The server launch an error if I try to run it

org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.telefonica.iot.nrf.service.openapi.rel17.management.api.NfInstancesApi.getNFInstances(com.telefonica.iot.nrf.service.openapi.rel17.management.model.NFType,java.lang.Integer