github.com/gogf/gf/v2@v2.7.4/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/gogf/gf. 6 7 package goai 8 9 import ( 10 "reflect" 11 12 "github.com/gogf/gf/v2/internal/json" 13 "github.com/gogf/gf/v2/os/gstructs" 14 "github.com/gogf/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 58 if bizRequestStructSchemaRef == nil { 59 return &SchemaRef{ 60 Value: schema, 61 }, nil 62 } 63 64 if in.RequestDataField == "" && bizRequestStructSchemaRef.Value != nil { 65 // Append bizRequest. 66 schema.Required = append(schema.Required, bizRequestStructSchemaRef.Value.Required...) 67 68 // Normal request. 69 bizRequestStructSchemaRef.Value.Properties.Iterator(func(key string, ref SchemaRef) bool { 70 schema.Properties.Set(key, ref) 71 return true 72 }) 73 } else { 74 // Common request. 75 structFields, _ := gstructs.Fields(gstructs.FieldsInput{ 76 Pointer: in.RequestObject, 77 RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag, 78 }) 79 for _, structField := range structFields { 80 var fieldName = structField.Name() 81 if jsonName := structField.TagJsonName(); jsonName != "" { 82 fieldName = jsonName 83 } 84 switch len(dataFieldsPartsArray) { 85 case 1: 86 if structField.Name() == dataFieldsPartsArray[0] { 87 if err = oai.tagMapToSchema(structField.TagMap(), bizRequestStructSchemaRef.Value); err != nil { 88 return nil, err 89 } 90 schema.Properties.Set(fieldName, *bizRequestStructSchemaRef) 91 break 92 } 93 default: 94 if structField.Name() == dataFieldsPartsArray[0] { 95 var structFieldInstance = reflect.New(structField.Type().Type).Elem() 96 schemaRef, err := oai.getRequestSchemaRef(getRequestSchemaRefInput{ 97 BusinessStructName: in.BusinessStructName, 98 RequestObject: structFieldInstance, 99 RequestDataField: gstr.Join(dataFieldsPartsArray[1:], "."), 100 }) 101 if err != nil { 102 return nil, err 103 } 104 schema.Properties.Set(fieldName, *schemaRef) 105 break 106 } 107 } 108 } 109 } 110 return &SchemaRef{ 111 Value: schema, 112 }, nil 113 }