Skip to content

Commit

Permalink
feat(openapi): 添加 AnyOfSchema 等方法
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Nov 27, 2024
1 parent 6758bf0 commit 56ce646
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
46 changes: 45 additions & 1 deletion openapi/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Schema struct {
type properties = orderedmap.OrderedMap[string, *renderer[schemaRenderer]]

type schemaRenderer struct {
Type string `json:"type" yaml:"type"`
Type string `json:"type,omitempty" yaml:"type,omitempty"` // AnyOf 等不为空,此值可为空
XML *XML `json:"xml,omitempty" yaml:"xml,omitempty"`
ExternalDocs *externalDocsRenderer `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Expand Down Expand Up @@ -93,6 +93,50 @@ func NewSchema(v any, title, desc web.LocaleStringer) *Schema {
return newSchema(nil, v, title, desc)
}

func AnyOfSchema(title, desc web.LocaleStringer, v ...any) *Schema {
return xOfSchema(0, title, desc, v...)
}

func OneOfSchema(title, desc web.LocaleStringer, v ...any) *Schema {
return xOfSchema(1, title, desc, v...)
}

func AllOfSchema(title, desc web.LocaleStringer, v ...any) *Schema {
return xOfSchema(2, title, desc, v...)
}

// - 0 anyof
// - 1 oneof
// - 2 allof
func xOfSchema(typ int, title, desc web.LocaleStringer, v ...any) *Schema {
if len(v) == 0 {
panic("参数 v 必不可少")
}

of := make([]*Schema, 0, len(v))
for _, vv := range v {
of = append(of, NewSchema(vv, nil, nil))
}

s := &Schema{
Title: title,
Description: desc,
}

switch typ {
case 0:
s.AnyOf = of
case 1:
s.OneOf = of
case 2:
s.AllOf = of
default:
panic("无效的参数 typ")
}

return s
}

func newSchema(d *Document, v any, title, desc web.LocaleStringer) *Schema {
s := &Schema{
Title: title,
Expand Down
29 changes: 29 additions & 0 deletions openapi/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ type schemaObject2 struct {
schemaObject1
}

func TestOfSchema(t *testing.T) {
a := assert.New(t, false)

a.PanicString(func() {
AllOfSchema(nil, nil)
}, "参数 v 必不可少")

s := AnyOfSchema(web.Phrase("lang"), nil, "1", 0)
a.Length(s.AnyOf, 2).
Empty(s.Type).
Equal(s.AnyOf[0].Type, TypeString).
Equal(s.AnyOf[0].Default, "1").
Equal(s.AnyOf[1].Type, TypeInteger).
Nil(s.AnyOf[1].Default)

s = OneOfSchema(web.Phrase("lang"), nil, true, uint(2))
a.Length(s.OneOf, 2).
Empty(s.Type).
Equal(s.OneOf[0].Type, TypeBoolean).
Equal(s.OneOf[0].Default, true).
Equal(s.OneOf[1].Type, TypeInteger)

s = AllOfSchema(web.Phrase("lang"), nil, "1", 2)
a.Length(s.AllOf, 2).
Empty(s.Type).
Equal(s.AllOf[0].Type, TypeString).
Equal(s.AllOf[1].Type, TypeInteger)
}

func TestDocument_newSchema(t *testing.T) {
a := assert.New(t, false)
ss := newServer(a)
Expand Down
2 changes: 1 addition & 1 deletion openapi/valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (s *Schema) valid(skipRefNotNil bool) *web.FieldError {
return nil
}

if s.Type == "" {
if s.Type == "" && len(s.AnyOf) == 0 && len(s.AllOf) == 0 && len(s.OneOf) == 0 {
return web.NewFieldError("Type", "不能为空")
}

Expand Down

0 comments on commit 56ce646

Please sign in to comment.