Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net/goai): fix openapi miss required tag of BizRequest when set CommonRequest #3724

Merged

Conversation

niluan304
Copy link
Contributor

@niluan304 niluan304 commented Aug 10, 2024

Fixes #3605


Anthor Question

When I was debugging, I found that tags such as v: "min",v: "max" were not rendered in the openapi.json
Is this as expected?

Example

  • Request & Response

    type CreateReq struct {
        g.Meta `path:"/Schema" tags:"schema" method:"POST" summary:"Create"`
    
        Option   string `dc:"option field"`
        Required string `v:"required" dc:"required field"`
        Min      int    `v:"min:1" d:"100" dc:"minimum value"`
        Max      int    `v:"max:10" d:"-1" dc:"maximum value"`
    }
    type CreateRes struct {
        Option   string `dc:"option field"`
        Required string `v:"required" dc:"required field"`
        Min      int    `v:"min:1" d:"200" dc:"minimum value"`
        Max      int    `v:"max:10" d:"-2" dc:"maximum value"`
    }
  • Swagger UI
    图片

Demo Code
package main

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

func main() {
	s := g.Server()
	s.Use(ghttp.MiddlewareHandlerResponse)

	s.BindObject("/v1", &Controller{})
	s.BindHandler("/status/:status", func(r *ghttp.Request) {
		r.Response.Write("Error status ", r.Get("status"), " found")
	})
	s.BindStatusHandler(404, func(r *ghttp.Request) {
		r.SetError(nil)
		r.Response.RedirectTo("status/404")
	})

	// TODO support Min, Max, MinLength, MaxLength

	// Custom enhance API document.
	enhanceOpenAPIDoc(s)
	s.SetOpenApiPath("/api.json")
	s.SetSwaggerPath("/swagger")

	s.SetPort(8999)
	// Just run the server.
	s.Run()
}

func enhanceOpenAPIDoc(s *ghttp.Server) {
	openapi := s.GetOpenApi()

	openapi.Config.CommonRequest = CommonReq{}
	openapi.Config.CommonResponse = ghttp.DefaultHandlerResponse{}
	openapi.Config.CommonResponseDataField = `Data`
}

type CommonReq struct {
	CommonOption   string `dc:"option field in common"`
	CommonRequired string `v:"required" dc:"required field in common"`
	CommonMin      int    `v:"min:-1" d:"88" dc:"minimum value in common"`
	CommonMax      int    `v:"max:10" d:"-8" dc:"maximum value in common"`
}

type Controller struct{}

type CreateReq struct {
	g.Meta `path:"/Schema" tags:"schema" method:"POST" summary:"Create"`

	Option   string `dc:"option field"`
	Required string `v:"required" dc:"required field"`
	Min      int    `v:"min:1" d:"100" dc:"minimum value"`
	Max      int    `v:"max:10" d:"-1" dc:"maximum value"`
}
type CreateRes struct {
	Option   string `dc:"option field"`
	Required string `v:"required" dc:"required field"`
	Min      int    `v:"min:1" d:"200" dc:"minimum value"`
	Max      int    `v:"max:10" d:"-2" dc:"maximum value"`
}

func (c *Controller) Create(ctx context.Context, req *CreateReq) (res *CreateRes, err error) {
	return nil, nil
}

type DeleteReq struct {
	g.Meta `path:"/Schema" tags:"schema" method:"DELETE" summary:"Delete"`

	Option   string `dc:"option field"`
	Required string `v:"required" dc:"required field"`
	Min      int    `v:"min:1" d:"300" dc:"minimum value"`
	Max      int    `v:"max:10" d:"-3" dc:"maximum value"`
}
type DeleteRes struct {
	Option   string `dc:"option field"`
	Required string `v:"required" dc:"required field"`
	Min      int    `v:"min:1" d:"400" dc:"minimum value"`
	Max      int    `v:"max:10" d:"-4" dc:"maximum value"`
}

func (c *Controller) Delete(ctx context.Context, req *DeleteReq) (res *DeleteRes, err error) {
	return nil, nil
}

type UpdateReq struct {
	g.Meta `path:"/Schema" tags:"schema" method:"PUT" summary:"Update"`

	Option   string `dc:"option field"`
	Required string `v:"required" dc:"required field"`
	Min      int    `v:"min:1" d:"500" dc:"minimum value"`
	Max      int    `v:"max:10" d:"-5" dc:"maximum value"`
}

type UpdateRes struct {
	Option   string `dc:"option field"`
	Required string `v:"required" dc:"required field"`
	Min      int    `v:"min:1" d:"600" dc:"minimum value"`
	Max      int    `v:"max:10" d:"-6" dc:"maximum value"`
}

func (c *Controller) Update(ctx context.Context, req *UpdateReq) (res *UpdateRes, err error) {
	return nil, nil
}

type GetReq struct {
	g.Meta `path:"/Schema" tags:"schema" method:"get" summary:"Get"`

	Option   string `dc:"option field"`
	Required string `v:"required" dc:"required field"`
	Min      int    `v:"min:1" d:"700" dc:"minimum value"`
	Max      int    `v:"max:10" d:"-7" dc:"maximum value"`
}
type GetRes struct {
	Option   string `dc:"option field"`
	Required string `v:"required" dc:"required field"`
	Min      int    `v:"min:1" d:"800" dc:"minimum value"`
	Max      int    `v:"max:10" d:"-8" dc:"maximum value"`
}

func (c *Controller) Get(ctx context.Context, req *GetReq) (res *GetRes, err error) {
	return nil, nil
}

fix openapi missing `required` tag of `BizRequest` when enable `CommonRequest`
Copy link

sonarcloud bot commented Aug 10, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
9.4% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

@gqcn gqcn merged commit 6b3fb60 into gogf:master Sep 10, 2024
23 of 24 checks passed
wln32 added a commit to wln32/gf that referenced this pull request Sep 12, 2024
* feat(database/gdb): add `time` field type for value converting for/from field (gogf#3712)

* fix(net/goai): fix openapi miss `required` tag of `BizRequest` when set `CommonRequest` (gogf#3724)

* perf(database/gdb): performance improvement for struct scanning when with feature disabled (gogf#3677)

* up

---------

Co-authored-by: CyJaySong <29367599+cyjaysong@users.noreply.github.com>
Co-authored-by: Zwei <zwei.elen@outlook.com>
Co-authored-by: wln32 <49137144+wln32@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

net/ghttp: openapi doc misses request parameter constraints when CommonRequest is set.
2 participants