goai_requestbody.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. package goai
  7. import (
  8. "reflect"
  9. "github.com/gogf/gf/v2/internal/json"
  10. "github.com/gogf/gf/v2/os/gstructs"
  11. "github.com/gogf/gf/v2/text/gstr"
  12. )
  13. // RequestBody is specified by OpenAPI/Swagger 3.0 standard.
  14. type RequestBody struct {
  15. Description string `json:"description,omitempty"`
  16. Required bool `json:"required,omitempty"`
  17. Content Content `json:"content,omitempty"`
  18. }
  19. type RequestBodyRef struct {
  20. Ref string
  21. Value *RequestBody
  22. }
  23. func (r RequestBodyRef) MarshalJSON() ([]byte, error) {
  24. if r.Ref != "" {
  25. return formatRefToBytes(r.Ref), nil
  26. }
  27. return json.Marshal(r.Value)
  28. }
  29. type getRequestSchemaRefInput struct {
  30. BusinessStructName string
  31. RequestObject interface{}
  32. RequestDataField string
  33. }
  34. func (oai *OpenApiV3) getRequestSchemaRef(in getRequestSchemaRefInput) (*SchemaRef, error) {
  35. if oai.Config.CommonRequest == nil {
  36. return &SchemaRef{
  37. Ref: in.BusinessStructName,
  38. }, nil
  39. }
  40. var (
  41. dataFieldsPartsArray = gstr.Split(in.RequestDataField, ".")
  42. bizRequestStructSchemaRef = oai.Components.Schemas.Get(in.BusinessStructName)
  43. schema, err = oai.structToSchema(in.RequestObject)
  44. )
  45. if err != nil {
  46. return nil, err
  47. }
  48. if in.RequestDataField == "" && bizRequestStructSchemaRef != nil {
  49. // Normal request.
  50. bizRequestStructSchemaRef.Value.Properties.Iterator(func(key string, ref SchemaRef) bool {
  51. schema.Properties.Set(key, ref)
  52. return true
  53. })
  54. } else {
  55. // Common request.
  56. structFields, _ := gstructs.Fields(gstructs.FieldsInput{
  57. Pointer: in.RequestObject,
  58. RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag,
  59. })
  60. for _, structField := range structFields {
  61. var fieldName = structField.Name()
  62. if jsonName := structField.TagJsonName(); jsonName != "" {
  63. fieldName = jsonName
  64. }
  65. switch len(dataFieldsPartsArray) {
  66. case 1:
  67. if structField.Name() == dataFieldsPartsArray[0] {
  68. if err = oai.tagMapToSchema(structField.TagMap(), bizRequestStructSchemaRef.Value); err != nil {
  69. return nil, err
  70. }
  71. schema.Properties.Set(fieldName, *bizRequestStructSchemaRef)
  72. break
  73. }
  74. default:
  75. if structField.Name() == dataFieldsPartsArray[0] {
  76. var structFieldInstance = reflect.New(structField.Type().Type).Elem()
  77. schemaRef, err := oai.getRequestSchemaRef(getRequestSchemaRefInput{
  78. BusinessStructName: in.BusinessStructName,
  79. RequestObject: structFieldInstance,
  80. RequestDataField: gstr.Join(dataFieldsPartsArray[1:], "."),
  81. })
  82. if err != nil {
  83. return nil, err
  84. }
  85. schema.Properties.Set(fieldName, *schemaRef)
  86. break
  87. }
  88. }
  89. }
  90. }
  91. return &SchemaRef{
  92. Value: schema,
  93. }, nil
  94. }