Skip to content

Commit

Permalink
fix : body object
Browse files Browse the repository at this point in the history
  • Loading branch information
seipan committed Sep 18, 2023
1 parent ffd26cd commit bbee405
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
9 changes: 7 additions & 2 deletions lib/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ package lib

import "github.com/getkin/kin-openapi/openapi3"

type Body struct {
name string
shema *openapi3.SchemaRef
}

type Method struct {
method string
params openapi3.Parameters
bodys []*openapi3.SchemaRef
bodys []Body
}

func (m *Method) Method() string {
Expand All @@ -38,7 +43,7 @@ func (m *Method) Params() openapi3.Parameters {
return m.params
}

func (m *Method) Bodys() []*openapi3.SchemaRef {
func (m *Method) Bodys() []Body {
return m.bodys
}

Expand Down
10 changes: 7 additions & 3 deletions lib/parse_openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ func (o *OpenAPI) Parse(ctx context.Context) ([]Path, error) {

for mtd, opr := range oprs {
var params openapi3.Parameters
var bodys []*openapi3.SchemaRef
var bodys []Body

if opr.Parameters != nil {
params = append(params, opr.Parameters...)
}

if opr.RequestBody != nil {
for _, param := range opr.RequestBody.Value.Content["application/json"].Schema.Value.Properties {
bodys = append(bodys, param)
for name, param := range opr.RequestBody.Value.Content["application/json"].Schema.Value.Properties {
bodys = append(bodys,
Body{
name: name,
shema: param,
})
}
}

Expand Down
28 changes: 26 additions & 2 deletions lib/parse_openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestParseOpenAPI(t *testing.T) {
assert.Equal(t, "GET", res[0].method[0].method)
assert.Equal(t, "POST", res[0].method[1].method)
if res[0].method[1].bodys != nil {
by, _ := res[0].method[1].bodys[0].MarshalJSON()
fmt.Println(string(by))
by := res[1].method[1].bodys[1]
fmt.Println(by)
}

assert.Equal(t, "/pets/{id}", res[1].Path())
Expand All @@ -51,3 +51,27 @@ func TestParseOpenAPI(t *testing.T) {
assert.Equal(t, "GET", res[1].method[1].method)
})
}

func TestParseOpenAPIYml(t *testing.T) {
t.Run("ParseOpenAPI", func(t *testing.T) {
o := NewOpenAPI("../testdata/openapi.yaml")
res, err := o.Parse(context.Background())
assert.NoError(t, err)

assert.Equal(t, "/users/me", res[0].Path())

assert.Equal(t, "GET", res[0].method[0].method)

assert.Equal(t, "/users", res[1].Path())
assert.Equal(t, "GET", res[1].method[0].method)
assert.Equal(t, "POST", res[1].method[1].method)
if res[1].method[1].bodys != nil {
by := res[1].method[1].bodys[0]
b, _ := by.shema.MarshalJSON()
fmt.Println(string(b))
fmt.Println(by.name)
fmt.Println(by.shema.Value.Example)
}

})
}

0 comments on commit bbee405

Please sign in to comment.