[Java] Codegen for definition with no properties leads to invalid Java
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
- generate Java source using the above command line
- attempt to compile
Related issues
I couldn't find any related
Seems kind of important why you would make a model with no properties... I'm not sure why the codegen should support nonsensical input? Is there a use case that we can take into consideration?
@fehguy I don't know why this particular model has no properties. I didn't make it or have control over that bit. The swagger file is being generated from external model data, of which this particular class is currently empty. I can't think of a valid use-case for it, except that it means that I can't generate client code if this circumstance arises. Do you think it is valid to omit empty models from the generated code? I can omit it from the swagger file if that is the case.
OK fair enough--I can look.
I am facing the same problem. In my case, I have no control over the swagger file and the code is full of missing imports due to empty classes.
A model may be just a simple data type.
...
"MyString": {
"description": "An alias to string to document usecase of this model",
"type": "string"
},
...
This is a valid according to spec and I think generator should support it. I face this problem in generated python client.
My solution is to have a model class extending the "type" or "Object" if there is no type.
I believe I am facing this same problem with the cpprest target. I have a type: object defined in the schema with no properties. This is a valid schema and the no properties is by design. The #include "Object.h" and std::shared_ptr<Object> m_Metadata is generated in the enclosing type's header file. However, there is no Object.h file generated. I would expect the generator to generate a std::map<std::string, std::string>.
Is there a workaround for this issue?