Files
go-zero/tools/goctl/api/swagger/response.go

66 lines
1.5 KiB
Go
Raw Normal View History

2025-04-17 22:38:55 +08:00
package swagger
import (
2025-05-15 21:59:48 +08:00
"net/http"
2025-04-17 22:38:55 +08:00
"github.com/go-openapi/spec"
apiSpec "github.com/zeromicro/go-zero/tools/goctl/api/spec"
)
2025-05-15 21:59:48 +08:00
func jsonResponseFromType(ctx Context, atDoc apiSpec.AtDoc, tp apiSpec.Type) *spec.Responses {
if tp == nil {
return &spec.Responses{
ResponsesProps: spec.ResponsesProps{
StatusCodeResponses: map[int]spec.Response{
http.StatusOK: {
ResponseProps: spec.ResponseProps{
Description: "",
Schema: &spec.Schema{},
},
},
},
},
}
}
2025-04-17 22:38:55 +08:00
props := spec.SchemaProps{
2025-05-15 21:59:48 +08:00
AdditionalProperties: mapFromGoType(ctx, tp),
Items: itemsFromGoType(ctx, tp),
}
if ctx.UseDefinitions {
structName, ok := containsStruct(tp)
if ok {
props.Ref = spec.MustCreateRef(getRefName(structName))
return &spec.Responses{
ResponsesProps: spec.ResponsesProps{
StatusCodeResponses: map[int]spec.Response{
http.StatusOK: {
ResponseProps: spec.ResponseProps{
Schema: &spec.Schema{
SchemaProps: wrapCodeMsgProps(ctx, props, atDoc),
},
},
},
},
},
}
}
2025-04-17 22:38:55 +08:00
}
2025-05-15 21:59:48 +08:00
p, _ := propertiesFromType(ctx, tp)
props.Type = typeFromGoType(ctx, tp)
props.Properties = p
2025-04-17 22:38:55 +08:00
return &spec.Responses{
ResponsesProps: spec.ResponsesProps{
2025-05-15 21:59:48 +08:00
StatusCodeResponses: map[int]spec.Response{
http.StatusOK: {
ResponseProps: spec.ResponseProps{
Schema: &spec.Schema{
SchemaProps: wrapCodeMsgProps(ctx, props, atDoc),
},
2025-04-17 22:38:55 +08:00
},
},
},
},
}
}