mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-14 02:10:00 +08:00
fix: goctl swagger missing security definition and submit json body data error (#4808)
This commit is contained in:
@@ -7,6 +7,15 @@ import (
|
|||||||
"google.golang.org/grpc/metadata"
|
"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 {
|
func getBoolFromKVOrDefault(properties map[string]string, key string, def bool) bool {
|
||||||
if len(properties) == 0 {
|
if len(properties) == 0 {
|
||||||
return def
|
return def
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ package swagger
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/parser"
|
"github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/parser"
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -29,4 +29,8 @@ const (
|
|||||||
schemeHttps = "https"
|
schemeHttps = "https"
|
||||||
defaultHost = "127.0.0.1"
|
defaultHost = "127.0.0.1"
|
||||||
defaultBasePath = "/"
|
defaultBasePath = "/"
|
||||||
|
|
||||||
|
swaggerSecurityDefinitionBearerAuth = "BearerAuth"
|
||||||
|
swaggerSecurityDefinitionName = "Authorization"
|
||||||
|
swaggerSecurityDefinitionIn = "header"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ type (
|
|||||||
tags: "query 演示" // 对应 swagger 的 tags,可以对 swagger 中的 api 进行分组
|
tags: "query 演示" // 对应 swagger 的 tags,可以对 swagger 中的 api 进行分组
|
||||||
summary: "query 类型接口集合" // 对应 swagger 的 summary
|
summary: "query 类型接口集合" // 对应 swagger 的 summary
|
||||||
prefix: v1
|
prefix: v1
|
||||||
|
jwt: Auth
|
||||||
)
|
)
|
||||||
service Swagger {
|
service Swagger {
|
||||||
@doc (
|
@doc (
|
||||||
|
|||||||
@@ -149,13 +149,13 @@ func parametersFromType(method string, tp apiSpec.Type) []spec.Parameter {
|
|||||||
ExclusiveMaximum: exclusiveMaximum,
|
ExclusiveMaximum: exclusiveMaximum,
|
||||||
Minimum: minimum,
|
Minimum: minimum,
|
||||||
ExclusiveMinimum: exclusiveMinimum,
|
ExclusiveMinimum: exclusiveMinimum,
|
||||||
Enum: enumsValueFromOptions(jsonTag.Options),
|
Enum: enumsValueFromOptions(jsonTag.Options),
|
||||||
AdditionalProperties: mapFromGoType(member.Type),
|
AdditionalProperties: mapFromGoType(member.Type),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
switch sampleTypeFromGoType(member.Type) {
|
switch sampleTypeFromGoType(member.Type) {
|
||||||
case swaggerTypeArray:
|
case swaggerTypeArray:
|
||||||
schema.Items=itemsFromGoType(member.Type)
|
schema.Items = itemsFromGoType(member.Type)
|
||||||
case swaggerTypeObject:
|
case swaggerTypeObject:
|
||||||
p, r := propertiesFromType(member.Type)
|
p, r := propertiesFromType(member.Type)
|
||||||
schema.Properties = p
|
schema.Properties = p
|
||||||
@@ -168,6 +168,7 @@ func parametersFromType(method string, tp apiSpec.Type) []spec.Parameter {
|
|||||||
resp = append(resp, spec.Parameter{
|
resp = append(resp, spec.Parameter{
|
||||||
ParamProps: spec.ParamProps{
|
ParamProps: spec.ParamProps{
|
||||||
In: paramsInBody,
|
In: paramsInBody,
|
||||||
|
Name: paramsInBody,
|
||||||
Required: true,
|
Required: true,
|
||||||
Schema: &spec.Schema{
|
Schema: &spec.Schema{
|
||||||
SchemaProps: spec.SchemaProps{
|
SchemaProps: spec.SchemaProps{
|
||||||
|
|||||||
@@ -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 {
|
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{
|
op := &spec.Operation{
|
||||||
OperationProps: spec.OperationProps{
|
OperationProps: spec.OperationProps{
|
||||||
Description: getStringFromKVOrDefault(route.AtDoc.Properties, "description", ""),
|
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),
|
Deprecated: getBoolFromKVOrDefault(route.AtDoc.Properties, "deprecated", false),
|
||||||
Parameters: parametersFromType(route.Method, route.RequestType),
|
Parameters: parametersFromType(route.Method, route.RequestType),
|
||||||
Responses: jsonResponseFromType(info, route.ResponseType),
|
Responses: jsonResponseFromType(info, route.ResponseType),
|
||||||
|
Security: security,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
externalDocsDescription := getStringFromKVOrDefault(route.AtDoc.Properties, "externalDocsDescription", "")
|
externalDocsDescription := getStringFromKVOrDefault(route.AtDoc.Properties, "externalDocsDescription", "")
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func propertiesFromType(tp apiSpec.Type) (spec.SchemaProperties, []string) {
|
|||||||
}
|
}
|
||||||
switch sampleTypeFromGoType(member.Type) {
|
switch sampleTypeFromGoType(member.Type) {
|
||||||
case swaggerTypeArray:
|
case swaggerTypeArray:
|
||||||
schema.Items=itemsFromGoType(member.Type)
|
schema.Items = itemsFromGoType(member.Type)
|
||||||
case swaggerTypeObject:
|
case swaggerTypeObject:
|
||||||
p, r := propertiesFromType(member.Type)
|
p, r := propertiesFromType(member.Type)
|
||||||
schema.Properties = p
|
schema.Properties = p
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
func spec2Swagger(api *apiSpec.ApiSpec) (*spec.Swagger, error) {
|
func spec2Swagger(api *apiSpec.ApiSpec) (*spec.Swagger, error) {
|
||||||
extensions, info := specExtensions(api.Info)
|
extensions, info := specExtensions(api.Info)
|
||||||
|
|
||||||
swagger := &spec.Swagger{
|
swagger := &spec.Swagger{
|
||||||
VendorExtensible: spec.VendorExtensible{
|
VendorExtensible: spec.VendorExtensible{
|
||||||
Extensions: extensions,
|
Extensions: extensions,
|
||||||
@@ -25,6 +26,9 @@ func spec2Swagger(api *apiSpec.ApiSpec) (*spec.Swagger, error) {
|
|||||||
Host: getStringFromKVOrDefault(api.Info.Properties, "host", defaultHost),
|
Host: getStringFromKVOrDefault(api.Info.Properties, "host", defaultHost),
|
||||||
BasePath: getStringFromKVOrDefault(api.Info.Properties, "basePath", defaultBasePath),
|
BasePath: getStringFromKVOrDefault(api.Info.Properties, "basePath", defaultBasePath),
|
||||||
Paths: spec2Paths(api.Info, api.Service),
|
Paths: spec2Paths(api.Info, api.Service),
|
||||||
|
SecurityDefinitions: spec.SecurityDefinitions{
|
||||||
|
swaggerSecurityDefinitionBearerAuth: spec.APIKeyAuth(swaggerSecurityDefinitionName, swaggerSecurityDefinitionIn),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user