我现在正在摆弄 openapi,并尝试创建一个使用 XML 文件的端点。然而,当使用 openapi 创建模型时,我习惯的所有 XML 注释似乎都丢失了。这是我正在使用的 openapi.yaml。
openapi: 3.0.1
info:
version: "1.1"
title: xml test
description: some xml test
servers:
- url: 'http://localhost/:8080'
paths:
'/test':
put:
operationId: testMethodNaming
requestBody:
content:
'application/xml':
schema:
$ref: '#/components/schemas/MyRequest'
responses:
'200':
description: 'OK'
components:
schemas:
MyRequest:
type: object
properties:
name:
type: string
xml:
attribute: true
MyRequest
模式是现在所讨论的问题。请注意,我将 name 属性声明为 XML 属性。生成的类如下所示:
/**
* MyRequest
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2019-03-12T15:32:37.070386+01:00[Europe/Berlin]")
public class MyRequest {
@JsonProperty("name")
private String name;
public MyRequest name(String name) {
this.name = name;
return this;
}
/**
* Get name
* @return name
*/
@ApiModelProperty(value = "")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MyRequest myRequest = (MyRequest) o;
return Objects.equals(this.name, myRequest.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class MyRequest {\n");
sb.append(" name: ").append(toIndentedString(name)).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 ");
}
}
我使用 spring-boot 生成器生成了它。我本来期望 @XmlAttribute
注释出现在名称字段上方。我还预计会有一个 @XmlRootElement
在类(class)中名列前茅。
由于某种原因,我现在无法运行生成的代码,但似乎我是否会发送 <MyRequest name="foobar">
到端点,它将无法使用该模型解析它。
我是否错过了一些配置选项或其他任何内容,以便它生成正确的注释?
查看openapi源码,需要的注解就有了
最佳答案
有些事情对我来说变得越来越清晰:现在,OpenAPITools 的生成器以及它的父亲 SwaggerCodeGen 都将 json 作为主要目标格式。 Xml 确实得到了支持,但更多的是作为一种选择,而且坦率地说,支持很糟糕。 我最近发现了 3 个错误:
- https://github.com/swagger-api/swagger-codegen/issues/9052
- https://github.com/OpenAPITools/openapi-generator/issues/2240
- https://github.com/OpenAPITools/openapi-generator/issues/2243
为了使其正常工作,我必须自定义各种 mustache templates为了有正确的xml注释。第一期中描述了解决方法。
重要提示:还要确保激活 withXml
选项,以便 Mustache Pojo 模板生成所需的 xml 注释。
祝你好运。
关于java - Openapi生成器不生成@XmlAttribute/@XmlElement注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55124811/