【问题标题】:Openapi generator does not generate @XmlAttribute/@XmlElement annotationsOpenapi 生成器不生成@XmlAttribute/@XmlElement 注解
【发布时间】:2019-03-12 15:09:50
【问题描述】:

我现在正在摆弄 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

由于某种原因,我现在无法运行生成的代码,但似乎如果我将 <MyRequest name="foobar"> 发送到端点,它将无法使用该模型解析它。

我是否遗漏了一些配置选项或其他任何东西以生成正确的注释?

查看openapi的源码,需要的注解都在那里

【问题讨论】:

    标签: java spring-boot openapi openapi-generator


    【解决方案1】:

    我越来越清楚了:现在,OpenAPITools 的生成器以及它的父亲 SwaggerCodeGen 都将 json 作为主要目标格式。确实支持 XML,但更多的是作为一种选择,坦率地说非常糟糕。 我最近发现了 3 个错误:

    为了让它工作,我必须自定义各种mustache templates 以获得正确的 xml 注释。解决方法在第一个问题中描述。

    重要提示:还要确保withXml 选项已激活,以便mustache Pojo 模板生成所需的xml 注释。

    祝你好运。

    【讨论】:

    • 嗯,如果我查看源代码,我需要的注释就在那里(请参阅我的编辑)。由于某种原因,在渲染模板时会跳过字段上方的类头和注释
    • 您是否在(Maven 或 Gradle)配置中将“withXml”设置为 true ?
    • 不,我没有,...如果您想编辑原始答案或粘贴新答案以便我可以接受,现在可以使用,如果其他人监督这个:)
    • 很高兴它的工作!我已经更新了我原来的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-07-12
    • 2023-01-12
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2019-12-07
    • 2019-12-20
    • 2023-01-23
    相关资源
    最近更新 更多