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 (
|
2022-03-29 20:31:00 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/json"
|
2022-03-29 20:31:00 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2021-10-02 14:52:28 +08:00
|
|
|
)
|
|
|
|
|
|
2024-10-21 21:16:45 +08:00
|
|
|
// StatusCode is http status for response.
|
|
|
|
|
type StatusCode = int
|
|
|
|
|
|
|
|
|
|
// ResponseStatusDef is used to enhance the documentation of the response.
|
|
|
|
|
// Normal response structure could implement this interface to provide more information.
|
|
|
|
|
type ResponseStatusDef interface {
|
|
|
|
|
ResponseStatusMap() map[StatusCode]any
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-02 14:52:28 +08:00
|
|
|
// Response is specified by OpenAPI/Swagger 3.0 standard.
|
|
|
|
|
type Response struct {
|
2022-03-31 15:42:12 +08:00
|
|
|
Description string `json:"description"`
|
|
|
|
|
Headers Headers `json:"headers,omitempty"`
|
|
|
|
|
Content Content `json:"content,omitempty"`
|
|
|
|
|
Links Links `json:"links,omitempty"`
|
|
|
|
|
XExtensions XExtensions `json:"-"`
|
2021-10-02 14:52:28 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-29 20:31:00 +08:00
|
|
|
func (oai *OpenApiV3) tagMapToResponse(tagMap map[string]string, response *Response) error {
|
2022-11-07 17:51:37 +08:00
|
|
|
var mergedTagMap = oai.fillMapWithShortTags(tagMap)
|
2022-03-29 20:31:00 +08:00
|
|
|
if err := gconv.Struct(mergedTagMap, response); err != nil {
|
|
|
|
|
return gerror.Wrap(err, `mapping struct tags to Response failed`)
|
2021-10-02 14:52:28 +08:00
|
|
|
}
|
2022-03-29 20:31:00 +08:00
|
|
|
oai.tagMapToXExtensions(mergedTagMap, response.XExtensions)
|
|
|
|
|
return nil
|
2021-10-11 17:10:55 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-29 20:31:00 +08:00
|
|
|
func (r Response) MarshalJSON() ([]byte, error) {
|
2021-10-11 17:10:55 +08:00
|
|
|
var (
|
2022-03-29 20:31:00 +08:00
|
|
|
b []byte
|
|
|
|
|
m map[string]json.RawMessage
|
|
|
|
|
err error
|
2021-10-11 17:10:55 +08:00
|
|
|
)
|
2022-03-29 20:31:00 +08:00
|
|
|
type tempResponse Response // To prevent JSON marshal recursion error.
|
|
|
|
|
if b, err = json.Marshal(tempResponse(r)); err != nil {
|
2021-10-11 17:10:55 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-03-29 20:31:00 +08:00
|
|
|
if err = json.Unmarshal(b, &m); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for k, v := range r.XExtensions {
|
|
|
|
|
if b, err = json.Marshal(v); err != nil {
|
|
|
|
|
return nil, err
|
2021-10-11 17:10:55 +08:00
|
|
|
}
|
2022-03-29 20:31:00 +08:00
|
|
|
m[k] = b
|
2021-10-11 17:10:55 +08:00
|
|
|
}
|
2022-03-29 20:31:00 +08:00
|
|
|
return json.Marshal(m)
|
2021-10-11 17:10:55 +08:00
|
|
|
}
|