add automatic generating for OpenApi specification for ghttp.Server routes

This commit is contained in:
John Guo
2021-10-02 22:34:39 +08:00
parent 198a6c8e33
commit 1e80dc71eb
11 changed files with 117 additions and 57 deletions

View File

@ -41,6 +41,7 @@ type ExternalDocs struct {
}
const (
HttpMethodAll = `ALL`
HttpMethodGet = `GET`
HttpMethodPut = `PUT`
HttpMethodPost = `POST`

View File

@ -92,25 +92,27 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
Responses: map[string]ResponseRef{},
}
)
// Path check.
if in.Path == "" {
in.Path = gmeta.Get(inputObject.Interface(), TagNamePath).String()
}
if in.Path == "" {
panic(gerror.NewCode(
return gerror.NewCode(
gcode.CodeMissingParameter,
`missing necessary path parameter "%s" for struct "%s"`,
`missing necessary path parameter "%s" for input struct "%s"`,
TagNamePath, inputStructTypeName,
))
)
}
// Method check.
if in.Method == "" {
in.Method = gmeta.Get(inputObject.Interface(), TagNameMethod).String()
}
if in.Method == "" {
panic(gerror.NewCode(
return gerror.NewCode(
gcode.CodeMissingParameter,
`missing necessary method parameter "%s" for struct "%s"`,
`missing necessary method parameter "%s" for input struct "%s"`,
TagNamePath, inputStructTypeName,
))
)
}
if err := oai.addSchema(inputObject.Interface(), outputObject.Interface()); err != nil {
@ -189,25 +191,38 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
// Assign to certain operation attribute.
switch gstr.ToUpper(in.Method) {
case HttpMethodGet:
// GET operations cannot have a requestBody.
operation.RequestBody.Value = nil
path.Get = &operation
case HttpMethodPut:
path.Put = &operation
case HttpMethodPost:
path.Post = &operation
case HttpMethodDelete:
// DELETE operations cannot have a requestBody.
operation.RequestBody.Value = nil
path.Delete = &operation
case HttpMethodConnect:
path.Connect = &operation
// Nothing to do for Connect.
case HttpMethodHead:
path.Head = &operation
case HttpMethodOptions:
path.Options = &operation
case HttpMethodPatch:
path.Patch = &operation
case HttpMethodTrace:
path.Trace = &operation
default:
panic(gerror.NewCode(gcode.CodeInvalidParameter, `invalid method "%s"`, in.Method))
return gerror.NewCodef(gcode.CodeInvalidParameter, `invalid method "%s"`, in.Method)
}
oai.Paths[in.Path] = path
return nil