Skip to content

Commit

Permalink
fix: typo
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Nov 23, 2024
1 parent d70b3fb commit 3438969
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 43 deletions.
2 changes: 1 addition & 1 deletion openapi/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (d *Document) build(p *message.Printer, lang language.Tag, filterTags []str
Tags: tags,
ExternalDocs: d.externalDocs.build(p),

XFavicon: d.favicon,
XLogo: d.logo,
XAssets: d.assetsURL,
XLanguage: lang.String(),
XModified: d.last.Format(time.RFC3339),
Expand Down
42 changes: 24 additions & 18 deletions openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type (

templateName string
assetsURL string
favicon string
logo string

// 其它一些状态的设置

Expand All @@ -98,20 +98,22 @@ type (
s web.Server
}

paths = orderedmap.OrderedMap[string, *renderer[pathItemRenderer]]

openAPIRenderer struct {
OpenAPI string `json:"openapi" yaml:"openapi"`
Info *infoRenderer `json:"info" yaml:"info"`
Servers []*serverRenderer `json:"servers,omitempty" yaml:"servers,omitempty"`
Paths *orderedmap.OrderedMap[string, *renderer[pathItemRenderer]] `json:"paths,omitempty" yaml:"paths,omitempty"`
WebHooks *orderedmap.OrderedMap[string, *renderer[pathItemRenderer]] `json:"webhooks,omitempty" yaml:"webhooks,omitempty"`
Components *componentsRenderer `json:"components,omitempty" yaml:"components,omitempty"`
Security []*orderedmap.OrderedMap[string, []string] `json:"security,omitempty" yaml:"security,omitempty"`
Tags []*tagRenderer `json:"tags,omitempty" yaml:"tags,omitempty"`
ExternalDocs *externalDocsRenderer `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
OpenAPI string `json:"openapi" yaml:"openapi"`
Info *infoRenderer `json:"info" yaml:"info"`
Servers []*serverRenderer `json:"servers,omitempty" yaml:"servers,omitempty"`
Paths *paths `json:"paths,omitempty" yaml:"paths,omitempty"`
WebHooks *paths `json:"webhooks,omitempty" yaml:"webhooks,omitempty"`
Components *componentsRenderer `json:"components,omitempty" yaml:"components,omitempty"`
Security []*orderedmap.OrderedMap[string, []string] `json:"security,omitempty" yaml:"security,omitempty"`
Tags []*tagRenderer `json:"tags,omitempty" yaml:"tags,omitempty"`
ExternalDocs *externalDocsRenderer `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`

// 扩展内容

XFavicon string `json:"x-favicon,omitempty" yaml:"x-favicon,omitempty"`
XLogo string `json:"x-logo,omitempty" yaml:"x-logo,omitempty"`
XAssets string `json:"x-assets,omitempty" yaml:"x-assets,omitempty"`
XLanguage string `json:"x-language,omitempty" yaml:"x-language,omitempty"`
XModified string `json:"x-modified,omitempty" yaml:"x-modified,omitempty"`
Expand Down Expand Up @@ -315,10 +317,12 @@ type (
Content map[string]*Schema // key = mimetype
}

content = orderedmap.OrderedMap[string, *mediaTypeRenderer]

responseRenderer struct {
Description string `json:"description" yaml:"description"`
Headers *orderedmap.OrderedMap[string, *renderer[headerRenderer]] `json:"headers,omitempty" yaml:"headers,omitempty"`
Content *orderedmap.OrderedMap[string, *mediaTypeRenderer] `json:"content,omitempty" yaml:"content,omitempty"`
Content *content `json:"content,omitempty" yaml:"content,omitempty"`
}

Request struct {
Expand All @@ -333,9 +337,9 @@ type (
}

requestRenderer struct {
Content *orderedmap.OrderedMap[string, *mediaTypeRenderer] `json:"content" yaml:"content"`
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
Description string `json:"description" yaml:"description"`
Content *content `json:"content" yaml:"content"`
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
Description string `json:"description" yaml:"description"`
}

Server struct {
Expand All @@ -344,10 +348,12 @@ type (
Variables []*ServerVariable
}

serverVars = orderedmap.OrderedMap[string, *serverVariableRenderer]

serverRenderer struct {
URL string `json:"url" yaml:"url"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Variables *orderedmap.OrderedMap[string, *serverVariableRenderer] `json:"variables,omitempty" yaml:"variables,omitempty"`
URL string `json:"url" yaml:"url"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Variables *serverVars `json:"variables,omitempty" yaml:"variables,omitempty"`
}

ServerVariable struct {
Expand Down
11 changes: 5 additions & 6 deletions openapi/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ func WithOptions(enable bool) Option {
// assets 资源基地址,apidoc 的浏览工具一般都是在一个 html 文件中引入一些依赖文件,
// assets 就是这些依赖文件的基地址,可是指向一个 CDN 的地址,比如 swagger 的
// https://unpkg.com/[email protected]
// favicon 图标,请使用 icon 或是 png 这种大部分浏览器都支持的图标格式,
// 可以为空。尽量提供比较大的图标,可能会被用在除 favicon 之外的地方;
// logo LOGO 图标,可能还会被用于 favicon 等功能,请确保格式能被正确处理,可以为空;
//
// NOTE: [github.com/issue9/webuse/openapi] 下实现了部分简单的模板。
func WithHTML(tpl, assets, favicon string) Option {
func WithHTML(tpl, assets, logo string) Option {
return func(d *Document) {
d.templateName = tpl
d.assetsURL = assets
d.favicon = favicon
d.logo = logo
}
}

Expand Down Expand Up @@ -148,7 +147,7 @@ func WithCookie(global bool, p ...*Parameter) Option {
}
}

// WithQuery 向 components/paramters 添加 InQuery 的对象
// WithQuery 向 components/parameters 添加 InQuery 的对象
//
// p 要求必须指定 Ref.Ref。其它接口可以通过 ref 引用此对象。
//
Expand All @@ -164,7 +163,7 @@ func WithQuery(p ...*Parameter) Option {
}
}

// WithPath 向 components/paramters 添加 InPath 的对象
// WithPath 向 components/parameters 添加 InPath 的对象
//
// p 要求必须指定 Ref.Ref。其它接口可以通过 ref 引用此对象。
//
Expand Down
14 changes: 13 additions & 1 deletion openapi/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestWithHTML(t *testing.T) {
d := New(ss, web.Phrase("desc"), WithHTML("tpl", "./dist", "./dist/favicon.png"))
a.Equal(d.assetsURL, "./dist").
Equal(d.templateName, "tpl").
Equal(d.favicon, "./dist/favicon.png")
Equal(d.logo, "./dist/favicon.png")
}

func TestNewLicense(t *testing.T) {
Expand Down Expand Up @@ -60,6 +60,18 @@ func TestWithResponse(t *testing.T) {
Equal(d.responses["500"], "500")
}

func TestWithProblemResponse(t *testing.T) {
a := assert.New(t, false)
ss := newServer(a)

d := New(ss, web.Phrase("desc"), WithProblemResponse())
a.NotNil(d).
Length(d.responses, 2).
Length(d.components.responses, 1).
Equal(d.responses["4XX"], "problem").
Equal(d.responses["5XX"], "problem")
}

func TestWithMediaType(t *testing.T) {
a := assert.New(t, false)
ss := newServer(a)
Expand Down
36 changes: 19 additions & 17 deletions openapi/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,25 @@ type Schema struct {
Enum []any
}

type properties = orderedmap.OrderedMap[string, *renderer[schemaRenderer]]

type schemaRenderer struct {
Type string `json:"type" yaml:"type"`
XML *XML `json:"xml,omitempty" yaml:"xml,omitempty"`
ExternalDocs *externalDocsRenderer `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
AllOf []*renderer[schemaRenderer] `json:"allOf,omitempty" yaml:"allOf,omitempty"`
OneOf []*renderer[schemaRenderer] `json:"oneOf,omitempty" yaml:"oneOf,omitempty"`
AnyOf []*renderer[schemaRenderer] `json:"anyOf,omitempty" yaml:"anyOf,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
Items *renderer[schemaRenderer] `json:"items,omitempty" yaml:"items,omitempty"`
Properties *orderedmap.OrderedMap[string, *renderer[schemaRenderer]] `json:"properties,omitempty" yaml:"properties,omitempty"`
AdditionalProperties *renderer[schemaRenderer] `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
Minimum int `json:"minimum,omitempty" yaml:"minimum,omitempty"`
Maximum int `json:"maximum,omitempty" yaml:"maximum,omitempty"`
Enum []any `json:"enum,omitempty" yaml:"enum,omitempty"`
Type string `json:"type" yaml:"type"`
XML *XML `json:"xml,omitempty" yaml:"xml,omitempty"`
ExternalDocs *externalDocsRenderer `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
AllOf []*renderer[schemaRenderer] `json:"allOf,omitempty" yaml:"allOf,omitempty"`
OneOf []*renderer[schemaRenderer] `json:"oneOf,omitempty" yaml:"oneOf,omitempty"`
AnyOf []*renderer[schemaRenderer] `json:"anyOf,omitempty" yaml:"anyOf,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
Items *renderer[schemaRenderer] `json:"items,omitempty" yaml:"items,omitempty"`
Properties *properties `json:"properties,omitempty" yaml:"properties,omitempty"`
AdditionalProperties *renderer[schemaRenderer] `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
Minimum int `json:"minimum,omitempty" yaml:"minimum,omitempty"`
Maximum int `json:"maximum,omitempty" yaml:"maximum,omitempty"`
Enum []any `json:"enum,omitempty" yaml:"enum,omitempty"`
}

func (d *Document) newSchema(t reflect.Type) *Schema {
Expand All @@ -98,7 +100,7 @@ func NewSchema(t reflect.Type, title, desc web.LocaleStringer) *Schema {

var timeType = reflect.TypeFor[time.Time]()

// d 仅用于查找其关联的 components/schema 中是否存在相同名称的对象,如果存在则直接生成引用对象。
// d 仅用于查找其关联的 components/schemas 中是否存在相同名称的对象,如果存在则直接生成引用对象。
//
// desc 表示类型 t 的 Description 属性
// rootName 根结构体的名称,主要是为了解决子元素又引用了根元素的类型引起的循环引用。
Expand Down

0 comments on commit 3438969

Please sign in to comment.