github.com/wangyougui/gf/v2@v2.6.5/net/goai/goai_requestbody.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package goai
     8  
     9  import (
    10  	"reflect"
    11  
    12  	"github.com/wangyougui/gf/v2/internal/json"
    13  	"github.com/wangyougui/gf/v2/os/gstructs"
    14  	"github.com/wangyougui/gf/v2/text/gstr"
    15  )
    16  
    17  // RequestBody is specified by OpenAPI/Swagger 3.0 standard.
    18  type RequestBody struct {
    19  	Description string  `json:"description,omitempty"`
    20  	Required    bool    `json:"required,omitempty"`
    21  	Content     Content `json:"content,omitempty"`
    22  }
    23  
    24  type RequestBodyRef struct {
    25  	Ref   string
    26  	Value *RequestBody
    27  }
    28  
    29  func (r RequestBodyRef) MarshalJSON() ([]byte, error) {
    30  	if r.Ref != "" {
    31  		return formatRefToBytes(r.Ref), nil
    32  	}
    33  	return json.Marshal(r.Value)
    34  }
    35  
    36  type getRequestSchemaRefInput struct {
    37  	BusinessStructName string
    38  	RequestObject      interface{}
    39  	RequestDataField   string
    40  }
    41  
    42  func (oai *OpenApiV3) getRequestSchemaRef(in getRequestSchemaRefInput) (*SchemaRef, error) {
    43  	if oai.Config.CommonRequest == nil {
    44  		return &SchemaRef{
    45  			Ref: in.BusinessStructName,
    46  		}, nil
    47  	}
    48  
    49  	var (
    50  		dataFieldsPartsArray      = gstr.Split(in.RequestDataField, ".")
    51  		bizRequestStructSchemaRef = oai.Components.Schemas.Get(in.BusinessStructName)
    52  		schema, err               = oai.structToSchema(in.RequestObject)
    53  	)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	if in.RequestDataField == "" && bizRequestStructSchemaRef != nil {
    58  		// Normal request.
    59  		bizRequestStructSchemaRef.Value.Properties.Iterator(func(key string, ref SchemaRef) bool {
    60  			schema.Properties.Set(key, ref)
    61  			return true
    62  		})
    63  	} else {
    64  		// Common request.
    65  		structFields, _ := gstructs.Fields(gstructs.FieldsInput{
    66  			Pointer:         in.RequestObject,
    67  			RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag,
    68  		})
    69  		for _, structField := range structFields {
    70  			var fieldName = structField.Name()
    71  			if jsonName := structField.TagJsonName(); jsonName != "" {
    72  				fieldName = jsonName
    73  			}
    74  			switch len(dataFieldsPartsArray) {
    75  			case 1:
    76  				if structField.Name() == dataFieldsPartsArray[0] {
    77  					if err = oai.tagMapToSchema(structField.TagMap(), bizRequestStructSchemaRef.Value); err != nil {
    78  						return nil, err
    79  					}
    80  					schema.Properties.Set(fieldName, *bizRequestStructSchemaRef)
    81  					break
    82  				}
    83  			default:
    84  				if structField.Name() == dataFieldsPartsArray[0] {
    85  					var structFieldInstance = reflect.New(structField.Type().Type).Elem()
    86  					schemaRef, err := oai.getRequestSchemaRef(getRequestSchemaRefInput{
    87  						BusinessStructName: in.BusinessStructName,
    88  						RequestObject:      structFieldInstance,
    89  						RequestDataField:   gstr.Join(dataFieldsPartsArray[1:], "."),
    90  					})
    91  					if err != nil {
    92  						return nil, err
    93  					}
    94  					schema.Properties.Set(fieldName, *schemaRef)
    95  					break
    96  				}
    97  			}
    98  		}
    99  	}
   100  	return &SchemaRef{
   101  		Value: schema,
   102  	}, nil
   103  }