fix: goctl swagger missing security definition and submit json body data error (#4808)

This commit is contained in:
kesonan
2025-04-25 22:58:45 +08:00
committed by GitHub
parent 44735e949c
commit 812140ba36
8 changed files with 33 additions and 4 deletions

View File

@@ -7,6 +7,15 @@ import (
"google.golang.org/grpc/metadata"
)
func hasKey(properties map[string]string, key string) bool {
if len(properties) == 0 {
return false
}
md := metadata.New(properties)
_, ok := md[key]
return ok
}
func getBoolFromKVOrDefault(properties map[string]string, key string, def bool) bool {
if len(properties) == 0 {
return def

View File

@@ -3,13 +3,13 @@ package swagger
import (
"encoding/json"
"errors"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"gopkg.in/yaml.v2"
)

View File

@@ -29,4 +29,8 @@ const (
schemeHttps = "https"
defaultHost = "127.0.0.1"
defaultBasePath = "/"
swaggerSecurityDefinitionBearerAuth = "BearerAuth"
swaggerSecurityDefinitionName = "Authorization"
swaggerSecurityDefinitionIn = "header"
)

View File

@@ -43,6 +43,7 @@ type (
tags: "query 演示" // 对应 swagger 的 tags,可以对 swagger 中的 api 进行分组
summary: "query 类型接口集合" // 对应 swagger 的 summary
prefix: v1
jwt: Auth
)
service Swagger {
@doc (

View File

@@ -149,13 +149,13 @@ func parametersFromType(method string, tp apiSpec.Type) []spec.Parameter {
ExclusiveMaximum: exclusiveMaximum,
Minimum: minimum,
ExclusiveMinimum: exclusiveMinimum,
Enum: enumsValueFromOptions(jsonTag.Options),
Enum: enumsValueFromOptions(jsonTag.Options),
AdditionalProperties: mapFromGoType(member.Type),
},
}
switch sampleTypeFromGoType(member.Type) {
case swaggerTypeArray:
schema.Items=itemsFromGoType(member.Type)
schema.Items = itemsFromGoType(member.Type)
case swaggerTypeObject:
p, r := propertiesFromType(member.Type)
schema.Properties = p
@@ -168,6 +168,7 @@ func parametersFromType(method string, tp apiSpec.Type) []spec.Parameter {
resp = append(resp, spec.Parameter{
ParamProps: spec.ParamProps{
In: paramsInBody,
Name: paramsInBody,
Required: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{

View File

@@ -61,6 +61,15 @@ func mergePathItem(old, new spec.PathItem) spec.PathItem {
}
func spec2Path(info apiSpec.Info, group apiSpec.Group, route apiSpec.Route) spec.PathItem {
needJwt := hasKey(group.Annotation.Properties, "jwt")
var security []map[string][]string
if needJwt {
security = []map[string][]string{
{
swaggerSecurityDefinitionBearerAuth: []string{},
},
}
}
op := &spec.Operation{
OperationProps: spec.OperationProps{
Description: getStringFromKVOrDefault(route.AtDoc.Properties, "description", ""),
@@ -72,6 +81,7 @@ func spec2Path(info apiSpec.Info, group apiSpec.Group, route apiSpec.Route) spec
Deprecated: getBoolFromKVOrDefault(route.AtDoc.Properties, "deprecated", false),
Parameters: parametersFromType(route.Method, route.RequestType),
Responses: jsonResponseFromType(info, route.ResponseType),
Security: security,
},
}
externalDocsDescription := getStringFromKVOrDefault(route.AtDoc.Properties, "externalDocsDescription", "")

View File

@@ -54,7 +54,7 @@ func propertiesFromType(tp apiSpec.Type) (spec.SchemaProperties, []string) {
}
switch sampleTypeFromGoType(member.Type) {
case swaggerTypeArray:
schema.Items=itemsFromGoType(member.Type)
schema.Items = itemsFromGoType(member.Type)
case swaggerTypeObject:
p, r := propertiesFromType(member.Type)
schema.Properties = p

View File

@@ -12,6 +12,7 @@ import (
func spec2Swagger(api *apiSpec.ApiSpec) (*spec.Swagger, error) {
extensions, info := specExtensions(api.Info)
swagger := &spec.Swagger{
VendorExtensible: spec.VendorExtensible{
Extensions: extensions,
@@ -25,6 +26,9 @@ func spec2Swagger(api *apiSpec.ApiSpec) (*spec.Swagger, error) {
Host: getStringFromKVOrDefault(api.Info.Properties, "host", defaultHost),
BasePath: getStringFromKVOrDefault(api.Info.Properties, "basePath", defaultBasePath),
Paths: spec2Paths(api.Info, api.Service),
SecurityDefinitions: spec.SecurityDefinitions{
swaggerSecurityDefinitionBearerAuth: spec.APIKeyAuth(swaggerSecurityDefinitionName, swaggerSecurityDefinitionIn),
},
},
}