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"
|
2021-11-24 16:17:50 +08:00
|
|
|
"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)
|
|
|
|
}
|
2021-10-11 17:10:55 +08:00
|
|
|
|
|
|
|
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 (
|
2022-03-07 19:46:05 +08:00
|
|
|
dataFieldsPartsArray = gstr.Split(in.RequestDataField, ".")
|
|
|
|
bizRequestStructSchemaRef = oai.Components.Schemas.Get(in.BusinessStructName)
|
|
|
|
schema, err = oai.structToSchema(in.RequestObject)
|
2021-10-11 17:10:55 +08:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-07 19:46:05 +08:00
|
|
|
if in.RequestDataField == "" && bizRequestStructSchemaRef != nil {
|
2022-03-02 20:00:40 +08:00
|
|
|
// Normal request.
|
2022-03-07 19:46:05 +08:00
|
|
|
bizRequestStructSchemaRef.Value.Properties.Iterator(func(key string, ref SchemaRef) bool {
|
|
|
|
schema.Properties.Set(key, ref)
|
|
|
|
return true
|
|
|
|
})
|
2021-10-11 17:10:55 +08:00
|
|
|
} else {
|
2022-03-02 20:00:40 +08:00
|
|
|
// Common request.
|
2021-11-24 16:17:50 +08:00
|
|
|
structFields, _ := gstructs.Fields(gstructs.FieldsInput{
|
2021-10-11 17:10:55 +08:00
|
|
|
Pointer: in.RequestObject,
|
2021-11-24 16:17:50 +08:00
|
|
|
RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag,
|
2021-10-11 17:10:55 +08:00
|
|
|
})
|
|
|
|
for _, structField := range structFields {
|
2022-03-02 20:00:40 +08:00
|
|
|
var fieldName = structField.Name()
|
2021-10-11 17:10:55 +08:00
|
|
|
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
|
|
|
|
}
|
2022-03-07 19:46:05 +08:00
|
|
|
schema.Properties.Set(fieldName, *bizRequestStructSchemaRef)
|
2021-10-11 17:10:55 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if structField.Name() == dataFieldsPartsArray[0] {
|
2022-03-02 20:00:40 +08:00
|
|
|
var structFieldInstance = reflect.New(structField.Type().Type).Elem()
|
2021-10-11 17:10:55 +08:00
|
|
|
schemaRef, err := oai.getRequestSchemaRef(getRequestSchemaRefInput{
|
|
|
|
BusinessStructName: in.BusinessStructName,
|
|
|
|
RequestObject: structFieldInstance,
|
|
|
|
RequestDataField: gstr.Join(dataFieldsPartsArray[1:], "."),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-07 19:46:05 +08:00
|
|
|
schema.Properties.Set(fieldName, *schemaRef)
|
2021-10-11 17:10:55 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &SchemaRef{
|
|
|
|
Value: schema,
|
|
|
|
}, nil
|
|
|
|
}
|