Skip to content

Commit

Permalink
优化OpenAPI定义解析
Browse files Browse the repository at this point in the history
将OpenAPI定义中"additional_properties"的类型从[]interface{}明确为[]map[string]interface{},以更好地表示JSON对象。此更改提高了定义解析的准确性和可读性。
  • Loading branch information
weibaohui committed Oct 15, 2024
1 parent 19ca589 commit 89f771d
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions internal/kubectl/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ type RootDefinitions struct {
Swagger string `json:"swagger"`
Definitions Definitions `json:"definitions,omitempty"`
}

// Definitions 表示所有定义
// 使用interface{}
type Definitions struct {
AdditionalProperties []interface{} `json:"additional_properties"`
AdditionalProperties []map[string]interface{} `json:"additional_properties"`
}

// definitionsMap 存储所有定义,以便处理引用
Expand Down Expand Up @@ -245,24 +248,15 @@ func initDoc() {
definitionList := root.Definitions.AdditionalProperties

// 进行第一遍处理,此时Ref并没有读取,只是记录了引用
for _, item := range definitionList {
definition, ok := item.(map[string]interface{})
jstr := utils.ToJSON(definition)
if !ok {
fmt.Printf("convert definition error\n")
os.Exit(1)
}

for _, definition := range definitionList {
str := utils.ToJSON(definition)
// 解析 Schema 并构建树形结构
treeRoot, err := parseOpenAPISchema(jstr)
treeRoot, err := parseOpenAPISchema(str)
if err != nil {
fmt.Printf("Error parsing OpenAPI schema: %v\n", err)
os.Exit(1)
}
trees = append(trees, treeRoot)
// // 打印树形结构
// printTree(treeRoot, 0)

}

// 进行遍历处理,将child中ref对应的类型提取出来
Expand Down

0 comments on commit 89f771d

Please sign in to comment.