238 lines
5.4 KiB
Go
238 lines
5.4 KiB
Go
|
// Package api_doc ...
|
||
|
//
|
||
|
// Description : api_doc ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 2024-07-19 17:52
|
||
|
package api_doc
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
// Test_parser_Openapi3 测试数据结构定义正确性
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 17:55 2024/7/19
|
||
|
func Test_parser_Openapi3(t *testing.T) {
|
||
|
testData := `{
|
||
|
"openapi": "3.0.1",
|
||
|
"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
|
||
|
err := json.Unmarshal([]byte(testData), &data)
|
||
|
if nil != err {
|
||
|
fmt.Println("解析失败 : " + err.Error())
|
||
|
} else {
|
||
|
fmt.Println("解析成功")
|
||
|
}
|
||
|
}
|