feat: 完善结构体字段类型解析
This commit is contained in:
58
openapi/schema_test.go
Normal file
58
openapi/schema_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Package openapi ...
|
||||
//
|
||||
// Description : openapi ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2026-01-06 10:50
|
||||
package openapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/getkin/kin-openapi/openapi3"
|
||||
)
|
||||
|
||||
func TestParseStructField(t *testing.T) {
|
||||
type Category struct {
|
||||
ID int64 `json:"id" description:"分类ID"`
|
||||
Name string `json:"name" description:"分类名称"`
|
||||
}
|
||||
type Product struct {
|
||||
ID int64 `json:"id" description:"产品ID" example:"1001" required:"true"`
|
||||
Name string `json:"name" description:"产品名称" example:"iPhone 13" minLength:"2" maxLength:"100" required:"true"`
|
||||
Price float64 `json:"price" description:"价格" example:"6999.99" min:"0"`
|
||||
Stock int `json:"stock" description:"库存" example:"100" min:"0"`
|
||||
Tags []string `json:"tags" description:"标签"`
|
||||
Attributes map[string]string `json:"attributes" description:"属性"`
|
||||
CreatedAt time.Time `json:"created_at" description:"创建时间"`
|
||||
UpdatedAt *time.Time `json:"updated_at,omitempty" description:"更新时间"`
|
||||
Category *Category `json:"category,omitempty" description:"分类"`
|
||||
}
|
||||
|
||||
// 生成 Product 的 Schema
|
||||
productSchema := GenerateOpenAPISchema(Product{})
|
||||
|
||||
// 创建完整的 OpenAPI 文档
|
||||
doc := &openapi3.T{
|
||||
OpenAPI: "3.0.0",
|
||||
Info: &openapi3.Info{
|
||||
Title: "产品服务 API",
|
||||
Description: "产品管理相关接口",
|
||||
Version: "1.0.0",
|
||||
},
|
||||
Components: &openapi3.Components{
|
||||
Schemas: map[string]*openapi3.SchemaRef{
|
||||
"Product": productSchema,
|
||||
"Category": GenerateOpenAPISchema(Category{}),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// 输出 JSON
|
||||
data, _ := json.MarshalIndent(doc, "", " ")
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
Reference in New Issue
Block a user