繁体   English   中英

Openapi 生成器不生成@XmlAttribute/@XmlElement 注解

[英]Openapi generator does not generate @XmlAttribute/@XmlElement annotations

我目前正在摆弄 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 属性。 生成的 class 看起来像这样:

/**
 * 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之上会有一个 @XmlRootElement。

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

我是否错过了一些配置选项或任何东西,所以它会生成正确的注释?

查看openapi的源代码所需的注释在那里

对我来说,事情变得越来越清晰:现在,OpenAPITools的生成器以及它的SwaggerCodeGen都将json作为主要目标格式。 Xml支持true,但更多情况下是可选的,坦率地说非常糟糕。 我最近发现了3个错误:

为了使其工作,我必须自定义各种胡须模板 ,以具有正确的xml注释。 解决方法在第一期中进行了描述。

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

祝好运。

您必须在正确的位置使用 configOption“withXML”。 它必须在“configOptions”中定义:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>openapi-generator-generate</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>some.yaml</inputSpec>
                <output>target/generated-sources</output>
                <generatorName>java</generatorName>
                <generateApis>true</generateApis>
                <generateApiTests>false</generateApiTests>
                <generateApiDocumentation>false</generateApiDocumentation>
                <generateModelTests>false</generateModelTests>
                <modelPackage>com.model</modelPackage>
                <apiPackage>com.client</apiPackage>
                <configOptions>
                    <sourceFolder>src/gen/java/main</sourceFolder>
                    <withXml>true</withXml>
                    <library>resttemplate</library>
                    <interfaceOnly>true</interfaceOnly>
                    <useBeanValidation>true</useBeanValidation>
<!--                    <dateLibrary>java8</dateLibrary>-->
                    <java8>true</java8>
                    <unhandledException>true</unhandledException>
                    <hideGenerationTimestamp>true</hideGenerationTimestamp>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM