swagger-api / swagger-api/swagger-codegen

[Java] Codegen for definition with no properties leads to invalid Java

Open
#4,804 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Swagger-Parser
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The swagger file is generated from an external model. For whatever reason (not too important), a definition has no properties. The Java class for this definition is not generated, but references to that class are generated in the model code, leading to compilation errors.

Swagger-codegen version

λ java -jar swagger-codegen\snapshot\swagger-codegen-cli-2.2.2-SNAPSHOT.jar version
2.2.2-SNAPSHOT

NOTE:
Also replicates with 2.2.1

Swagger declaration file content or url

Below see an edited version of the swagger file, removed all paths and not required definitions

{
  "swagger": "2.0",
  "info": {
    "version": "v2",
    "title": "StayinFront.WebAPI V2"
  },
  "host": "localhost",
  "basePath": "/revapi",
  "schemes": [
    "http"
  ],
  "paths": {},
  "definitions": {
    "CHT_ChannelWF_PutData": {
      "description": "CHT_ChannelWF_PutData",
      "properties": {}
    },
    "CHT_Channel_PutData": {
      "description": "CHT_Channel_PutData",
      "properties": {
        "WF": {
          "$ref": "#/definitions/CHT_ChannelWF_PutData"
        }
      }
    }
  }
}

This generates invalid Java:

...

package io.swagger.client.model;

import java.util.Objects;
import com.google.gson.annotations.SerializedName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.client.model.CHTChannelWFPutData;


/**
 * CHT_Channel_PutData
 */
@ApiModel(description = "CHT_Channel_PutData")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2017-02-16T09:36:18.569+13:00")
public class CHTChannelPutData   {
  @SerializedName("WF")
  private CHTChannelWFPutData WF = null;

  public CHTChannelPutData WF(CHTChannelWFPutData WF) {
    this.WF = WF;
    return this;
  }

   /**
   * Get WF
   * @return WF
  **/
  @ApiModelProperty(example = "null", value = "")
  public CHTChannelWFPutData getWF() {
    return WF;
  }

  public void setWF(CHTChannelWFPutData WF) {
    this.WF = WF;
  }

...
}

The actual class CHTChannelWFPutData is not generated so the above is nonsense. I would expect that CHTChannelWFPutData was generated.

Command line used for generation

java -jar swagger-codegen\snapshot\swagger-codegen-cli-2.2.1.jar generate -i swagger-file.json -l java -o output-dir

Steps to reproduce
  1. generate Java source using the above command line
  2. attempt to compile
Related issues

I couldn't find any related

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Java code-generation command with the reduced Swagger JSON and inspect the Java model-generation entry point for definitions with empty properties. Add coverage for the empty definition and verify that the referenced Java class is generated and the resulting model sources compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.