2

I'm using Groovy to generate an openapi spec document in YAML. I'm using YamlBuilder to convert the object model to a YAML string.

It's been working well so far, but one issue I've noticed is that null properties are present in the YAML output. This is causing validation errors in the openapi validators I'm using, so I'd like to remove any null properties from the YAML output.

Is there any way to achieve this? I can't see it in the docs. The equivalent JSONBuilder allows config options to be set, is there such a thing for YamlBuilder?

The part of the script which generates the YAML looks like this:

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()
}

Here's my current output. Note the null value of the format property on firstname, among others.

---
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"
Scottm
  • 7,004
  • 8
  • 32
  • 33
  • You should show the code generating this YAML so that we know what you're doing. I don't see why the [solution to exclude null properties with JSONBuilder](https://stackoverflow.com/q/14749817/347964) would not work with YAML. – flyx Dec 11 '20 at 12:25
  • Yup, fair enough. I've added it now. – Scottm Dec 11 '20 at 13:20
  • do a recursive method to drop all nulls in customerSpec before passing it into yaml(...) – daggett Dec 11 '20 at 14:09
  • I've been trying to implement such a method. One thing that is getting in the way is that I've used a class structure for the various parts of the YAML document. I've not just used plain maps and lists. I'm not seeing how to drop null properties from an object. The example 'denull' method on the other answer only works with Maps and Lists. – Scottm Dec 13 '20 at 19:39

0 Answers0