Merge pull request #86 from Lansongxx/feature

fix(parser.go): 修复validate字段的bug,添加了对form-data的支持
This commit is contained in:
MaxToby 2023-11-24 10:37:39 +08:00 committed by GitHub
commit a21dceb01b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -139,7 +139,7 @@ type swaggerOperationObject struct {
} `json:"requestBody,omitempty"` } `json:"requestBody,omitempty"`
Tags []string `json:"tags,omitempty"` Tags []string `json:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty"` Deprecated bool `json:"deprecated,omitempty"`
Consumes []string `json:"consumes,omitempty"`
Security *[]swaggerSecurityRequirementObject `json:"security,omitempty"` Security *[]swaggerSecurityRequirementObject `json:"security,omitempty"`
ExternalDocs *swaggerExternalDocumentationObject `json:"externalDocs,omitempty"` ExternalDocs *swaggerExternalDocumentationObject `json:"externalDocs,omitempty"`
} }

View File

@ -18,6 +18,7 @@ import (
var strColon = []byte(":") var strColon = []byte(":")
const ( const (
validateKey = "validate"
defaultOption = "default" defaultOption = "default"
stringOption = "string" stringOption = "string"
optionalOption = "optional" optionalOption = "optional"
@ -241,6 +242,7 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
Required: true, Required: true,
Schema: &schema, Schema: &schema,
} }
doc := strings.Join(route.RequestType.Documents(), ",") doc := strings.Join(route.RequestType.Documents(), ",")
doc = strings.Replace(doc, "//", "", -1) doc = strings.Replace(doc, "//", "", -1)
@ -295,6 +297,15 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
}, },
} }
if defineStruct, ok := route.RequestType.(spec.DefineStruct); ok {
for _, member := range defineStruct.Members {
if strings.Contains(member.Tag, "form") {
operationObject.Consumes = []string{"multipart/form-data"}
break
}
}
}
for _, v := range route.Doc { for _, v := range route.Doc {
markerIndex := strings.Index(v, atRespDoc) markerIndex := strings.Index(v, atRespDoc)
if markerIndex >= 0 { if markerIndex >= 0 {
@ -390,6 +401,10 @@ func renderStruct(member spec.Member) swaggerParameterObject {
sp := swaggerParameterObject{In: "query", Type: ftype, Format: format} sp := swaggerParameterObject{In: "query", Type: ftype, Format: format}
for _, tag := range member.Tags() { for _, tag := range member.Tags() {
if tag.Key == validateKey {
continue
}
sp.Name = tag.Name sp.Name = tag.Name
if len(tag.Options) == 0 { if len(tag.Options) == 0 {
sp.Required = true sp.Required = true
@ -491,6 +506,9 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.
*schema.Properties = append(*schema.Properties, kv) *schema.Properties = append(*schema.Properties, kv)
for _, tag := range member.Tags() { for _, tag := range member.Tags() {
if tag.Key == validateKey {
continue
}
if len(tag.Options) == 0 { if len(tag.Options) == 0 {
if !contains(schema.Required, tag.Name) && tag.Name != "required" { if !contains(schema.Required, tag.Name) && tag.Name != "required" {
schema.Required = append(schema.Required, tag.Name) schema.Required = append(schema.Required, tag.Name)