-
Notifications
You must be signed in to change notification settings - Fork 0
/
goai_requestbody.go
103 lines (94 loc) · 2.86 KB
/
goai_requestbody.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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.
package goai
import (
"reflect"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/os/gstructs"
"github.com/gogf/gf/v2/text/gstr"
)
// RequestBody is specified by OpenAPI/Swagger 3.0 standard.
type RequestBody struct {
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Content Content `json:"content,omitempty"`
}
type RequestBodyRef struct {
Ref string
Value *RequestBody
}
func (r RequestBodyRef) MarshalJSON() ([]byte, error) {
if r.Ref != "" {
return formatRefToBytes(r.Ref), nil
}
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 {
// Normal request.
bizRequestStructSchemaRef.Value.Properties.Iterator(func(key string, ref SchemaRef) bool {
schema.Properties.Set(key, ref)
return true
})
} else {
// Common request.
structFields, _ := gstructs.Fields(gstructs.FieldsInput{
Pointer: in.RequestObject,
RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag,
})
for _, structField := range structFields {
var fieldName = structField.Name()
if jsonName := structField.TagJsonName(); jsonName != "" {
fieldName = jsonName
}
switch len(dataFieldsPartsArray) {
case 1:
if structField.Name() == dataFieldsPartsArray[0] {
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] {
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
}