优化文档数据结构体 #19
@ -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。
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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,
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										37
									
								
								parser.go
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user