carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.62k stars 136 forks source link

Generate OpenAPI Schema for Data Values Maps #457

Closed cari-lynn closed 2 years ago

cari-lynn commented 2 years ago

:green_circle: Scenario 1: Convert Inferred Schema with Map and String to JSONSchema

Given the following schema exists

#! data.yml
#@data/values
---
foo: bar

When I execute ytt tools generate schema --type jsonschema -f data.yml Then I should see on my screen

---
"$id": https://carvel.dev/ytt/schema.jsonschema.yml
"$schema": https://json-schema.org/draft/2020-12/schema
"@id": "ytt:data.yml"
type: object
additionalProperties: false
properties:
  foo:
    type: string
    default: bar

:green_circle: Scenario 2: Convert Inferred Schema with Map of Maps to JSONSchema

Given the following schema exists

#! data.yml
#@data/values
---
foo:
  int_key: 10
  bool_key: true
  null_key: null
  false_key: false
  string_key: some text
  inner_map:
    float_key: 9.1

When I execute ytt tools generate schema --type jsonschema -f data.yml Then I should see on my screen

---
"$id": https://carvel.dev/ytt/schema.jsonschema.yml
"$schema": https://json-schema.org/draft/2020-12/schema
"@id": "ytt:data.yml"
type: object
additionalProperties: false
properties:
  foo:
    type: object
    additionalProperties: false
    properties:
      int_key:
        type: integer
        default: 10
      bool_key:
        type: boolean
        default: true
      null_key:
        type: "null"
        default: null
      false_key:
        type: boolean
        default: false
      string_key:
        type: string
        default: some text
      inner_map:
        type: object
        additionalProperties: false
        properties:
          float_key:
            type: number
            default: 9.1

:red_circle: Scenario 3: Fails when no Data Value file provided

Given the following file exists

#! template.yml
---
foo: some value

When I execute ytt tools generate schema --type jsonschema -f template.yml Then I should see on my screen

Error: Unable to generate a schema from input files

Hint: Provide a file with a Schema or Data Values to generate the JSONSchema representation

note: updated error text to include data values.


OpenAPI spec

cari-lynn commented 2 years ago

I am closing this story in favor of n issue that combines generating a OpenAPI Schema from the merged Data Values Schema and Data Values files. So there is no need to separate the issues at this time.