feature/openapi增加openapi版本文档数据结构定义 #1

Merged
zhangdeman merged 15 commits from feature/openapi into master 2024-07-19 22:06:14 +08:00
Showing only changes of commit 6642b9bcef - Show all commits

View File

@ -11,6 +11,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.zhangdeman.cn/gateway/api-doc/define" "git.zhangdeman.cn/gateway/api-doc/define"
"os"
"os/user"
"testing" "testing"
) )
@ -20,215 +22,10 @@ import (
// //
// Date : 17:55 2024/7/19 // Date : 17:55 2024/7/19
func Test_parser_Openapi3(t *testing.T) { func Test_parser_Openapi3(t *testing.T) {
testData := `{ current, _ := user.Current()
"openapi": "3.0.1", byteData, _ := os.ReadFile(current.HomeDir + "/Downloads/test-openapi-doc.json")
"info": {
"title": "测试",
"description": "测试",
"contact": {
"name": "白茶",
"url": "http://www.baidu.com",
"email": "go@email.com"
},
"license": {
"name": "Apache-2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.txt"
},
"version": ""
},
"servers": [
{
"url": "http://www.baidu.com/"
}
],
"paths": {
"/test": {
"post": {
"tags": [
"测试接口生成"
],
"summary": "测试接口",
"description": "测试接口",
"requestBody": {
"description": "参数结构",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/test.jsonBody"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "返回数据",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/test.200.output"
}
}
}
}
},
"x-codegen-request-body-name": "jsonBody"
}
}
},
"components": {
"schemas": {
"object_map": {
"type": "object"
},
"test.200.output": {
"type": "object",
"properties": {
"nick_name": {
"type": "string",
"description": "姓名"
},
"obj": {
"type": "object",
"description": "测试返回生成map",
"allOf": [
{
"$ref": "#/components/schemas/object_map"
}
]
},
"person": {
"type": "object",
"description": "参数描述",
"allOf": [
{
"$ref": "#/components/schemas/test.200.output.person"
}
]
}
},
"format": "map[string]any"
},
"test.200.output.person": {
"type": "object",
"properties": {
"nick_name": {
"type": "string",
"description": "昵称"
}
},
"format": "map[string]any"
},
"test.jsonBody": {
"required": [
"age",
"name"
],
"type": "object",
"properties": {
"age": {
"type": "integer",
"description": "年龄"
},
"name": {
"type": "string",
"description": "姓名"
},
"obj": {
"type": "object",
"description": "测试global_map",
"allOf": [
{
"$ref": "#/components/schemas/object_map"
}
]
},
"person": {
"type": "object",
"description": "参数描述",
"allOf": [
{
"$ref": "#/components/schemas/test.jsonBody.person"
}
]
},
"test_list": {
"type": "array",
"description": "参数描述",
"items": {
"$ref": "#/components/schemas/test.jsonBody.test_list.item"
}
}
},
"format": "map[string]any"
},
"test.jsonBody.person": {
"type": "object",
"properties": {
"job": {
"type": "object",
"description": "参数描述",
"allOf": [
{
"$ref": "#/components/schemas/test.jsonBody.person.job"
}
]
},
"sex": {
"type": "string",
"description": "性别"
}
},
"format": "map[string]any"
},
"test.jsonBody.person.job": {
"type": "object",
"properties": {
"list": {
"type": "array",
"description": "性别",
"items": {
"type": "string"
}
},
"test": {
"type": "integer",
"description": "测试工作"
},
"year": {
"type": "array",
"description": "年份",
"items": {
"type": "integer"
}
}
},
"format": "map[string]any"
},
"test.jsonBody.test_list.item": {
"required": [
"age",
"name"
],
"type": "object",
"properties": {
"age": {
"type": "integer",
"description": "年龄"
},
"name": {
"type": "string",
"description": "年龄"
}
},
"format": "map[string]any"
}
}
},
"x-original-swagger-version": "2.0"
}`
var data define.OpenapiDoc var data define.OpenapiDoc
err := json.Unmarshal([]byte(testData), &data) err := json.Unmarshal(byteData, &data)
if nil != err { if nil != err {
fmt.Println("解析失败 : " + err.Error()) fmt.Println("解析失败 : " + err.Error())
} else { } else {