mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
Merge pull request #517 from wuxingzhong/swagger_validate_comment
add validate comment
This commit is contained in:
commit
e51cfe1f24
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@ -184,6 +185,12 @@ func (t *swaggerGen) getQueryParameter(file *descriptor.FileDescriptorProto,
|
||||
cleanComment := tag.GetCommentWithoutTag(fComment.Leading)
|
||||
|
||||
p.Description = strings.Trim(strings.Join(cleanComment, "\n"), "\n\r ")
|
||||
validateComment := getValidateComment(field)
|
||||
if p.Description != "" && validateComment != "" {
|
||||
p.Description = p.Description + "," + validateComment
|
||||
} else if validateComment != "" {
|
||||
p.Description = validateComment
|
||||
}
|
||||
p.In = "query"
|
||||
p.Required = generator.GetFieldRequired(field, t.Reg, input)
|
||||
typ, isArray, format := getFieldSwaggerType(field)
|
||||
@ -208,6 +215,12 @@ func (t *swaggerGen) schemaForField(file *descriptor.FileDescriptorProto,
|
||||
gen.Error(err, "comment not found err %+v")
|
||||
}
|
||||
schema.Description = strings.Trim(fComment.Leading, "\n\r ")
|
||||
validateComment := getValidateComment(field)
|
||||
if schema.Description != "" && validateComment != "" {
|
||||
schema.Description = schema.Description + "," + validateComment
|
||||
} else if validateComment != "" {
|
||||
schema.Description = validateComment
|
||||
}
|
||||
typ, isArray, format := getFieldSwaggerType(field)
|
||||
if !generator.IsScalar(field) {
|
||||
if generator.IsMap(field, t.Reg) {
|
||||
@ -303,3 +316,26 @@ func getFieldSwaggerType(field *descriptor.FieldDescriptorProto) (typeName strin
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getValidateComment(field *descriptor.FieldDescriptorProto) string {
|
||||
var (
|
||||
tags []reflect.StructTag
|
||||
)
|
||||
//get required info from gogoproto.moretags
|
||||
moretags := tag.GetMoreTags(field)
|
||||
if moretags != nil {
|
||||
tags = []reflect.StructTag{reflect.StructTag(*moretags)}
|
||||
}
|
||||
validateTag := tag.GetTagValue("validate", tags)
|
||||
|
||||
// trim
|
||||
regStr := []string{
|
||||
"required *,*",
|
||||
"omitempty *,*",
|
||||
}
|
||||
for _, v := range regStr {
|
||||
re, _ := regexp.Compile(v)
|
||||
validateTag = re.ReplaceAllString(validateTag, "")
|
||||
}
|
||||
return validateTag
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user