Skip to content

Commit

Permalink
Merge pull request #45 from go-andiamo/schema-format
Browse files Browse the repository at this point in the history
Add `Schema.Format`
  • Loading branch information
marrow16 authored Dec 2, 2023
2 parents 8fda997 + 76ac6b2 commit d3b30d6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
10 changes: 10 additions & 0 deletions additional.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions additional_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
10 changes: 7 additions & 3 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
},
}
Expand Down

0 comments on commit d3b30d6

Please sign in to comment.