我创建了这个 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 解析器会处理不带引号的 yes
、y
、no
、n
、on
和关闭
as boolean values .
要避免这种情况,请在 OpenAPI 文件中的 no
两边添加引号:
'no': # <---
type: string
xml:
name: 'NO' # <---
关于java - OpenAPI 生成器将 "no"转换为 "false",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75773899/