openapi

package
v1.0.0-alpha.25 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseStructTags

func ParseStructTags(t reflect.Type) map[string]FieldInfo

ParseStructTags parses struct tags to generate field information.

Types

type Components

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

Components holds a set of reusable objects for different aspects of the OAS.

type Contact

type Contact struct {
	Name  string `json:"name,omitempty"`
	URL   string `json:"url,omitempty"`
	Email string `json:"email,omitempty"`
}

Contact information for the exposed API.

type FieldInfo

type FieldInfo struct {
	Name             string
	Description      string
	Example          interface{}
	Required         bool
	Format           string
	ContentMediaType string // For images: image/png, image/jpeg, etc.
	Minimum          *float64
	Maximum          *float64
	MinLength        *int
	MaxLength        *int
	Pattern          string
	Enum             []interface{}
}

FieldInfo contains OpenAPI information for a struct field.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator generates OpenAPI specifications from route metadata.

func NewGenerator

func NewGenerator(title, version string) *Generator

NewGenerator creates a new OpenAPI generator.

func (*Generator) Generate

func (g *Generator) Generate(routes []RouteInfo) *Specification

Generate generates the OpenAPI specification.

func (*Generator) WithBearerAuth

func (g *Generator) WithBearerAuth() *Generator

WithBearerAuth adds bearer authentication.

func (*Generator) WithDescription

func (g *Generator) WithDescription(description string) *Generator

WithDescription sets the API description.

func (*Generator) WithSecurityScheme

func (g *Generator) WithSecurityScheme(name string, scheme SecurityScheme) *Generator

WithSecurityScheme adds a custom security scheme.

func (*Generator) WithServer

func (g *Generator) WithServer(url, description string) *Generator

WithServer adds a server to the specification.

type Info

type Info struct {
	Title       string   `json:"title"`
	Description string   `json:"description,omitempty"`
	Version     string   `json:"version"`
	Contact     *Contact `json:"contact,omitempty"`
	License     *License `json:"license,omitempty"`
}

Info provides metadata about the API.

type License

type License struct {
	Name string `json:"name"`
	URL  string `json:"url,omitempty"`
}

License information for the exposed API.

type MediaType

type MediaType struct {
	Schema Schema `json:"schema"`
}

MediaType provides schema and examples for the media type identified by its key.

type Operation

type Operation struct {
	Summary     string                `json:"summary,omitempty"`
	Description string                `json:"description,omitempty"`
	OperationID string                `json:"operationId,omitempty"`
	Tags        []string              `json:"tags,omitempty"`
	Parameters  []Parameter           `json:"parameters,omitempty"`
	RequestBody *RequestBody          `json:"requestBody,omitempty"`
	Responses   map[string]Response   `json:"responses"`
	Deprecated  bool                  `json:"deprecated,omitempty"`
	Security    []map[string][]string `json:"security,omitempty"`
}

Operation describes a single API operation on a path.

type Parameter

type Parameter struct {
	Name        string `json:"name"`
	In          string `json:"in"` // query, path, header, cookie
	Description string `json:"description,omitempty"`
	Required    bool   `json:"required,omitempty"`
	Schema      Schema `json:"schema"`
}

Parameter describes a single operation parameter.

type PathItem

type PathItem map[string]Operation // key is method (get, post, etc.)

PathItem describes the operations available on a single path.

type RequestBody

type RequestBody struct {
	Description string               `json:"description,omitempty"`
	Required    bool                 `json:"required,omitempty"`
	Content     map[string]MediaType `json:"content"`
}

RequestBody describes a single request body.

type Response

type Response struct {
	Description string               `json:"description"`
	Content     map[string]MediaType `json:"content,omitempty"`
}

Response describes a single response from an API operation.

type RouteInfo

type RouteInfo interface {
	GetMethod() string
	GetPath() string
	GetName() string
	GetSummary() string
	GetDescription() string
	GetTags() []string
	GetMetadata() []interface{}
	IsOpenApiEnabled() bool
	GetApiSecurityRequirements() []map[string][]string
}

RouteInfo represents route information for OpenAPI generation.

type Schema

type Schema struct {
	Type             string            `json:"type,omitempty"`
	Description      string            `json:"description,omitempty"`
	Format           string            `json:"format,omitempty"`
	ContentMediaType string            `json:"contentMediaType,omitempty"` // For Base64 images, e.g., image/png
	Example          interface{}       `json:"example,omitempty"`
	Properties       map[string]Schema `json:"properties,omitempty"`
	Items            *Schema           `json:"items,omitempty"` // for array
	Ref              string            `json:"$ref,omitempty"`
	Enum             []interface{}     `json:"enum,omitempty"`
	Required         []string          `json:"required,omitempty"`
	Minimum          *float64          `json:"minimum,omitempty"`
	Maximum          *float64          `json:"maximum,omitempty"`
	MinLength        *int              `json:"minLength,omitempty"`
	MaxLength        *int              `json:"maxLength,omitempty"`
	Pattern          string            `json:"pattern,omitempty"`
	Nullable         bool              `json:"nullable,omitempty"`
}

Schema represents a schema object.

func ArraySchema

func ArraySchema(description string, itemSchema Schema) Schema

ArraySchema creates an array Schema.

func BoolSchema

func BoolSchema(description string, example ...bool) Schema

BoolSchema creates a boolean Schema.

func IntSchema

func IntSchema(description string, example ...int) Schema

IntSchema creates an integer Schema.

func NumberSchema

func NumberSchema(description string, example ...float64) Schema

NumberSchema creates a number Schema.

func ObjectSchema

func ObjectSchema(description string, builder func(*SchemaBuilder)) Schema

ObjectSchema creates an object Schema with properties.

func StringSchema

func StringSchema(description string, example ...string) Schema

StringSchema creates a string Schema.

type SchemaBuilder

type SchemaBuilder struct {
	// contains filtered or unexported fields
}

SchemaBuilder provides a fluent API for building JSON Schema.

func NewSchema

func NewSchema() *SchemaBuilder

NewSchema creates a new Schema builder. Default type is "object" with an empty properties map.

func (*SchemaBuilder) ArrayProperty

func (b *SchemaBuilder) ArrayProperty(name, description string, itemSchema Schema) *SchemaBuilder

ArrayProperty adds an array property (convenience method).

func (*SchemaBuilder) BoolProperty

func (b *SchemaBuilder) BoolProperty(name, description string, example ...bool) *SchemaBuilder

BoolProperty adds a boolean property (convenience method).

func (*SchemaBuilder) Build

func (b *SchemaBuilder) Build() Schema

Build constructs the final Schema.

func (*SchemaBuilder) Description

func (b *SchemaBuilder) Description(desc string) *SchemaBuilder

Description sets the description.

func (*SchemaBuilder) Enum

func (b *SchemaBuilder) Enum(values ...interface{}) *SchemaBuilder

Enum sets the enum values.

func (*SchemaBuilder) Example

func (b *SchemaBuilder) Example(ex interface{}) *SchemaBuilder

Example sets the example value.

func (*SchemaBuilder) Format

func (b *SchemaBuilder) Format(format string) *SchemaBuilder

Format sets the format (email, date-time, uri, byte, binary, etc.).

func (*SchemaBuilder) ImageProperty

func (b *SchemaBuilder) ImageProperty(name, description, mediaType string, example ...string) *SchemaBuilder

ImageProperty adds a Base64 encoded image property (convenience method). This creates a string property with format: byte and contentMediaType for proper Swagger UI display. Supported mediaType values: image/png, image/jpeg, image/gif, image/webp, image/svg+xml. Example: b.ImageProperty("avatar", "用户头像", "image/png", "iVBORw0KGgo...")

func (*SchemaBuilder) IntProperty

func (b *SchemaBuilder) IntProperty(name, description string, example ...int) *SchemaBuilder

IntProperty adds an integer property (convenience method).

func (*SchemaBuilder) Max

func (b *SchemaBuilder) Max(max float64) *SchemaBuilder

Max sets the maximum value (for numbers).

func (*SchemaBuilder) MaxLength

func (b *SchemaBuilder) MaxLength(max int) *SchemaBuilder

MaxLength sets the maximum length (for strings).

func (*SchemaBuilder) Min

func (b *SchemaBuilder) Min(min float64) *SchemaBuilder

Min sets the minimum value (for numbers).

func (*SchemaBuilder) MinLength

func (b *SchemaBuilder) MinLength(min int) *SchemaBuilder

MinLength sets the minimum length (for strings).

func (*SchemaBuilder) Nullable

func (b *SchemaBuilder) Nullable(nullable bool) *SchemaBuilder

Nullable marks the schema as nullable.

func (*SchemaBuilder) NumberProperty

func (b *SchemaBuilder) NumberProperty(name, description string, example ...float64) *SchemaBuilder

NumberProperty adds a number property (convenience method).

func (*SchemaBuilder) ObjectProperty

func (b *SchemaBuilder) ObjectProperty(name, description string, builder func(*SchemaBuilder)) *SchemaBuilder

ObjectProperty adds an object property (convenience method). The builder function allows nested schema definition.

func (*SchemaBuilder) Pattern

func (b *SchemaBuilder) Pattern(pattern string) *SchemaBuilder

Pattern sets the regex pattern.

func (*SchemaBuilder) Property

func (b *SchemaBuilder) Property(name string, schema Schema) *SchemaBuilder

Property adds an object property.

func (*SchemaBuilder) Required

func (b *SchemaBuilder) Required(fields ...string) *SchemaBuilder

Required sets required fields.

func (*SchemaBuilder) StringProperty

func (b *SchemaBuilder) StringProperty(name, description string, example ...string) *SchemaBuilder

StringProperty adds a string property (convenience method).

func (*SchemaBuilder) Type

func (b *SchemaBuilder) Type(t string) *SchemaBuilder

Type sets the Schema type (object, array, string, integer, number, boolean).

type SecurityScheme

type SecurityScheme struct {
	Type         string `json:"type"` // apiKey, http, oauth2, openIdConnect
	Description  string `json:"description,omitempty"`
	Name         string `json:"name,omitempty"`         // for apiKey
	In           string `json:"in,omitempty"`           // for apiKey (query, header, cookie)
	Scheme       string `json:"scheme,omitempty"`       // for http (bearer, basic)
	BearerFormat string `json:"bearerFormat,omitempty"` // for http bearer
}

SecurityScheme defines a security scheme that can be used by the operations.

type Server

type Server struct {
	URL         string `json:"url"`
	Description string `json:"description,omitempty"`
}

Server represents a server.

type Specification

type Specification struct {
	OpenAPI    string                `json:"openapi"`
	Info       Info                  `json:"info"`
	Servers    []Server              `json:"servers,omitempty"`
	Paths      map[string]PathItem   `json:"paths"`
	Components Components            `json:"components,omitempty"`
	Security   []map[string][]string `json:"security,omitempty"`
	Tags       []Tag                 `json:"tags,omitempty"`
}

Specification represents an OpenAPI 3.0.3 specification.

type Tag

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

Tag adds metadata to a single tag.

Jump to

Keyboard shortcuts

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