use*_*649 5 playback jackson codegen swagger
我的问题有点复杂,我会尽量解释清楚。为此,我做了一个简单的项目。
我正在使用Swagger 代码生成器从 swagger 文件生成 Java 类。在 swagger 文件中,定义使用了 additionalnalProperties。
MyRequestBody:
type: object
properties:
property1:
type: string
property2:
type: string
additionalProperties:
type: object
Run Code Online (Sandbox Code Playgroud)
生成的java类:
/*
* 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 ");
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,生成的类扩展了 HashMap 的附加属性。
在这个阶段,没有什么令人震惊的。
这个类已经用在一个play/java项目中,使用play库对json和pojo进行序列化/反序列化。
我创建了一个简单的路由和控制器来使用以下主体(与 swagger 定义匹配)执行 POST /test
{
"property1": "p1",
"property2": "p2"
}
Run Code Online (Sandbox Code Playgroud)
我的控制器看起来像:
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();
}
Run Code Online (Sandbox Code Playgroud)
日志显示了问题:
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]==================================
Run Code Online (Sandbox Code Playgroud)
对象字段“property1”和“property2”为空,因为字段名和值都放在Map键/值中。
有没有人知道解决这个问题的最佳方法,知道: - 我不能修改 swagger 定义(因为在我的真实项目中,它是由另一个社会提供的)。- 我希望继续使用 swagger 代码生成库。
提前致谢,
我面临着另一个似乎与你的问题相关的问题。我已经设法在 API 定义中将属性和映射分开,它可能会帮助您,但生成的代码是错误的,因为尽管使用了 HashMap.java,但它仍然缺少 HashMap.java 的导入。
模式:
ReportRequest:
type: object
properties:
authentication:
$ref: '#/components/schemas/SessionAuthentication'
reportParameters:
$ref: '#/components/schemas/ReportParameters'
ReportParameters:
type: object
properties:
fromDate:
type: string
toDate:
type: string
required:
- fromDate
- toDate
additionalProperties: true
example:
fromDate: '2020-04-13'
toDate: '2022-04-13'
Run Code Online (Sandbox Code Playgroud)
请注意,缺少 java.util.HashMap 导入和键入错误。
类型不匹配:无法从 HashMap<String,Object> 转换为 ReportParameters
package io.swagger.model;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.model.ReportParameters;
import io.swagger.model.SessionAuthentication;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;
/**
* ReportRequest
*/
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2022-03-31T11:12:13.135Z[GMT]")
public class ReportRequest {
@JsonProperty("authentication")
private SessionAuthentication authentication = null;
@JsonProperty("reportParameters")
private ReportParameters reportParameters = new HashMap<String, Object>();
Run Code Online (Sandbox Code Playgroud)
有关 assionalProperties 的更多信息,请访问
| 归档时间: |
|
| 查看次数: |
1671 次 |
| 最近记录: |