swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.9k stars 6.03k forks source link

json array not generating proper model classes #5426

Open r-raghu opened 7 years ago

r-raghu commented 7 years ago
Description

I am trying to generate swagger api from the following json: https://watson-api-explorer.mybluemix.net/listings/conversation-v1.json

but it's not generating proper model classes, where ever it has following similar json:

 "CreateWorkspace": {
      "allOf": [
        {
          "$ref": "#/definitions/BaseEntity"
        }
      ],
      "x-java-builder": true
    },

Generated Model Class doesn't have reference to BaseEntity Class

import java.util.Objects;
import com.ibm.mfp.adapters.sample.model.BaseEntity;

/**
 * CreateWorkspace
 */
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2017-04-19T17:14:05.018+05:30")
public class CreateWorkspace   {

  @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 CreateWorkspace {\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    ");
  }
}

json looks correct only, as i am using watson json mentioned in above link.

Swagger-codegen version

2.2.2

Swagger declaration file content or url

https://watson-api-explorer.mybluemix.net/listings/conversation-v1.json

nbruno commented 7 years ago

The supplied JSON spec has the following listed for the CreateWorkspace model:

"CreateWorkspace": {
  "allOf": [
    {
      "$ref": "#/definitions/WorkspaceRequest"
    }
  ],
  "x-java-builder": true
}

It doesn't have a reference to BaseEntity. In my test, this generated the following Java class:

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2017-04-23T00:13:46.800-04:00")
public class CreateWorkspace {
  @SerializedName("name")
  private String name = null;

  @SerializedName("description")
  private String description = null;

  @SerializedName("language")
  private String language = null;

  @SerializedName("metadata")
  private Object metadata = null;

  @SerializedName("intents")
  private List<CreateIntent> intents = null;

  @SerializedName("entities")
  private List<CreateEntity> entities = null;

  @SerializedName("dialog_nodes")
  private List<CreateDialogNode> dialogNodes = null;

  @SerializedName("counterexamples")
  private List<CreateExample> counterexamples = null;

This is with the java language parameter. How are you invoking codegen? What language parameter are you specifying?

r-raghu commented 7 years ago

What i mentioned is just an example (Changed Workspace to BaseEntity to make json small). i tried mentioned json link https://watson-api-explorer.mybluemix.net/listings/conversation-v1.json, with following maven plugin:

  <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>exec1</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>conversation-v1.json</inputSpec>
                            <language>java</language>
                            <output>../watsonConversationclient/</output>
                            <apiPackage>com.test.sample.api</apiPackage>
                            <modelPackage>com.test.sample.model</modelPackage>
                            <invokerPackage>com.test.sample.sample</invokerPackage>
                            <configurationFile>Config.json</configurationFile>
                            <configOptions>
                                <sourceFolder>src/main/java</sourceFolder>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Config.json

{
  "additionalProperties": {
    "serviceFactoryClassname": "watsonConversationAdapterServiceFactory",
    "adapterApplicationClassname": "watsonConversationAdapterApplication"
  },
  "artifactVersion": "1.2.0"
}

And output is:

package com.test.sample.model;

import java.util.Objects;
import com.test.sample.model.CreateDialogNode;
import com.test.sample.model.CreateEntity;
import com.test.sample.model.CreateExample;
import com.test.sample.model.CreateIntent;
import com.test.sample.model.WorkspaceRequest;
import java.util.List;

/**
 * CreateWorkspace
 */
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2017-04-17T14:55:57.091+05:30")
public class CreateWorkspace   {

  @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 CreateWorkspace {\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    ");
  }
}

Which is not a correct model class.

nbruno commented 7 years ago

Your original post says you're using version swagger-codegen v2.2.2 but your Maven plugin is specifying 2.2.1. I just verified that there is a problem with this type of model in 2.2.1, but it appears to be fixed in 2.2.2. Can you update to 2.2.2 and try again?

r-raghu commented 7 years ago

I tested with v2.2.2 as well, it generate same model classes.

r-raghu commented 7 years ago

ahh.. looks like different model classes in with v2.2.2.