From 22550f9ac063e57daf0a9cb16cc89af297bfc70f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 25 Feb 2025 22:21:17 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=A3=E6=9E=90openapi?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E7=9A=84=E6=96=B9=E6=B3=95,=20=E5=BE=85?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/openapi.go | 2 +- generate.go | 4 ++-- parser.go | 37 +++++++++++++++++++------------------ parser_test.go | 7 ------- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/define/openapi.go b/define/openapi.go index 1337eeb..52bd43f 100644 --- a/define/openapi.go +++ b/define/openapi.go @@ -227,7 +227,7 @@ type RequestBody struct { // Date : 17:21 2024/7/19 type Media struct { Schema *Schema `json:"schema,omitempty"` // 定义此媒体类型的结构。 - Example string `json:"example,omitempty"` // 媒体类型的示例。示例对象应该符合此媒体类型的格式, 这里指定的example对象 object is mutually exclusive of the examples object. 而且如果引用的schema也包含示例,在这里指定的example值将会覆盖schema提供的示例。 + Example map[string]any `json:"example,omitempty"` // 媒体类型的示例。示例对象应该符合此媒体类型的格式, 这里指定的example对象 object is mutually exclusive of the examples object. 而且如果引用的schema也包含示例,在这里指定的example值将会覆盖schema提供的示例。 Examples map[string]*Example `json:"examples,omitempty"` // 媒体类型的示例,每个媒体对象的值都应该匹配它对应的媒体类型的格式。 The examples object is mutually exclusive of the example object. 而且如果引用的schema也包含示例,在这里指定的example值将会覆盖schema提供的示例。 Encoding map[string]*Encoding `json:"encoding,omitempty"` // 属性名与编码信息的映射。每个属性名必须存在于schema属性的key中,当媒体类型等于multipart或application/x-www-form-urlencoded时,编码对象信息仅适用于requestBody。 } diff --git a/generate.go b/generate.go index 5c2f56b..fa4351f 100644 --- a/generate.go +++ b/generate.go @@ -207,7 +207,7 @@ func (g *Generate) AddApiFromInAndOut(uriPrefix string, paramType reflect.Type, Schema: &define.Schema{ Ref: g.getSchemaRef(paramSchemaName), }, - Example: "", + Example: nil, Examples: nil, Encoding: nil, } @@ -220,7 +220,7 @@ func (g *Generate) AddApiFromInAndOut(uriPrefix string, paramType reflect.Type, Schema: &define.Schema{ Ref: g.getSchemaRef(resultSchemaName), }, - Example: "", + Example: nil, Examples: nil, Encoding: nil, } diff --git a/parser.go b/parser.go index 327fe9f..5cd81a9 100644 --- a/parser.go +++ b/parser.go @@ -8,34 +8,35 @@ package api_doc import ( + "errors" "git.zhangdeman.cn/gateway/api-doc/define" "git.zhangdeman.cn/zhangdeman/serialize" - "github.com/tidwall/gjson" - "strings" ) -// Parse 解析swagger文档 +// ParseOpenapi3 解析openapi文档 // // Author : go_developer@163.com<白茶清欢> // -// Date : 16:54 2024/12/23 -func Parse(docUrl string) (*define.DocParseResult, error) { +// Date : 21:02 2025/2/25 +func ParseOpenapi3(docUrl string, docContent string) (*define.OpenapiDoc, error) { var ( - err error - docContent []byte + docRes define.OpenapiDoc + err error + docByte []byte ) - if docContent, err = serialize.File.ReadFromRemote(docUrl); nil != err { + if len(docContent) < 2 { + if len(docUrl) == 0 { + return nil, errors.New("doc url and doc content all empty") + } + if docByte, err = serialize.File.ReadFromRemote(docUrl); nil != err { + return nil, err + } + } else { + docByte = []byte(docContent) + } + if err = serialize.JSON.UnmarshalWithNumber(docByte, &docRes); nil != err { return nil, err } - swaggerVersion := gjson.GetBytes(docContent, "swagger").String() - if "" == swaggerVersion || !strings.HasPrefix(swaggerVersion, "2.") { - // 未指定swagger版本或swagger版本3.x - return ParseOpenapi3(docContent) - } - return ParseSwagger2(docContent) -} - -func ParseOpenapi3(docContent []byte) (*define.DocParseResult, error) { - return nil, nil + return &docRes, nil } diff --git a/parser_test.go b/parser_test.go index ff7cfca..529c13a 100644 --- a/parser_test.go +++ b/parser_test.go @@ -11,7 +11,6 @@ import ( "encoding/json" "fmt" "git.zhangdeman.cn/gateway/api-doc/define" - "git.zhangdeman.cn/zhangdeman/serialize" "reflect" "testing" ) @@ -95,9 +94,3 @@ func Test_parser_Openapi3(t *testing.T) { byteData, _ := json.Marshal(g.docData) fmt.Println(string(byteData)) } - -func TestParseForSwagger(t *testing.T) { - docUrl := "https://git.zhangdeman.cn/swagger.v1.json" - res, _ := Parse(docUrl) - serialize.JSON.ConsoleOutput(res) -} From b5dbc330fc4c4a180a1e46d07ef0408a045bc038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 26 Feb 2025 11:10:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/openapi.go | 6 +++--- parser_test.go | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/define/openapi.go b/define/openapi.go index 52bd43f..d8a83d8 100644 --- a/define/openapi.go +++ b/define/openapi.go @@ -132,7 +132,7 @@ type Schema struct { Required []string `json:"required,omitempty"` // 必传属性列表 Enum []any `json:"enum,omitempty"` // 枚举值列表 XEnumDescription map[string]string `json:"x-enumDescriptions,omitempty"` // 枚举值描述的扩展, redoc-free支持 - Type string `json:"type,omitempty"` // 类型 + Type any `json:"type,omitempty"` // 类型 (string | []string) Items *PropertyXOf `json:"items,omitempty"` // items 必须存在如果 type 的值是 array。 OneOf []*PropertyXOf `json:"oneOf,omitempty"` // type 是一个对象, allOf 指向对象描述 Ref string `json:"$ref,omitempty"` // 类型引用 @@ -150,7 +150,7 @@ type Schema struct { // // Date : 17:05 2024/7/19 type Property struct { - Type string `json:"type,omitempty"` // 数据类型, swagger本身的定义 + Type any `json:"type,omitempty"` // 数据类型(string | []string), swagger本身的定义 Format string `json:"format,omitempty"` // 对应编程语言中的数据类型描述 Enum []any `json:"enum,omitempty"` // 枚举值列表 XEnumDescription map[string]string `json:"x-enumDescriptions,omitempty"` // 枚举值描述的扩展, redoc-free支持 @@ -165,7 +165,7 @@ type Property struct { OneOf []*PropertyXOf `json:"oneOf,omitempty"` // type 是一个对象, allOf 指向对象描述 AnyOf []*PropertyXOf `json:"anyOf,omitempty"` // type 是一个对象, allOf 指向对象描述 Items *PropertyXOf `json:"items,omitempty"` // items 必须存在如果 type 的值是 array。 - AdditionalProperties *PropertyXOf `json:"additionalProperties,omitempty"` // additionalProperties 是一个用于描述模型中包含未在属性列表中定义的额外属性的选项。它允许接受任意的一个或多个键值对。它的作用是为了在模型定义中包含未知或动态属性。通常,在设计 API 时,我们无法预先知道 API 用户会传递什么样的额外属性,这时就可以使用 additionalProperties 功能来灵活地处理这些未知属性。 + AdditionalProperties any `json:"additionalProperties,omitempty"` // additionalProperties(PropertyXOf | bool) 是一个用于描述模型中包含未在属性列表中定义的额外属性的选项。它允许接受任意的一个或多个键值对。它的作用是为了在模型定义中包含未知或动态属性。通常,在设计 API 时,我们无法预先知道 API 用户会传递什么样的额外属性,这时就可以使用 additionalProperties 功能来灵活地处理这些未知属性。 Properties map[string]*Property `json:"properties,omitempty"` // type = object 时, 定义对象属性 Ref string `json:"$ref,omitempty"` // 对描述的引用 } diff --git a/parser_test.go b/parser_test.go index 529c13a..9211b43 100644 --- a/parser_test.go +++ b/parser_test.go @@ -94,3 +94,8 @@ func Test_parser_Openapi3(t *testing.T) { byteData, _ := json.Marshal(g.docData) fmt.Println(string(byteData)) } + +func Test_parse_Openapi3_doc(t *testing.T) { + res, err := ParseOpenapi3("http://localhost:10990/static-server/github-openapi.json", "") + fmt.Println(res, err) +}