openapi 生成器包装元素不工作

问题描述 投票:0回答:0

这是我要映射到 pojo 类的 xml 响应结果:

<PASSENGERS>
    <ADULT>
        <NB>1</NB>
        <BASE_PRICE>12.23</BASE_PRICE>
        <TAX>2.19</TAX>
    </ADULT>
    <ADULT>
        <NB>1</NB>
        <BASE_PRICE>12.23</BASE_PRICE>
        <TAX>2.19</TAX>
    </ADULT>
    <NON_ADULTS>
        <NON_ADULT>
            <AGE_RANGE>2-12</AGE_RANGE>
            <NO>1</NO>
            <BASE_PRICE>12.23</BASE_PRICE>
            <TAX>2.19</TAX>
        </NON_ADULT>
        <NON_ADULT>
            <AGE_RANGE>2-12</AGE_RANGE>
            <NO>1</NO>
            <BASE_PRICE>12.23</BASE_PRICE>
            <TAX>2.19</TAX>
        </NON_ADULT>
    </NON_ADULTS>
</PASSENGERS>

我创建了这个 OpenAPI YAML 定义:

info:
  title: Passengers
  version: 1.0.0
servers:
  - url: http://localhost:8000
paths:
  /passengers:
    get:
      summary: Get passengers information
      responses:
        '200':
          description: OK
          content:
            application/xml:
              schema:
                $ref: '#/components/schemas/Passengers'
components:
  schemas:
    Passengers:
      type: object
      properties:
        adult:
          type: array
          xml:
            name: ADULT
          items:
            $ref: '#/components/schemas/Adult'
        nonAdult:
          type: array
          items:
            $ref: '#/components/schemas/NonAdult'
          xml:
            wrapped: true
            name: NON_ADULTS
      xml:
        name: PASSENGERS
    Adult:
      type: object
      properties:
        nb:
          type: integer
          xml:
            name: NB
        basePrice:
          type: number
          format: double
          xml:
            name: BASE_PRICE
        tax:
          type: number
          format: double
          xml:
            name: TAX
      xml:
        name: ADULT
    NonAdult:
      type: object
      properties:
        ageRange:
          type: string
          description: The age range of the non-adult
          xml:
            name: AGE_RANGE
        no:
          type: integer
          format: int32
          xml:
            name: 'NO'
        basePrice:
          type: number
          format: BigDecimal
          description: The base price for the non-adults
          xml:
            name: BASE_PRICE
        tax:
          type: number
          format: BigDecimal
          description: The tax for the non-adults
          xml:
            name: TAX
      xml:
        name: NON_ADULT

我的 pom 构建是:

<plugin>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <executions>
          <execution>
            <configuration>
              <configOptions>
                <additionalModelTypeAnnotations>
                  @lombok.Data
                  @lombok.NoArgsConstructor
                  @lombok.AllArgsConstructor
                  @lombok.Builder
                </additionalModelTypeAnnotations>
                <additionalProperty>jackson=false</additionalProperty>
                <dateLibrary>java8</dateLibrary>
                <interfaceOnly>true</interfaceOnly>
                <java8>false</java8>
                <modelPropertyNaming>original</modelPropertyNaming>
                <serializableModel>true</serializableModel>
                <skipDefaultInterface>true</skipDefaultInterface>
                <sourceFolder>src/main/java</sourceFolder>
                <useBeanValidation>true</useBeanValidation>
                <useTags>true</useTags>
                <withXml>true</withXml>
              </configOptions>
              <generateApiDocumentation>false</generateApiDocumentation>
              <generateApiTests>false</generateApiTests>
              <generateApis>false</generateApis>
              <generateModelTests>false</generateModelTests>
              <generateSupportingFiles>false</generateSupportingFiles>
              <generatorName>spring</generatorName>
              <importMappings>
              </importMappings>
              <inputSpec>package-Passenger.yaml</inputSpec>
              <library>spring-boot</library>
              <modelPackage>com.gqt.business.domains.packageQuote</modelPackage>
              <typeMappings/>
            </configuration>
            <goals>
              <goal>generate</goal>
            </goals>
            <id>domain-commons-classes</id>
            <phase>generate-sources</phase>
          </execution>
        </executions>
        <groupId>org.openapitools</groupId>
        <version>5.1.1</version>
      </plugin>
  1. 当我运行 mvn clean package 时,生成的类 Passenger 没有像我在 yaml 文件中定义的包装元素 NON_ADULTS。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.gqt.business.domains.packageQuote;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.validation.Valid;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@JacksonXmlRootElement(
  localName = "PASSENGERS"
)
@XmlRootElement(
  name = "PASSENGERS"
)
@XmlAccessorType(XmlAccessType.FIELD)
public class Passengers implements Serializable {
  private static final long serialVersionUID = 1L;
  @JsonProperty("adult")
  @JacksonXmlProperty(
    localName = "ADULT"
  )
  private @Valid List<Adult> adult = null;
  @JsonProperty("nonAdult")
  @JacksonXmlProperty(
    localName = "NON_ADULTS"
  )
  private @Valid List<NonAdult> nonAdult = null;

  public Passengers adult(List<Adult> adult) {
    this.adult = adult;
    return this;
  }

  public Passengers addAdultItem(Adult adultItem) {
    if (this.adult == null) {
      this.adult = new ArrayList();
    }

    this.adult.add(adultItem);
    return this;
  }

  @ApiModelProperty("")
  public @Valid List<Adult> getAdult() {
    return this.adult;
  }

  public void setAdult(List<Adult> adult) {
    this.adult = adult;
  }

  public Passengers nonAdult(List<NonAdult> nonAdult) {
    this.nonAdult = nonAdult;
    return this;
  }

  public Passengers addNonAdultItem(NonAdult nonAdultItem) {
    if (this.nonAdult == null) {
      this.nonAdult = new ArrayList();
    }

    this.nonAdult.add(nonAdultItem);
    return this;
  }

  @ApiModelProperty("")
  public @Valid List<NonAdult> getNonAdult() {
    return this.nonAdult;
  }

  public void setNonAdult(List<NonAdult> nonAdult) {
    this.nonAdult = nonAdult;
  }

  public boolean equals(Object o) {
    if (this == o) {
      return true;
    } else if (o != null && this.getClass() == o.getClass()) {
      Passengers passengers = (Passengers)o;
      return Objects.equals(this.adult, passengers.adult) && Objects.equals(this.nonAdult, passengers.nonAdult);
    } else {
      return false;
    }
  }

  public int hashCode() {
    return Objects.hash(new Object[]{this.adult, this.nonAdult});
  }

  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Passengers {\n");
    sb.append("    adult: ").append(this.toIndentedString(this.adult)).append("\n");
    sb.append("    nonAdult: ").append(this.toIndentedString(this.nonAdult)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  private String toIndentedString(Object o) {
    return o == null ? "null" : o.toString().replace("\n", "\n    ");
  }

  public static PassengersBuilder builder() {
    return new PassengersBuilder();
  }

  public Passengers() {
  }

  public Passengers(final List<Adult> adult, final List<NonAdult> nonAdult) {
    this.adult = adult;
    this.nonAdult = nonAdult;
  }

  public static class PassengersBuilder {
    private List<Adult> adult;
    private List<NonAdult> nonAdult;

    PassengersBuilder() {
    }

    @JsonProperty("adult")
    @JacksonXmlProperty(
      localName = "ADULT"
    )
    public PassengersBuilder adult(final List<Adult> adult) {
      this.adult = adult;
      return this;
    }

    @JsonProperty("nonAdult")
    @JacksonXmlProperty(
      localName = "NON_ADULTS"
    )
    public PassengersBuilder nonAdult(final List<NonAdult> nonAdult) {
      this.nonAdult = nonAdult;
      return this;
    }

    public Passengers build() {
      return new Passengers(this.adult, this.nonAdult);
    }

    public String toString() {
      return "Passengers.PassengersBuilder(adult=" + this.adult + ", nonAdult=" + this.nonAdult + ")";
    }
  }
}

  1. 我使用 javax.xml.bind.Marshaller 将 Passenger 对象写入 xml 字符串,子元素不像我定义的 xml 名称。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PASSENGERS>
    <adult>
        <nb>2</nb>
        <basePrice>1.0</basePrice>
    </adult>
    <adult>
        <nb>1</nb>
        <basePrice>0.0</basePrice>
    </adult>
    <nonAdult>
        <ageRange>2-12</ageRange>
        <no>1</no>
    </nonAdult>
    <nonAdult>
        <ageRange>2-12</ageRange>
        <no>1</no>
    </nonAdult>
</PASSENGERS>

你们能帮帮我吗?

我在谷歌上进行了研究,但仍然没有找到解决方案

java spring-boot jaxb openapi swagger-codegen
© www.soinside.com 2019 - 2024. All rights reserved.