gf/protocol/goai/goai_requestbody.go

104 lines
2.9 KiB
Go
Raw Normal View History

2021-10-02 18:54:06 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2021-10-02 14:52:28 +08:00
package goai
import (
2021-11-13 23:34:16 +08:00
"reflect"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/os/gstructs"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/text/gstr"
2021-10-02 14:52:28 +08:00
)
// RequestBody is specified by OpenAPI/Swagger 3.0 standard.
type RequestBody struct {
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
Content Content `json:"content,omitempty" yaml:"content,omitempty"`
}
type RequestBodyRef struct {
Ref string
Value *RequestBody
}
func (r RequestBodyRef) MarshalJSON() ([]byte, error) {
if r.Ref != "" {
2021-10-02 18:54:06 +08:00
return formatRefToBytes(r.Ref), nil
2021-10-02 14:52:28 +08:00
}
return json.Marshal(r.Value)
}
type getRequestSchemaRefInput struct {
BusinessStructName string
RequestObject interface{}
RequestDataField string
}
func (oai *OpenApiV3) getRequestSchemaRef(in getRequestSchemaRefInput) (*SchemaRef, error) {
if oai.Config.CommonRequest == nil {
return &SchemaRef{
Ref: in.BusinessStructName,
}, nil
}
var (
dataFieldsPartsArray = gstr.Split(in.RequestDataField, ".")
bizRequestStructSchemaRef = oai.Components.Schemas.Get(in.BusinessStructName)
schema, err = oai.structToSchema(in.RequestObject)
)
if err != nil {
return nil, err
}
if in.RequestDataField == "" && bizRequestStructSchemaRef != nil {
2022-03-02 20:00:40 +08:00
// Normal request.
bizRequestStructSchemaRef.Value.Properties.Iterator(func(key string, ref SchemaRef) bool {
schema.Properties.Set(key, ref)
return true
})
} else {
2022-03-02 20:00:40 +08:00
// Common request.
structFields, _ := gstructs.Fields(gstructs.FieldsInput{
Pointer: in.RequestObject,
RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag,
})
for _, structField := range structFields {
2022-03-02 20:00:40 +08:00
var fieldName = structField.Name()
if jsonName := structField.TagJsonName(); jsonName != "" {
fieldName = jsonName
}
switch len(dataFieldsPartsArray) {
case 1:
if structField.Name() == dataFieldsPartsArray[0] {
2022-03-02 20:00:40 +08:00
if err = oai.tagMapToSchema(structField.TagMap(), bizRequestStructSchemaRef.Value); err != nil {
return nil, err
}
schema.Properties.Set(fieldName, *bizRequestStructSchemaRef)
break
}
default:
if structField.Name() == dataFieldsPartsArray[0] {
2022-03-02 20:00:40 +08:00
var structFieldInstance = reflect.New(structField.Type().Type).Elem()
schemaRef, err := oai.getRequestSchemaRef(getRequestSchemaRefInput{
BusinessStructName: in.BusinessStructName,
RequestObject: structFieldInstance,
RequestDataField: gstr.Join(dataFieldsPartsArray[1:], "."),
})
if err != nil {
return nil, err
}
schema.Properties.Set(fieldName, *schemaRef)
break
}
}
}
}
return &SchemaRef{
Value: schema,
}, nil
}