增加对any数据类型解析的支持 #13
| @ -133,6 +133,7 @@ type Schema struct { | |||||||
| 	XEnumDescription map[string]string    `json:"x-enumDescriptions,omitempty"` // 枚举值描述的扩展, redoc-free支持 | 	XEnumDescription map[string]string    `json:"x-enumDescriptions,omitempty"` // 枚举值描述的扩展, redoc-free支持 | ||||||
| 	Type             string               `json:"type,omitempty"`               // 类型 | 	Type             string               `json:"type,omitempty"`               // 类型 | ||||||
| 	Items            *PropertyXOf         `json:"items,omitempty"`              // items 必须存在如果 type 的值是 array。 | 	Items            *PropertyXOf         `json:"items,omitempty"`              // items 必须存在如果 type 的值是 array。 | ||||||
|  | 	OneOf            []*PropertyXOf       `json:"oneOf,omitempty"`              // type 是一个对象, allOf 指向对象描述 | ||||||
| 	Ref              string               `json:"$ref,omitempty"`               // 类型引用 | 	Ref              string               `json:"$ref,omitempty"`               // 类型引用 | ||||||
| 	Format           string               `json:"format,omitempty"`             // 格式化类型 | 	Format           string               `json:"format,omitempty"`             // 格式化类型 | ||||||
| 	Maximum          *int64               `json:"maximum,omitempty"`            // 最大值 | 	Maximum          *int64               `json:"maximum,omitempty"`            // 最大值 | ||||||
| @ -174,6 +175,7 @@ type PropertyXOf struct { | |||||||
| 	Maximum *int64 `json:"maximum,omitempty"` // 最大值 | 	Maximum *int64 `json:"maximum,omitempty"` // 最大值 | ||||||
| 	Minimum *int64 `json:"minimum,omitempty"` // 最小值 | 	Minimum *int64 `json:"minimum,omitempty"` // 最小值 | ||||||
| 	Ref     string `json:"$ref,omitempty"`    // 引用的结构描述 | 	Ref     string `json:"$ref,omitempty"`    // 引用的结构描述 | ||||||
|  | 	// Items   *PropertyXOf `json:"items,omitempty"`   // 数据每一项已用类型 | ||||||
| } | } | ||||||
|  |  | ||||||
| // SchemaDiscriminator 当一个 request bodies 或 response payloads 可以是多种 schemas 时,可以使用一个 discriminator 对象来帮助序列化、反序列化和校验 | // SchemaDiscriminator 当一个 request bodies 或 response payloads 可以是多种 schemas 时,可以使用一个 discriminator 对象来帮助序列化、反序列化和校验 | ||||||
|  | |||||||
							
								
								
									
										83
									
								
								generate.go
									
									
									
									
									
								
							
							
						
						
									
										83
									
								
								generate.go
									
									
									
									
									
								
							| @ -369,6 +369,22 @@ func (g *Generate) ParseReadConfigParam(requestCfg *define.UriBaseConfig, baseRe | |||||||
| 			}) | 			}) | ||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
|  | 		if inputType.Field(i).Type.Kind() == reflect.Interface { | ||||||
|  | 			baseReqCfg.Parameters = append(baseReqCfg.Parameters, &define.PathConfigParameter{ | ||||||
|  | 				Name:        ParseStructFieldTag.GetParamName(inputType.Field(i)), | ||||||
|  | 				In:          consts.SwaggerParameterInQuery, | ||||||
|  | 				Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)), | ||||||
|  | 				Required:    ValidateRule.IsRequired(inputType.Field(i)), | ||||||
|  | 				Deprecated:  ParseStructFieldTag.Deprecated(inputType.Field(i)), | ||||||
|  | 				Schema: &define.Schema{ | ||||||
|  | 					OneOf:            g.anyTypeConfig(inputType.Field(i)).OneOf, | ||||||
|  | 					Format:           realInputTypeFormat, | ||||||
|  | 					Enum:             ValidateRule.Enum(inputType.Field(i)), | ||||||
|  | 					XEnumDescription: ParseStructFieldTag.EnumDescription(inputType.Field(i)), | ||||||
|  | 				}, | ||||||
|  | 			}) | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
| 		if inputType.Field(i).Type.Kind() == reflect.Ptr { | 		if inputType.Field(i).Type.Kind() == reflect.Ptr { | ||||||
| 			// 处理指针 | 			// 处理指针 | ||||||
| 			if inputType.Field(i).Type.Elem().Kind() == reflect.Struct { | 			if inputType.Field(i).Type.Elem().Kind() == reflect.Struct { | ||||||
| @ -513,12 +529,18 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in | |||||||
| 				g.handleAnonymousField(schemaName, inputType.Field(i)) | 				g.handleAnonymousField(schemaName, inputType.Field(i)) | ||||||
| 				continue | 				continue | ||||||
| 			} | 			} | ||||||
|  | 			if inputType.Kind() == reflect.Interface { | ||||||
|  | 				// 处理interface{}类型参数 | ||||||
|  | 				g.docData.Components.Schemas[schemaName].Properties[propertyName] = g.anyTypeConfig(inputType.Field(i)) | ||||||
|  | 				continue | ||||||
|  | 			} | ||||||
| 			if inputType.Field(i).Type.Kind() == reflect.Ptr { | 			if inputType.Field(i).Type.Kind() == reflect.Ptr { | ||||||
| 				// 处理指针 | 				// 处理指针 | ||||||
| 				if inputType.Field(i).Type.Elem().Kind() == reflect.Struct { | 				if inputType.Field(i).Type.Elem().Kind() == reflect.Struct { | ||||||
| 					// 结构体指针 | 					// 结构体指针 | ||||||
| 					schemaNameNext := g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type.Elem()) | 					schemaNameNext := g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type.Elem()) | ||||||
| 					g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{ | 					g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{ | ||||||
|  | 						// Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)), | ||||||
| 						Ref: g.getSchemaRef(schemaNameNext), | 						Ref: g.getSchemaRef(schemaNameNext), | ||||||
| 					} | 					} | ||||||
| 				} else { | 				} else { | ||||||
| @ -572,12 +594,16 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in | |||||||
| 					g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type) | 					g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type) | ||||||
| 				} | 				} | ||||||
| 			} else { | 			} else { | ||||||
| 				convertBaseType, _ := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()) | 				if inputType.Field(i).Type.Kind() == reflect.Interface { | ||||||
| 				g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{ | 					g.docData.Components.Schemas[schemaName].Properties[propertyName] = g.anyTypeConfig(inputType.Field(i)) | ||||||
| 					Type:        convertBaseType, | 				} else { | ||||||
| 					Format:      inputType.Field(i).Type.String(), | 					convertBaseType, _ := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()) | ||||||
| 					Default:     ParseStructFieldTag.GetDefaultValue(inputType.Field(i)), | 					g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{ | ||||||
| 					Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)), | 						Type:        convertBaseType, | ||||||
|  | 						Format:      inputType.Field(i).Type.String(), | ||||||
|  | 						Default:     ParseStructFieldTag.GetDefaultValue(inputType.Field(i)), | ||||||
|  | 						Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)), | ||||||
|  | 					} | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 			// 设置参数各种属性 | 			// 设置参数各种属性 | ||||||
| @ -625,6 +651,10 @@ func (g *Generate) handleAnonymousField(schemaName string, field reflect.StructF | |||||||
| 		} else { | 		} else { | ||||||
| 			baseConvertType, isBaseType := g.realBaseType2SwaggerType(itemField.Type.String()) | 			baseConvertType, isBaseType := g.realBaseType2SwaggerType(itemField.Type.String()) | ||||||
| 			if !isBaseType { | 			if !isBaseType { | ||||||
|  | 				paramName := ParseStructFieldTag.GetParamName(itemField) | ||||||
|  | 				if itemField.Type.Kind() == reflect.Interface { | ||||||
|  | 					g.docData.Components.Schemas[schemaName].Properties[paramName] = g.anyTypeConfig(itemField) | ||||||
|  | 				} | ||||||
| 				g.AddComponentsSchema(schemaName, handleType.Field(i).Type.PkgPath(), handleType.Field(i).Type) | 				g.AddComponentsSchema(schemaName, handleType.Field(i).Type.PkgPath(), handleType.Field(i).Type) | ||||||
| 				continue | 				continue | ||||||
| 			} else { | 			} else { | ||||||
| @ -814,3 +844,44 @@ func (g *Generate) parseBaseUriConfig(uriPrefix string, paramType reflect.Type) | |||||||
| 	} | 	} | ||||||
| 	return res, nil | 	return res, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // anyTypeConfig 任意类型数据配置 | ||||||
|  | // | ||||||
|  | // Author : go_developer@163.com<白茶清欢> | ||||||
|  | // | ||||||
|  | // Date : 14:33 2025/2/19 | ||||||
|  | func (g *Generate) anyTypeConfig(structField reflect.StructField) *define.Property { | ||||||
|  | 	return &define.Property{ | ||||||
|  | 		Description: ParseStructFieldTag.GetParamDesc(structField), | ||||||
|  | 		OneOf: []*define.PropertyXOf{ | ||||||
|  | 			{ | ||||||
|  | 				Type:   consts.SwaggerDataTypeObject, | ||||||
|  | 				Format: "map[string]any", | ||||||
|  | 			}, | ||||||
|  | 			/*	{ | ||||||
|  | 				Type:   consts.SwaggerDataTypeArray, | ||||||
|  | 				Format: "[]any", | ||||||
|  | 				Items: &define.PropertyXOf{ | ||||||
|  | 					Items: nil, | ||||||
|  | 				}, | ||||||
|  | 			},*/ | ||||||
|  | 			{ | ||||||
|  | 				Type:   consts.SwaggerDataTypeInteger, | ||||||
|  | 				Format: "int/uint", | ||||||
|  | 			}, | ||||||
|  | 			{ | ||||||
|  | 				Type:   consts.SwaggerDataTypeNumber, | ||||||
|  | 				Format: "int/uint/float", | ||||||
|  | 			}, | ||||||
|  | 			{ | ||||||
|  | 				Type:   consts.SwaggerDataTypeString, | ||||||
|  | 				Format: "string", | ||||||
|  | 			}, | ||||||
|  | 			{ | ||||||
|  | 				Type:   consts.SwaggerDataTypeBoolean, | ||||||
|  | 				Format: "bool", | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user