mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
fix issue #1798
This commit is contained in:
@ -82,8 +82,8 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
patternKeyForRequired = `required`
|
||||
patternKeyForIn = `in:`
|
||||
validationRuleKeyForRequired = `required`
|
||||
validationRuleKeyForIn = `in:`
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -80,8 +80,9 @@ func (oai *OpenApiV3) newParameterRefWithStructMethod(field gstructs.Field, path
|
||||
}
|
||||
|
||||
// Required check.
|
||||
if parameter.Schema.Value != nil && parameter.Schema.Value.Pattern != "" {
|
||||
if gset.NewStrSetFrom(gstr.Split(parameter.Schema.Value.Pattern, "|")).Contains(patternKeyForRequired) {
|
||||
if parameter.Schema.Value != nil && parameter.Schema.Value.ValidationRules != "" {
|
||||
validationRuleArray := gstr.Split(parameter.Schema.Value.ValidationRules, "|")
|
||||
if gset.NewStrSetFrom(validationRuleArray).Contains(validationRuleKeyForRequired) {
|
||||
parameter.Required = true
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,6 +60,7 @@ type Schema struct {
|
||||
AdditionalProperties *SchemaRef `json:"additionalProperties,omitempty"`
|
||||
Discriminator *Discriminator `json:"discriminator,omitempty"`
|
||||
XExtensions XExtensions `json:"-"`
|
||||
ValidationRules string `json:"-"`
|
||||
}
|
||||
|
||||
func (s Schema) MarshalJSON() ([]byte, error) {
|
||||
@ -183,9 +184,9 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) {
|
||||
}
|
||||
|
||||
schema.Properties.Iterator(func(key string, ref SchemaRef) bool {
|
||||
if ref.Value != nil && ref.Value.Pattern != "" {
|
||||
validationRuleSet := gset.NewStrSetFrom(gstr.Split(ref.Value.Pattern, "|"))
|
||||
if validationRuleSet.Contains(patternKeyForRequired) {
|
||||
if ref.Value != nil && ref.Value.ValidationRules != "" {
|
||||
validationRuleSet := gset.NewStrSetFrom(gstr.Split(ref.Value.ValidationRules, "|"))
|
||||
if validationRuleSet.Contains(validationRuleKeyForRequired) {
|
||||
schema.Required = append(schema.Required, key)
|
||||
}
|
||||
}
|
||||
@ -212,14 +213,14 @@ func (oai *OpenApiV3) tagMapToSchema(tagMap map[string]string, schema *Schema) e
|
||||
for _, tag := range gvalid.GetTags() {
|
||||
if validationTagValue, ok := tagMap[tag]; ok {
|
||||
_, validationRules, _ := gvalid.ParseTagValue(validationTagValue)
|
||||
schema.Pattern = validationRules
|
||||
schema.ValidationRules = validationRules
|
||||
// Enum checks.
|
||||
if len(schema.Enum) == 0 {
|
||||
for _, rule := range gstr.SplitAndTrim(validationRules, "|") {
|
||||
if gstr.HasPrefix(rule, patternKeyForIn) {
|
||||
if gstr.HasPrefix(rule, validationRuleKeyForIn) {
|
||||
var (
|
||||
isAllEnumNumber = true
|
||||
enumArray = gstr.SplitAndTrim(rule[len(patternKeyForIn):], ",")
|
||||
enumArray = gstr.SplitAndTrim(rule[len(validationRuleKeyForIn):], ",")
|
||||
)
|
||||
for _, enum := range enumArray {
|
||||
if !gstr.IsNumeric(enum) {
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// XExtensions stores the `x-` custom extensions.
|
||||
type XExtensions map[string]interface{}
|
||||
type XExtensions map[string]string
|
||||
|
||||
func (oai *OpenApiV3) tagMapToXExtensions(tagMap map[string]string, extensions XExtensions) {
|
||||
for k, v := range tagMap {
|
||||
|
||||
Reference in New Issue
Block a user