我正在使用 Groovy 在 YAML 中生成一个 openapi 规范文档。我正在使用 YamlBuilder 将对象模型转换为 YAML 字符串。
到目前为止它运行良好,但我注意到的一个问题是 YAML 输出中存在空属性。这导致我正在使用的 openapi 验证器出现验证错误,因此我想从 YAML 输出中删除任何空属性。
有什么办法可以做到这一点?我在文档中看不到它。等效的 JSONBuilder 允许设置配置选项,YamlBuilder 有这样的东西吗?
生成 YAML 的脚本部分如下所示:
def generateSpec() {
println "============================\nGenerating Customer SPI spec\n============================"
def components = generateComponents()
def paths = generatePaths()
def info = Info
.builder()
.title('Customer SPI')
.description('A customer API')
.version('0.1')
.build()
def customerSpec = [openapi: "3.0.3", components : components, info : info, paths : paths]
def yaml = new YamlBuilder()
yaml(customerSpec)
println(yaml.toString())
return yaml.toString()
}
这是我当前的输出。请注意上的属性null
值,等等。format
firstname
---
openapi: "3.0.3"
components:
schemas:
Customer:
type: "object"
properties:
firstname:
type: "string"
format: null
ArrayOfCustomers:
items:
$ref: "#/components/schemas/Customer"
info:
title: "Customer SPI"
version: "0.1"
description: "An API."
paths:
/customers:
parameters: null
get:
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/ArrayOfCustomers"
description: "An array of customers matching the search criteria"
summary: "Search customers"
/customers/{customerRef}:
parameters:
- required: true
schema:
type: "string"
format: null
description: "Customer reference"
in: "path"
name: "customerRef"
get:
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/Customer"
description: "A customer with the given reference"
summary: "Load a customer"