oa

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeBoolean = "boolean"
	TypeInteger = "integer"
	TypeNumber  = "number"
	TypeString  = "string"
	TypeArray   = "array"
	TypeObject  = "object"
)

JSON Schema 类型常量

View Source
const DocsHTML = `` /* 593-byte string literal not displayed */
View Source
const Version = "3.1.1"

Variables

This section is empty.

Functions

func DefaultSchemaNamer

func DefaultSchemaNamer(t reflect.Type) string

DefaultSchemaNamer 根据 “去域名 + 取最后两级” 策略生成名称

func TypeNullable

func TypeNullable(typ any, t ...reflect.Type) any

Types

type AddOperation

type AddOperation func(*Operation)

type Components

type Components struct {
	Schemas         map[string]*Schema         `yaml:"schemas,omitempty"`
	SecuritySchemes map[string]*SecurityScheme `yaml:"securitySchemes,omitempty"`
}

type Config

type Config struct {
	// SchemaNamer 是一个函数,用于从 Go 类型生成其在 OpenAPI 组件中的唯一名称。
	SchemaNamer func(t reflect.Type) string
}

Config 持有 OpenAPI 生成过程中的所有可配置策略。

type Info

type Info struct {
	Title   string `yaml:"title"`
	Version string `yaml:"version"`
}

type MediaType

type MediaType struct {
	Schema *Schema `yaml:"schema,omitempty"`
}

type OpenAPI

type OpenAPI struct {
	OpenAPI    string               `yaml:"openapi"`
	Info       *Info                `yaml:"info"`
	Paths      map[string]*PathItem `yaml:"paths,omitempty"`
	Components *Components          `yaml:"components"`
	Security   []Requirement        `yaml:"security,omitempty"`
	Tags       []*Tag               `yaml:"tags,omitempty"`
	// contains filtered or unexported fields
}

func New

func New(c Config, f ...func(*OpenAPI)) *OpenAPI

func (*OpenAPI) Config

func (oa *OpenAPI) Config() Config

func (*OpenAPI) Register

func (oa *OpenAPI) Register(op *Operation, path, method string, handler any)

func (*OpenAPI) YAML

func (oa *OpenAPI) YAML() ([]byte, error)

YAML 返回 YAML 格式的 OpenAPI 规范

type Operation

type Operation struct {
	Summary     string               `yaml:"summary,omitempty"`
	Description string               `yaml:"description,omitempty"`
	Parameters  []*Param             `yaml:"parameters,omitempty"`
	RequestBody *RequestBody         `yaml:"requestBody,omitempty"`
	Responses   map[string]*Response `yaml:"responses,omitempty"`
	Security    []Requirement        `yaml:"security,omitempty"`
	Tags        []string             `yaml:"tags,omitempty"`
}

func (*Operation) Clone

func (o *Operation) Clone() *Operation

Clone 返回一份深度的 Operation 副本

type Param

type Param struct {
	Ref         string  `yaml:"$ref,omitempty"`
	Name        string  `yaml:"name,omitempty"`
	In          string  `yaml:"in,omitempty"` // "query", "header", "path", "cookie"
	Required    bool    `yaml:"required,omitempty"`
	Description string  `yaml:"description,omitempty"`
	Schema      *Schema `yaml:"schema,omitempty"`
}

type PathItem

type PathItem struct {
	Ref         string `yaml:"$ref,omitempty"`
	Summary     string `yaml:"summary,omitempty"`
	Description string `yaml:"description,omitempty"`

	Get     *Operation `yaml:"get,omitempty"`
	Put     *Operation `yaml:"put,omitempty"`
	Post    *Operation `yaml:"post,omitempty"`
	Delete  *Operation `yaml:"delete,omitempty"`
	Options *Operation `yaml:"options,omitempty"`
	Head    *Operation `yaml:"head,omitempty"`
	Patch   *Operation `yaml:"patch,omitempty"`
	Trace   *Operation `yaml:"trace,omitempty"`

	Parameters []*Param `yaml:"parameters,omitempty"`
}

type RequestBody

type RequestBody struct {
	Ref         string                `yaml:"$ref,omitempty"`
	Description string                `yaml:"description,omitempty"`
	Content     map[string]*MediaType `yaml:"content"`
	Required    bool                  `yaml:"required,omitempty"`
}

type Requirement

type Requirement map[string][]string

type Response

type Response struct {
	Ref         string                `yaml:"$ref,omitempty"`
	Description string                `yaml:"description,omitempty"`
	Headers     map[string]*Param     `yaml:"headers,omitempty"`
	Content     map[string]*MediaType `yaml:"content,omitempty"`
}

type Schema

type Schema struct {
	Type                 any                `yaml:"type,omitempty"`
	Title                string             `yaml:"title,omitempty"`
	Description          string             `yaml:"description,omitempty"`
	Ref                  string             `yaml:"$ref,omitempty"`
	Format               string             `yaml:"format,omitempty"`
	ContentEncoding      string             `yaml:"contentEncoding,omitempty"`
	Default              any                `yaml:"default,omitempty"`
	Items                *Schema            `yaml:"items,omitempty"`                // For arrays
	AdditionalProperties any                `yaml:"additionalProperties,omitempty"` // Schema or bool
	Properties           map[string]*Schema `yaml:"properties,omitempty"`
	Enum                 []any              `yaml:"enum,omitempty"`
	Required             []string           `yaml:"required,omitempty"`
}

type SecurityScheme

type SecurityScheme struct {
	Type         string `yaml:"type"` // "http", "apiKey", "oauth2"
	Description  string `yaml:"description,omitempty"`
	Name         string `yaml:"name,omitempty"`         // Header name for apiKey
	In           string `yaml:"in,omitempty"`           // "header" for apiKey
	Scheme       string `yaml:"scheme,omitempty"`       // "bearer" (for HTTP)
	BearerFormat string `yaml:"bearerFormat,omitempty"` // "JWT" (for bearer)
}

type Tag

type Tag struct {
	Name        string `yaml:"name"`
	Description string `yaml:"description,omitempty"`
}

Jump to

Keyboard shortcuts

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