My problem is a little bit complicated, I'll try to explain it clearly. To do it, I've done a simple project.
I'm using Swagger codegen to generate Java classes from swagger file. In the swagger file, a definition is using additionnalProperties.
MyRequestBody:
type: object
properties:
property1:
type: string
property2:
type: string
additionalProperties:
type: object
The generated java class :
/*
* Chatbot api
* Api for chatbot interface.
*
* OpenAPI spec version: 1.0
*
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
package lu.post.models.api.test;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import javax.xml.bind.annotation.*;
/**
* MyRequestBody
*/
@javax.annotation.Generated(value = "lu.post.codegen.ApiplaylibGenerator")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@XmlRootElement(name="MyRequestBody")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyRequestBody extends HashMap<String, Object> {
@JsonProperty("property1")
@XmlElement(name="property1")
private String property1 = null;
@JsonProperty("property2")
@XmlElement(name="property2")
private String property2 = null;
public MyRequestBody property1(String property1) {
this.property1 = property1;
return this;
}
/**
* Get property1
* @return property1
**/
@ApiModelProperty(example = "null", value = "")
public String getProperty1() {
return property1;
}
public void setProperty1(String property1) {
this.property1 = property1;
}
public MyRequestBody property2(String property2) {
this.property2 = property2;
return this;
}
/**
* Get property2
* @return property2
**/
@ApiModelProperty(example = "null", value = "")
public String getProperty2() {
return property2;
}
public void setProperty2(String property2) {
this.property2 = property2;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || !(o instanceof MyRequestBody)) {
return false;
}
MyRequestBody myRequestBody = (MyRequestBody) o;
return Objects.equals(this.property1, myRequestBody.property1) &&
Objects.equals(this.property2, myRequestBody.property2) &&
super.equals(o);
}
@Override
public int hashCode() {
return Objects.hash(property1, property2, super.hashCode());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class MyRequestBody {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
sb.append(" property1: ").append(toIndentedString(property1)).append("\n");
sb.append(" property2: ").append(toIndentedString(property2)).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 ");
}
}
As you can see, the generated class extends HashMap for additionnalProperties.
At this stage, nothing shocking.
This class has been used in a play/java project, using play libraries to serialize/deserialize json and pojo.
I've create a simple route and controller to do a POST /test with the following body (which match with the swagger definition)
{
"property1": "p1",
"property2": "p2"
}
And my controller looks like :
public Result test() {
classLogger.debug("==================================");
classLogger.debug("START test()");
JsonNode bodyJsonNode = request().body().asJson();
MyRequestBody myRequestBody = Json.fromJson(bodyJsonNode, MyRequestBody.class);
classLogger.debug("myRequestBody : ");
classLogger.debug(myRequestBody.toString());
classLogger.debug("END test()");
classLogger.debug("==================================");
return ok();
}
And the logs show the problem :
2017-12-16 22:54:15,556[DEBUG][][][][ConversationController]==================================
2017-12-16 22:54:15,556[DEBUG][][][][ConversationController]START test()
2017-12-16 22:54:15,605[DEBUG][][][][ConversationController]myRequestBody :
2017-12-16 22:54:15,605[DEBUG][][][][ConversationController]class MyRequestBody {
{property2=p2, property1=p1}
property1: null
property2: null
}
2017-12-16 22:54:15,605[DEBUG][][][][ConversationController]END test()
2017-12-16 22:54:15,605[DEBUG][][][][ConversationController]==================================
The object fields "property1" and "property2" are null, because the field name and value are put in the Map key/value.
Does anybody know the best way to resolve this problem, knowing that : - I can't modify the swagger definition (because in my real projet, it is provided by another society). - I wish to continue to use the swagger codegen library.
Thanks in advance,
OBJECT_MAPPER.configOverride(MyClass.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.OBJECT));
– Feune