java - OpenAPI 生成器将 "no"转换为 "false"

标签 java swagger openapi openapi-generator

我创建了这个 OpenAPI YAML 定义:

openapi: 3.0.3
info:
  title: NonAdult API
  version: 1.0.0
paths:
  /api/v1/components/schemas/NonAdult:
    description: 'Waiting for 3.1.0 spec to have "paths" optional.'
    get:
      tags:
        - schemas-controller
      operationId: getSchemaDefinition
      responses:
        "200":
          description: OK
          content:
            'application/xml':
              schema:
                $ref: '#/components/schemas/NonAdult'
components:
  schemas:
    NonAdult:
      type: object
      properties:
        ageRange:
          type: string
          description: The age range of the non-adult
          xml:
            name: AGE_RANGE
        no:
          type: string
          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
...

我使用 OpenAPI Generator Maven 插件从中生成 Spring 服务器。

我的 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>
                <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>NonAdult.yaml</inputSpec>
              <library>spring-boot</library>
              <modelPackage>com.demo.business.domains.Demo</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>

但是当我运行 mvn clean package 时,我收到有关“no”属性的错误。它在 Java 类中被转换为 false,如下所示:

@JsonProperty("ageRange")
  @JacksonXmlProperty(localName = "AGE_RANGE")
  private String ageRange;

  @JsonProperty("false")
  @JacksonXmlProperty(localName = "false")
  private String false;

我该如何解决这个问题?我不想更改属性的名称,因为我想使用 XML 响应映射数据。

最佳答案

某些 YAML 解析器会处理不带引号的 yesynonon关闭 as boolean values .

要避免这种情况,请在 OpenAPI 文件中的 no 两边添加引号:

        'no':   # <---
          type: string
          xml:
            name: 'NO'   # <---

关于java - OpenAPI 生成器将 "no"转换为 "false",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75773899/

相关文章:

FileUploadBean.uploadFile 中的 java.lang.NullPointerException

java - 如何使 Avro 模式中的所有字段都可以为空?

ANT jar 问题。 RT。在 Java/lib 中找不到 jar

yaml - 如何验证 openapi 3.0.0 yaml 规范?

java - 为 vertx 项目生成 OpenAPI 规范

java - 是否有办法创建一个 Spring Aspect 建议来触发任何未由某些注释注释的方法?

typescript - Swagger 的 bool 值作为字符串而不是 NestJS 中的 bool 值发送

自动将结构转到 OpenAPI 到 JSONSchema 生成

swagger - 为什么 swagger jbpm 文档最近大幅减少?

asp.net-core - 如何从 OpenAPI 3.0 架构生成 ASP.NET Core 2 Controller ?