swagger-api / swagger-api/swagger-codegen
[ALL] [3.0] Vars empty when additionalProperties used
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When defining a model with some explicitly defined properties and additionalProperties, ex.
Org:
type: "object"
required:
- "id"
- "name"
properties:
id:
type: "string"
name:
type: "string"
additionalProperties:
type: "object"
, all *vars properties in swagger-codegen are empty (as verified by -DdebugModels=true), ex.
{
"importPath" : "from swagger_client.models.org import Org",
"model" : {
"vendorExtensions" : {
"x-is-alias" : false,
"x-is-container" : true,
"x-is-map-container" : true
},
"parent" : "dict<String, object>",
"name" : "Org",
"classname" : "Org",
"classVarName" : "org",
"modelJson" : "{\n \"required\" : [ \"id\", \"name\" ],\n \"type\" : \"object\",\n \"properties\" : {\n \"id\" : {\n \"type\" : \"string\"\n },\n \"name\" : {\n \"type\" : \"string\"\n }\n },\n \"additionalProperties\" : {\n \"type\" : \"object\",\n \"properties\" : { }\n }\n}",
"classFilename" : "org",
"vars" : [ ],
"requiredVars" : [ ],
"optionalVars" : [ ],
"readOnlyVars" : [ ],
"readWriteVars" : [ ],
"allVars" : [ ],
"parentVars" : [ ],
"mandatory" : [ ],
"allMandatory" : [ ],
"imports" : [ ],
"emptyVars" : false,
"hasVars" : false,
"hasOnlyReadOnly" : false,
"hasMoreModels" : false,
"hasEnums" : false,
"hasRequired" : false,
"hasOptional" : false,
"hasChildren" : false,
"hasHeaders" : false,
"isString" : false,
"isNumeric" : false,
"isInteger" : false,
"isLong" : false,
"isNumber" : false,
"isFloat" : false,
"isDouble" : false,
"isByteArray" : false,
"isBoolean" : false,
"isDate" : false,
"isDateTime" : false,
"isUuid" : false,
"isDefault" : false,
"isMapContainer" : true,
"isListContainer" : false,
"isMultipart" : false,
"isResponseBinary" : false,
"isResponseFile" : false,
"isBinary" : false,
"isFile" : false,
"isEnum" : false,
"isArrayModel" : false,
"isAlias" : false,
"isContainer" : true,
"isNotContainer" : false,
"isReadOnly" : false,
"isCollectionFormatMulti" : false,
"hasMore" : false,
"isPrimitiveType" : false
}
}
This results in swagger-codegen generating incomplete classes, ex:
package io.swagger.client.model;
import java.util.Objects;
import java.util.HashMap;
import java.util.Map;
/**
* Org
*/
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2019-01-22T16:05:29.386-08:00[America/Los_Angeles]")public class Org extends HashMap<String, Object> {
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return super.equals(o);
}
@Override
public int hashCode() {
return java.util.Objects.hash(super.hashCode());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Org {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\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 ");
}
}
Compare to v2 where it generates a class that extends HashMap<String, Object> to accomodate but also has fields for explicitly defined properties.
Swagger-codegen version
3.0.4 and 3.0.5-SNAPSHOT
Swagger declaration file content or url
definitions:
Org:
type: "object"
required:
- "id"
- "name"
properties:
id:
type: "string"
name:
type: "string"
additionalProperties:
type: "object"
Command line used for generation
java -jar swagger-codegen-cli-3.0.4.jar generate -i /path/to/swagger.yaml -l java -o /var/tmp/java-api-client -DdebugModels=true
Steps to reproduce
See above.
Related issues/PRs
Suggest a fix/enhancement
I suggest populating *vars even when additionalProperties is enabled. This should resolve the issue as 2.0 already handled this case and the templates should already be able to handle them.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided Swagger YAML and run the Java generation command using swagger-codegen 3.0.4 and -DdebugModels=true. Trace how models with additionalProperties populate the *vars collections and compare the generated Java model with the stated v2 behavior. Done means explicitly defined properties appear alongside the HashMap behavior in the generated class.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100