From 76ac6b2b468e2978ba47250f6f0f16a51685434f Mon Sep 17 00:00:00 2001 From: marrow16 Date: Sat, 2 Dec 2023 11:46:25 +0000 Subject: [PATCH] Add `Schema.Format` --- additional.go | 10 ++++++++++ additional_test.go | 29 +++++++++++++++++++++++++++++ schema.go | 10 +++++++--- schema_test.go | 9 +++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 additional_test.go diff --git a/additional.go b/additional.go index 121f46e..f8f5133 100644 --- a/additional.go +++ b/additional.go @@ -2,6 +2,16 @@ package chioas import "github.com/go-andiamo/chioas/yaml" +// AdditionalOasProperties is a type (map) of additional OAS properties that can be used for +// use on any .Additional property +type AdditionalOasProperties map[string]any + +func (ap AdditionalOasProperties) Write(on any, w yaml.Writer) { + for k, v := range ap { + w.WriteTagValue(k, v) + } +} + // Additional is an interface that can be supplied to many parts of the definition // to write additional yaml to the OAS type Additional interface { diff --git a/additional_test.go b/additional_test.go new file mode 100644 index 0000000..3c0ef79 --- /dev/null +++ b/additional_test.go @@ -0,0 +1,29 @@ +package chioas + +import ( + "github.com/go-andiamo/chioas/yaml" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestWriteAdditional(t *testing.T) { + ap := AdditionalOasProperties{"foo": "bar"} + w := yaml.NewWriter(nil) + writeAdditional(ap, nil, w) + data, err := w.Bytes() + assert.NoError(t, err) + const expect = `foo: bar +` + assert.Equal(t, expect, string(data)) +} + +func TestAdditionalOasProperties(t *testing.T) { + ap := AdditionalOasProperties{"foo": "bar"} + w := yaml.NewWriter(nil) + ap.Write(nil, w) + data, err := w.Bytes() + assert.NoError(t, err) + const expect = `foo: bar +` + assert.Equal(t, expect, string(data)) +} diff --git a/schema.go b/schema.go index 8a74ef4..afb4ef6 100644 --- a/schema.go +++ b/schema.go @@ -51,6 +51,8 @@ type Schema struct { // // Should be one of "string", "object", "array", "boolean", "integer", "number" or "null" Type string + // Format is the OAS format + Format string // RequiredProperties is the ordered collection of required properties // // If any of the items in Properties is also denoted as Property.Required, these are @@ -111,7 +113,8 @@ func (s *Schema) writeYaml(withName bool, w yaml.Writer) { writeSchemaRef(s.SchemaRef, s.Type == tagValueTypeArray, w) } else { w.WriteTagValue(tagNameDescription, s.Description). - WriteTagValue(tagNameType, defValue(s.Type, tagValueTypeObject)) + WriteTagValue(tagNameType, defValue(s.Type, tagValueTypeObject)). + WriteTagValue(tagNameFormat, s.Format) if s.Ofs != nil { s.Ofs.writeYaml(w) } @@ -150,8 +153,9 @@ func (s *Schema) writeYaml(withName bool, w yaml.Writer) { } func (s *Schema) writeOfYaml(w yaml.Writer) { - w.WriteItemStart(tagNameType, defValue(s.Type, tagValueTypeObject)). - WriteTagValue(tagNameDescription, s.Description) + w.WriteTagValue(tagNameDescription, s.Description). + WriteItemStart(tagNameType, defValue(s.Type, tagValueTypeObject)). + WriteTagValue(tagNameFormat, s.Format) if reqs, has := s.getRequiredProperties(); has { w.WriteTagStart(tagNameRequired) for _, rp := range reqs { diff --git a/schema_test.go b/schema_test.go index 1b3fde5..208af15 100644 --- a/schema_test.go +++ b/schema_test.go @@ -240,6 +240,15 @@ properties: oneOf: - $ref: "#/components/schemas/foo" - $ref: "#/components/schemas/bar" +`, + }, + { + schema: Schema{ + Type: "string", + Format: "uuid", + }, + expect: `type: string +format: uuid `, }, }