简体   繁体   English

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

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

I am fiddling around with openapi at the moment and trying to create an endpoint that consumes a XML file.我目前正在摆弄 openapi 并尝试创建一个使用 XML 文件的端点。 When creating the models with openapi however it seems all the XML annotations that I'm used to are missing.然而,当使用 openapi 创建模型时,我习惯的所有 XML 注释似乎都丢失了。 This is the openapi.yaml I'm using.这是我正在使用的 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

The MyRequest schema is the thing in question now. MyRequest模式是现在有问题的东西。 Note that I declare the name property as a XML attribute.请注意,我将 name 属性声明为 XML 属性。 The generated class looks looks like this:生成的 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    ");
  }
}

I generated this using the spring-boot generator.我使用 spring-boot 生成器生成了这个。 I would have expected a @XmlAttribute annotation to be present above the name field.我本来希望在名称字段上方出现@XmlAttribute注释。 I would also have expected that there would be a @XmlRootElement on top of the class.我还期望在@XmlRootElement之上会有一个 @XmlRootElement。

For some reason I cannot run the generated code right now, but it seems if I would send <MyRequest name="foobar"> to the endpoint it would not be able to parse it with that model.由于某种原因,我现在无法运行生成的代码,但似乎如果我将<MyRequest name="foobar">发送到端点,它将无法用 model 解析它。

Did I miss some configuration option or anything so it generates the correct annotations?我是否错过了一些配置选项或任何东西,所以它会生成正确的注释?

Looking at the sourcecode of openapi the needed annotations are there查看openapi的源代码所需的注释在那里

Something is becoming more and more clear to me: right now, the generators of OpenAPITools , as well as it father SwaggerCodeGen, have json as primary target format. 对我来说,事情变得越来越清晰:现在,OpenAPITools的生成器以及它的SwaggerCodeGen都将json作为主要目标格式。 Xml is supported true, but more as an option and frankly quite badly. Xml支持true,但更多情况下是可选的,坦率地说非常糟糕。 I have recently discovered 3 bugs: 我最近发现了3个错误:

To make it work, I had to customize myself the various mustache templates in order to have correct xml annotations. 为了使其工作,我必须自定义各种胡须模板 ,以具有正确的xml注释。 The workaround is described in the first issue. 解决方法在第一期中进行了描述。

Important: also make sure the withXml option is activated, so that the mustache Pojo template will produce the required xml annotations. 重要提示:还请确保withXml选项已激活,以便withXml Pojo模板将生成所需的xml注释。

Good luck. 祝好运。

You have to use the configOption "withXML" at the right place.您必须在正确的位置使用 configOption“withXML”。 It must be defined within the "configOptions":它必须在“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