Skip to content

Commit

Permalink
feat: add openapi operation override unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleChair committed Oct 28, 2024
1 parent 95d401a commit 7db6e28
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions net/goai/goai_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,52 @@ func Test_Issue3747(t *testing.T) {
t.Assert(schema, Issue3747Res403Schema)
})
}

type Issue3889DefaultReq struct {
g.Meta `path:"/default" method:"post"`
Name string
}
type Issue3889DefaultRes struct{}

type Issue3889 struct{}

func (Issue3889) Default(ctx context.Context, req *Issue3889DefaultReq) (res *Issue3889DefaultRes, err error) {
res = &Issue3889DefaultRes{}
return
}

func OverrideOperation(operation *goai.Operation, reqObject interface{}, resObject interface{}) {
// Find some reqObject or resObject if needed.
// ...
operation.Summary = "Override summary"
operation.Responses["201"] = operation.Responses["200"]
}

// https://github.com/gogf/gf/issues/3889
func Test_Issue3889(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server(guid.S())
s.Use(ghttp.MiddlewareHandlerResponse)
s.Group("/", func(group *ghttp.RouterGroup) {
group.Bind(
new(Issue3889),
)
})
s.SetLogger(nil)
s.SetOpenApiPath("/api.json")
s.SetDumpRouterMap(false)
goai := s.GetOpenApi()
goai.Config.OperationOverrideHook = OverrideOperation
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)

c := g.Client()
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
apiContent := c.GetBytes(ctx, "/api.json")
j, err := gjson.LoadJson(apiContent)
t.AssertNil(err)
t.Assert(j.Get(`paths./default.post.summary`).String(), "Override summary")
t.AssertNE(j.Get(`paths./default.post.responses.201`).String(), "")
})
}

0 comments on commit 7db6e28

Please sign in to comment.