openapi

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package openapi generated by go-bindata. sources: internal/static/favicon.ico internal/static/favicon.png internal/static/redoc.standalone.js internal/static/swagger-ui-bundle.js internal/static/swagger-ui.css

Index

Constants

View Source
const (
	SwaggerCssName    = "swagger-ui.css"
	FaviconName       = "favicon.png"
	FaviconIcoName    = "favicon.ico"
	SwaggerJsName     = "swagger-ui-bundle.js"
	RedocJsName       = "redoc.standalone.js"
	JsonUrl           = "openapi.json"
	SwaggerFaviconUrl = "https://fastapi.tiangolo.com/img/" + FaviconName
	SwaggerCssUrl     = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/" + SwaggerCssName
	SwaggerJsUrl      = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/" + SwaggerJsName
	RedocJsUrl        = "https://cdn.jsdelivr.net/npm/redoc@next/bundles/" + RedocJsName
)

用于swagger的一些静态文件,来自FastApi

View Source
const (
	PathParamPrefix         = ":" // 路径参数起始字符
	PathSeparator           = "/" // 路径分隔符
	OptionalPathParamSuffix = "?" // 可选路径参数结束字符
)
View Source
const (
	MIMETextXML                    string = "text/xml"
	MIMETextHTML                   string = "text/html"
	MIMETextPlain                  string = "text/plain"
	MIMETextCSS                    string = "text/css"
	MIMETextJavaScript             string = "text/javascript"
	MIMEApplicationXML             string = "application/xml"
	MIMEApplicationJSON            string = "application/json"
	MIMEApplicationForm            string = "application/x-www-form-urlencoded"
	MIMEOctetStream                string = "application/octet-stream"
	MIMEMultipartForm              string = "multipart/form-data"
	MIMETextXMLCharsetUTF8         string = "text/xml; charset=utf-8"
	MIMETextHTMLCharsetUTF8        string = "text/html; charset=utf-8"
	MIMETextPlainCharsetUTF8       string = "text/plain; charset=utf-8"
	MIMETextCSSCharsetUTF8         string = "text/css; charset=utf-8"
	MIMETextJavaScriptCharsetUTF8  string = "text/javascript; charset=utf-8"
	MIMEApplicationXMLCharsetUTF8  string = "application/xml; charset=utf-8"
	MIMEApplicationJSONCharsetUTF8 string = "application/json; charset=utf-8"
)
View Source
const ApiVersion = "3.0.2"
View Source
const HeaderContentType = "Content-Type"

Variables

Functions

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("nonexistent") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func FastApiRoutePath

func FastApiRoutePath(path string) string

FastApiRoutePath 将 fiber.App 格式的路径转换成 FastApi 格式的路径

Example:
必选路径参数:
	Input: "/api/rcst/:no"
	Output: "/api/rcst/{no}"
可选路径参数:
	Input: "/api/rcst/:no?"
	Output: "/api/rcst/{no}"
常规路径:
	Input: "/api/rcst/no"
	Output: "/api/rcst/no"

func MakeOauth2RedirectHtml

func MakeOauth2RedirectHtml() string

func MakeRedocUiHtml

func MakeRedocUiHtml(title, openapiUrl, jsUrl, faviconUrl string) string

func MakeSwaggerUiHtml

func MakeSwaggerUiHtml(title, openapiUrl, jsUrl, cssUrl, faviconUrl string) string

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

Types

type APIKeyIn

type APIKeyIn string
const (
	APIKeyInQuery  APIKeyIn = "query"
	APIKeyInHeader APIKeyIn = "header"
	APIKeyInCookie APIKeyIn = "cookie"
)

type ComponentScheme

type ComponentScheme struct {
	Model *godantic.Metadata `json:"model" description:"模型定义"`
	Name  string             `json:"name" description:"模型名称,包含包名"`
}

ComponentScheme openapi 的模型文档部分

type Components

type Components struct {
	Scheme []*ComponentScheme `json:"scheme" description:"模型文档"`
}

Components openapi 的模型部分 需要重写序列化方法

func (*Components) AddModel

func (c *Components) AddModel(m *godantic.Metadata)

AddModel 添加一个模型文档

func (*Components) MarshalJSON

func (c *Components) MarshalJSON() ([]byte, error)

MarshalJSON 重载序列化方法

type Contact

type Contact struct {
	Name  string `json:"name" description:"姓名/名称"`
	Url   string `json:"url" description:"链接"`
	Email string `json:"email" description:"联系方式"`
}

Contact 联系方式, 显示在 info 字段内部 无需重写序列化方法

type Encoding

type Encoding struct {
	ContentType   string
	Style         string
	Headers       []Header
	Explode       bool
	AllowReserved bool
}

Encoding 编码(不常用)

type Header struct {
	Content     map[string]MediaType `json:"content" description:""`
	Description string               `json:"description" description:"说明"`
	Required    bool                 `json:"required" description:"是否必须"`
	Deprecated  bool                 `json:"deprecated" description:"是否禁用"`
}

Header 请求头参数,通常与认证相关(不常用)

type Info

type Info struct {
	Title          string  `json:"title" description:"显示在文档顶部的标题"`
	Version        string  `json:"version" description:"显示在标题右上角的程序版本号"`
	Description    string  `json:"description" description:"显示在标题下方的说明"`
	Contact        Contact `json:"contact" description:"联系方式"`
	License        License `json:"license" description:"许可证"`
	TermsOfService string  `json:"termsOfService" description:"服务条款(不常用)"`
}

Info 文档说明信息 无需重写序列化方法

type License

type License struct {
	Name string `json:"name" description:"名称"`
	Url  string `json:"url" description:"链接"`
}

License 权利证书, 显示在 info 字段内部 无需重写序列化方法

type Link struct {
	Description  string         `json:"description" description:"说明"`
	OperationRef string         `json:"operationRef" description:""`
	OperationId  string         `json:"operationId" description:"唯一ID"`
	Parameters   map[string]any `json:"parameters" description:"路由参数"`
	RequestBody  RequestBody    `json:"requestBody" description:"请求"`
	Server       Server         `json:"server" description:""`
}

func (Link) Alias

func (l Link) Alias() string

type MediaType

type MediaType struct {
	Encoding map[string]Encoding
}

MediaType 媒体类型(不常用)

type ModelContentSchema

type ModelContentSchema interface {
	SchemaType() godantic.OpenApiDataType
	Schema() map[string]any
	SchemaName(exclude ...bool) string
}

type OpenApi

type OpenApi struct {
	Info       *Info       `json:"info,omitempty" description:"联系信息"`
	Components *Components `json:"components" description:"模型文档"`
	Paths      *Paths      `json:"paths" description:"路由列表,同一路由存在多个方法文档"`
	Version    string      `json:"openapi" description:"Open API版本号"`
	// contains filtered or unexported fields
}

OpenApi 模型类, 移除 FastApi 中不常用的属性

func NewOpenApi

func NewOpenApi(title, version, description string) *OpenApi

NewOpenApi 构造一个新的 OpenApi 文档

func (*OpenApi) AddDefinition

func (o *OpenApi) AddDefinition(meta *godantic.Metadata) *OpenApi

AddDefinition 添加一个模型文档

func (*OpenApi) QueryPathItem

func (o *OpenApi) QueryPathItem(path string) *PathItem

QueryPathItem 查询路由对象, 不存在则新建

func (*OpenApi) RecreateDocs

func (o *OpenApi) RecreateDocs() *OpenApi

RecreateDocs 重建Swagger 文档

func (*OpenApi) Schema

func (o *OpenApi) Schema() []byte

Schema Swagger 文档, 并非完全符合 OpenApi 文档规范

type Operation

type Operation struct {
	Tags        []string `json:"tags" description:"路由标签"`
	Summary     string   `json:"summary" description:"摘要描述"`
	Description string   `json:"description" description:"说明"`
	OperationId string   `json:"operationId,omitempty" description:"唯一ID"` // no use, keep
	// 路径参数和查询参数, 对于路径相同,方法不同的路由来说,其查询参数可以不一样,但其路径参数都是一样的
	Parameters []*Parameter `json:"parameters,omitempty" description:"路径参数和查询参数"`
	// 请求体,通过 MakeOperationRequestBody 构建
	RequestBody *RequestBody `json:"requestBody,omitempty" description:"请求体"`
	// 响应文档,对于任一个路由,均包含2个响应实例:200 + 422, 通过函数 MakeOperationResponses 构建
	Responses  []*Response `json:"responses" description:"响应体"`
	Deprecated bool        `json:"deprecated" description:"是否禁用"`
}

Operation 路由HTTP方法: Get/Post/Patch/Delete 等操作方法

func (*Operation) MarshalJSON

func (o *Operation) MarshalJSON() ([]byte, error)

MarshalJSON 重写序列化方法,修改 Responses 和 RequestBody 字段

type Parameter

type Parameter struct {
	Default any              `json:"default,omitempty" description:"默认值"`
	Schema  *ParameterSchema `json:"schema,omitempty" description:"字段模型"`
	ParameterBase
}

Parameter 路径参数或者查询参数

func QModelToParameter

func QModelToParameter(model *godantic.QModel) *Parameter

type ParameterBase

type ParameterBase struct {
	Name        string          `json:"name" description:"名称"`
	Description string          `json:"description,omitempty" description:"说明"`
	In          ParameterInType `json:"in" description:"参数位置"`
	Required    bool            `json:"required" description:"是否必须"`
	Deprecated  bool            `json:"deprecated" description:"是否禁用"`
}

ParameterBase 各种参数的基类

type ParameterInType

type ParameterInType string
const (
	InQuery  ParameterInType = "query"
	InHeader ParameterInType = "header"
	InPath   ParameterInType = "path"
	InCookie ParameterInType = "cookie"
)

type ParameterSchema

type ParameterSchema struct {
	Type  godantic.OpenApiDataType `json:"type" description:"数据类型"`
	Title string                   `json:"title"`
}

type PathItem

type PathItem struct {
	Get    *Operation `json:"get,omitempty" description:"GET方法"`
	Put    *Operation `json:"put,omitempty" description:"PUT方法"`
	Post   *Operation `json:"post,omitempty" description:"POST方法"`
	Patch  *Operation `json:"patch,omitempty" description:"PATCH方法"`
	Delete *Operation `json:"delete,omitempty" description:"DELETE方法"`
	Head   *Operation `json:"head,omitempty" description:"header方法"`
	Trace  *Operation `json:"trace,omitempty" description:"trace方法"`
	Path   string     `json:"-" description:"请求绝对路径"`
}

PathItem 路由选项,由于同一个路由可以存在不同的操作方法,因此此选项可以存在多个 Operation

type PathModelContent

type PathModelContent struct {
	Schema   ModelContentSchema `json:"schema" description:"模型引用文档"`
	MIMEType string             `json:"-"`
}

PathModelContent 路由中请求体 RequestBody 和 响应体中返回值 Responses 模型

func (*PathModelContent) MarshalJSON

func (p *PathModelContent) MarshalJSON() ([]byte, error)

MarshalJSON 自定义序列化

type Paths

type Paths struct {
	Paths []*PathItem
}

Paths openapi 的路由部分 需要重写序列化方法

func (*Paths) AddItem

func (p *Paths) AddItem(item *PathItem)

func (*Paths) MarshalJSON

func (p *Paths) MarshalJSON() ([]byte, error)

MarshalJSON 重载序列化方法

type Reference

type Reference struct {
	// 关联模型, 取值为 godantic.RefPrefix + modelName
	Name string `json:"-" description:"关联模型"`
}

Reference 引用模型,用于模型字段和路由之间互相引用

func (*Reference) MarshalJSON

func (r *Reference) MarshalJSON() ([]byte, error)

type RequestBody

type RequestBody struct {
	Content  *PathModelContent `json:"content,omitempty" description:"请求体模型"`
	Required bool              `json:"required" description:"是否必须"`
}

RequestBody 路由 请求体模型文档

func MakeOperationRequestBody

func MakeOperationRequestBody(model *godantic.Metadata) *RequestBody

MakeOperationRequestBody 将路由中的 *godantic.Metadata 转换成 openapi 的请求体 RequestBody

type Response

type Response struct {
	Content     *PathModelContent `json:"content" description:"返回值模型"`
	Description string            `json:"description" description:"说明"`
	StatusCode  int               `json:"-" description:"状态码"`
}

Response 路由返回体,包含了返回状态码,状态码说明和返回值模型

func MakeOperationResponses

func MakeOperationResponses(model *godantic.Metadata) []*Response

MakeOperationResponses 将路由中的 *godantic.Metadata 转换成 openapi 的返回体 []*Response

type Server

type Server struct {
	Url         string           `json:"url" description:"链接"`
	Description string           `json:"description" description:"说明"`
	Variables   []ServerVariable `json:"variables" description:""`
}

Server 服务器配置信息(不常用)

type ServerVariable

type ServerVariable struct {
	Default     string   `json:"default" description:"默认值"`
	Description string   `json:"description" description:"说明"`
	Enum        []string `json:"enum" description:"可选项"`
}

ServerVariable 服务器变量(不常用)

type Tag

type Tag struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

func (Tag) Schema

func (t Tag) Schema() map[string]string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL