在使用Groovy';谁是YAMLBuilder?

在使用Groovy';谁是YAMLBuilder?,groovy,yaml,yamlbuilder,Groovy,Yaml,Yamlbuilder,我正在使用Groovy在YAML中生成openapi规范文档。我正在使用YamlBuilder将对象模型转换为YAML字符串 到目前为止,它工作得很好,但我注意到的一个问题是YAML输出中存在null属性。这会在我使用的openapi验证程序中导致验证错误,因此我想从YAML输出中删除任何空属性 有没有办法做到这一点?我在文件里看不到。等效的JSONBuilder允许设置配置选项,YamlBuilder有这样的功能吗 脚本中生成YAML的部分如下所示: def generateSpec() {

我正在使用Groovy在YAML中生成openapi规范文档。我正在使用YamlBuilder将对象模型转换为YAML字符串

到目前为止,它工作得很好,但我注意到的一个问题是YAML输出中存在null属性。这会在我使用的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()
}
这是我当前的输出。请注意
firstname
format
属性的
null
值以及其他内容

---
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"

您应该显示生成此YAML的代码,以便我们知道您在做什么。我不明白为什么他们不能和YAML合作。是的,很公平。我现在添加了它。在将customerSpec传递到yaml之前,使用递归方法删除customerSpec中的所有空值(…),我一直在尝试实现这种方法。阻碍我的一件事是,我对YAML文档的各个部分使用了类结构。我不仅仅使用普通的地图和列表。我不知道如何从对象中删除空属性。另一个答案上的示例“denull”方法仅适用于地图和列表。您应该显示生成此YAML的代码,以便我们知道您在做什么。我不明白为什么他们不能和YAML合作。是的,很公平。我现在添加了它。在将customerSpec传递到yaml之前,使用递归方法删除customerSpec中的所有空值(…),我一直在尝试实现这种方法。阻碍我的一件事是,我对YAML文档的各个部分使用了类结构。我不仅仅使用普通的地图和列表。我不知道如何从对象中删除空属性。另一个答案上的示例“denull”方法仅适用于地图和列表。