Skip to content

Commit

Permalink
refactor(openapi): 修正以下问题
Browse files Browse the repository at this point in the history
- 添加 Schema.isBasicType 用以判断是否为基本类型;
- 删除 Document.Handler 中的无用代码;
- 合并 Operation.QueryObject 与 NewSchema 中的相似代码;
- Operation.*Ref 添加对是否存在的判断;
  • Loading branch information
caixw committed Nov 22, 2024
1 parent 8f6d52c commit 373a66d
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 140 deletions.
60 changes: 30 additions & 30 deletions openapi/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func newComponents() *components {
}
}

func (t *Parameter) addComponents(c *components, in string) {
func (t *Parameter) addToComponents(c *components, in string) {
if t.Ref == nil {
return
}
Expand All @@ -72,136 +72,136 @@ func (t *Parameter) addComponents(c *components, in string) {
}

if t.Schema != nil {
t.Schema.addComponents(c)
t.Schema.addToComponents(c)
}
}

func (s *Schema) addComponents(c *components) {
func (s *Schema) addToComponents(c *components) {
if s.Ref != nil {
if _, found := c.schemas[s.Ref.Ref]; !found {
c.schemas[s.Ref.Ref] = s
}
}

for _, item := range s.AllOf {
item.addComponents(c)
item.addToComponents(c)
}

for _, item := range s.OneOf {
item.addComponents(c)
item.addToComponents(c)
}

for _, item := range s.AnyOf {
item.addComponents(c)
item.addToComponents(c)
}

if s.Items != nil {
s.Items.addComponents(c)
s.Items.addToComponents(c)
}

for _, item := range s.Properties {
item.addComponents(c)
item.addToComponents(c)
}
}

func (resp *Callback) addComponents(c *components) {
func (resp *Callback) addToComponents(c *components) {
if resp.Ref != nil {
if _, found := c.callbacks[resp.Ref.Ref]; !found {
c.callbacks[resp.Ref.Ref] = resp
}
}

for _, item := range resp.Callback {
item.addComponents(c)
item.addToComponents(c)
}
}

// 所有带 $ref 的字段如果还未存在于 c,则会写入。
func (o *Operation) addComponents(c *components) {
func (o *Operation) addToComponents(c *components) {
for _, p := range o.Paths {
p.addComponents(c, InPath)
p.addToComponents(c, InPath)
}

for _, p := range o.Queries {
p.addComponents(c, InQuery)
p.addToComponents(c, InQuery)
}

for _, p := range o.Cookies {
p.addComponents(c, InCookie)
p.addToComponents(c, InCookie)
}

for _, p := range o.Headers {
p.addComponents(c, InHeader)
p.addToComponents(c, InHeader)
}

if o.RequestBody != nil {
o.RequestBody.addComponents(c)
o.RequestBody.addToComponents(c)
}

for _, r := range o.Responses {
r.addComponents(c)
r.addToComponents(c)
}

for _, r := range o.Callbacks {
r.addComponents(c)
r.addToComponents(c)
}
}

func (resp *Response) addComponents(c *components) {
func (resp *Response) addToComponents(c *components) {
if resp.Ref != nil {
if _, found := c.responses[resp.Ref.Ref]; !found {
c.responses[resp.Ref.Ref] = resp
}
}

for _, h := range resp.Headers {
h.addComponents(c, InHeader)
h.addToComponents(c, InHeader)
}

if resp.Body != nil {
resp.Body.addComponents(c)
resp.Body.addToComponents(c)
}

for _, s := range resp.Content {
s.addComponents(c)
s.addToComponents(c)
}
}

func (req *Request) addComponents(c *components) {
func (req *Request) addToComponents(c *components) {
if req.Ref != nil {
if _, found := c.requests[req.Ref.Ref]; !found {
c.requests[req.Ref.Ref] = req
}
}

if req.Body != nil {
req.Body.addComponents(c)
req.Body.addToComponents(c)
}

if len(req.Content) > 0 {
for _, s := range req.Content {
s.addComponents(c)
s.addToComponents(c)
}
}
}

func (item *PathItem) addComponents(c *components) {
func (item *PathItem) addToComponents(c *components) {
if item.Ref != nil {
if _, found := c.pathItems[item.Ref.Ref]; !found {
c.pathItems[item.Ref.Ref] = item
}
}

for _, p := range item.Paths {
p.addComponents(c, InPath)
p.addToComponents(c, InPath)
}
for _, p := range item.Queries {
p.addComponents(c, InQuery)
p.addToComponents(c, InQuery)
}
for _, p := range item.Headers {
p.addComponents(c, InHeader)
p.addToComponents(c, InHeader)
}
for _, p := range item.Cookies {
p.addComponents(c, InCookie)
p.addToComponents(c, InCookie)
}
}
20 changes: 10 additions & 10 deletions openapi/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func TestParameter_addComponents(t *testing.T) {
d := New(ss, web.Phrase("desc"))

p := &Parameter{}
p.addComponents(d.components, InPath)
p.addToComponents(d.components, InPath)
a.Empty(d.paths)

p = &Parameter{Schema: &Schema{Type: TypeString}}
p.addComponents(d.components, InPath)
p.addToComponents(d.components, InPath)
a.Empty(d.paths)

p = &Parameter{Schema: &Schema{Type: TypeString}, Ref: &Ref{Ref: "string"}}
p.addComponents(d.components, InPath)
p.addToComponents(d.components, InPath)
a.Equal(d.components.paths["string"], p)
}

Expand All @@ -36,28 +36,28 @@ func TestSchema_addComponents(t *testing.T) {
d := New(ss, web.Phrase("desc"))

s := &Schema{}
s.addComponents(d.components)
s.addToComponents(d.components)
a.Empty(d.components.schemas)

s = &Schema{Type: TypeString}
s.addComponents(d.components)
s.addToComponents(d.components)
a.Empty(d.components.schemas)

s1 := &Schema{Type: TypeString, Ref: &Ref{Ref: "t1"}}
s1.addComponents(d.components)
s1.addToComponents(d.components)
a.Length(d.components.schemas, 1).Equal(d.components.schemas["t1"], s1)

// 同名不会再添加
s2 := &Schema{Type: TypeString, Ref: &Ref{Ref: "t1"}}
s2.addComponents(d.components)
s2.addToComponents(d.components)
a.Length(d.components.schemas, 1).Equal(d.components.schemas["t1"], s1)

s2 = &Schema{Type: TypeString, Ref: &Ref{Ref: "t2"}, Items: s1}
s2.addComponents(d.components)
s2.addToComponents(d.components)
a.Length(d.components.schemas, 2).Equal(d.components.schemas["t2"], s2)

s3 := &Schema{Type: TypeString, Ref: &Ref{Ref: "t3"}, Items: &Schema{Type: TypeNumber, Ref: &Ref{Ref: "t4"}}}
s3.addComponents(d.components)
s3.addToComponents(d.components)
a.Length(d.components.schemas, 4).
Equal(d.components.schemas["t3"], s3).
Equal(d.components.schemas["t4"].Type, TypeNumber)
Expand All @@ -74,7 +74,7 @@ func TestPathItem_addComponents(t *testing.T) {
Headers: []*Parameter{{Name: "h1", Ref: &Ref{Ref: "h1"}}, {Name: "h2"}},
Cookies: []*Parameter{{Name: "c1", Ref: &Ref{Ref: "c1"}}, {Name: "c2"}},
}
item.addComponents(d.components)
item.addToComponents(d.components)
a.Length(d.components.paths, 1).
Length(d.components.cookies, 1).
Length(d.components.queries, 1).
Expand Down
Loading

0 comments on commit 373a66d

Please sign in to comment.