spec

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2025 License: MIT Imports: 10 Imported by: 19

README

oaswrap/spec

oaswrap/spec is a lightweight, framework-agnostic OpenAPI 3.x specification builder for Go.
It provides the core logic to describe your API operations, paths, parameters, and schemas — without locking you into a specific web framework.

✨ Features

  • ✅ Build OpenAPI 3.x specs programmatically in Go.
  • ✅ No runtime web server logic — just spec generation.
  • ✅ Designed to be wrapped by framework adapters like Fiber, Gin, Echo, etc.

📦 Installation

go get github.com/oaswrap/spec

📖 Documentation

For detailed usage instructions, check out the documentation.

📄 License

This project is licensed under the MIT License.

Documentation

Index

Constants

View Source
const (
	SecuritySchemeAPIKeyInQuery  = SecuritySchemeAPIKeyIn("query")
	SecuritySchemeAPIKeyInHeader = SecuritySchemeAPIKeyIn("header")
	SecuritySchemeAPIKeyInCookie = SecuritySchemeAPIKeyIn("cookie")
)

SecuritySchemeAPIKeyIn values enumeration.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	OpenAPIVersion  string // OpenAPI version, e.g., "3.1.0"
	Title           string
	Version         string
	Description     *string
	Servers         []Server
	SecuritySchemes map[string]*SecurityScheme

	DisableOpenAPI bool
	BaseURL        string
	DocsPath       string
	SwaggerConfig  *SwaggerConfig

	Logger Logger
}

Config holds the configuration for OpenAPI documentation generation.

type Generator

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

Generator is responsible for generating OpenAPI documentation.

func NewGenerator

func NewGenerator(configs ...*Config) (*Generator, error)

NewGenerator creates a new Generator instance with the provided configuration.

func (*Generator) AddOperation

func (g *Generator) AddOperation(ctx OperationContext) error

AddOperation adds an operation to the OpenAPI documentation.

func (*Generator) GenerateSchema

func (g *Generator) GenerateSchema(formats ...string) ([]byte, error)

GenerateSchema generates the OpenAPI schema in the specified format (JSON or YAML).

func (*Generator) NewOperationContext

func (g *Generator) NewOperationContext(method, path string) (OperationContext, error)

NewOperationContext creates a new operation context for the specified method and path.

func (*Generator) WriteSchemaTo

func (g *Generator) WriteSchemaTo(path string) error

WriteSchemaTo writes the OpenAPI schema to the specified file path.

type Logger

type Logger interface {
	Printf(format string, v ...any)
}

type NoopLogger

type NoopLogger struct{}

func (NoopLogger) Printf

func (l NoopLogger) Printf(format string, v ...any)

type OAuthFlows

type OAuthFlows struct {
	Implicit          *OAuthFlowsDefsImplicit
	Password          *OAuthFlowsDefsPassword
	ClientCredentials *OAuthFlowsDefsClientCredentials
	AuthorizationCode *OAuthFlowsDefsAuthorizationCode
	MapOfAnything     map[string]any // Key must match pattern: `^x-`.
}

OAuthFlows structure is generated from "#/$defs/oauth-flows".

type OAuthFlowsDefsAuthorizationCode

type OAuthFlowsDefsAuthorizationCode struct {
	// Format: uri.
	// Required.
	AuthorizationURL string
	// Format: uri.
	// Required.
	TokenURL      string
	RefreshURL    *string           // Format: uri.
	Scopes        map[string]string // Required.
	MapOfAnything map[string]any    // Key must match pattern: `^x-`.
}

OAuthFlowsDefsAuthorizationCode structure is generated from "#/$defs/oauth-flows/$defs/authorization-code".

type OAuthFlowsDefsClientCredentials

type OAuthFlowsDefsClientCredentials struct {
	// Format: uri.
	// Required.
	TokenURL      string
	RefreshURL    *string           // Format: uri.
	Scopes        map[string]string // Required.
	MapOfAnything map[string]any    // Key must match pattern: `^x-`.
}

OAuthFlowsDefsClientCredentials structure is generated from "#/$defs/oauth-flows/$defs/client-credentials".

type OAuthFlowsDefsImplicit

type OAuthFlowsDefsImplicit struct {
	// Format: uri.
	// Required.
	AuthorizationURL string
	RefreshURL       *string           // Format: uri.
	Scopes           map[string]string // Required.
	MapOfAnything    map[string]any    // Key must match pattern: `^x-`.
}

OAuthFlowsDefsImplicit structure is generated from "#/$defs/oauth-flows/$defs/implicit".

type OAuthFlowsDefsPassword

type OAuthFlowsDefsPassword struct {
	// Format: uri.
	// Required.
	TokenURL      string
	RefreshURL    *string           // Format: uri.
	Scopes        map[string]string // Required.
	MapOfAnything map[string]any    // Key must match pattern: `^x-`.
}

OAuthFlowsDefsPassword structure is generated from "#/$defs/oauth-flows/$defs/password".

type OperationContext

type OperationContext interface {
	openapi.OperationContext
	OpenAPIOperationContext() openapi.OperationContext
}

type Reflector

type Reflector interface {
	AddOperation(oc OperationContext) error
	NewOperationContext(method, path string) (OperationContext, error)
	Spec() Spec
}

type SecurityScheme

type SecurityScheme struct {
	Description   *string
	APIKey        *SecuritySchemeAPIKey
	HTTPBearer    *SecuritySchemeHTTPBearer
	OAuth2        *SecuritySchemeOAuth2
	MapOfAnything map[string]any // Key must match pattern: `^x-`.
}

SecurityScheme structure is generated from "#/$defs/security-scheme".

type SecuritySchemeAPIKey

type SecuritySchemeAPIKey struct {
	Name string                 // Required.
	In   SecuritySchemeAPIKeyIn // Required.
}

SecuritySchemeAPIKey structure is generated from "#/$defs/security-scheme/$defs/type-apikey".

type SecuritySchemeAPIKeyIn

type SecuritySchemeAPIKeyIn string

SecuritySchemeAPIKeyIn is an enum type.

type SecuritySchemeHTTPBearer

type SecuritySchemeHTTPBearer struct {
	// Value must match pattern: `^[Bb][Ee][Aa][Rr][Ee][Rr]$`.
	// Required.
	Scheme       string  `json:"scheme"`
	BearerFormat *string `json:"bearerFormat,omitempty"`
}

SecuritySchemeHTTPBearer structure is generated from "#/$defs/security-scheme/$defs/type-http-bearer".

type SecuritySchemeOAuth2

type SecuritySchemeOAuth2 struct {
	Flows OAuthFlows // Required.
}

SecuritySchemeOAuth2 structure is generated from "#/$defs/security-scheme/$defs/type-oauth2".

type Server

type Server struct {
	// Format: uri-reference.
	// Required.
	URL           string
	Description   *string
	Variables     map[string]ServerVariable
	MapOfAnything map[string]any // Key must match pattern: `^x-`.
}

Server structure is generated from "#/$defs/server".

type ServerVariable

type ServerVariable struct {
	Enum          []string
	Default       string // Required.
	Description   *string
	MapOfAnything map[string]any // Key must match pattern: `^x-`.
}

ServerVariable structure is generated from "#/$defs/server-variable".

type Spec

type Spec interface {
	MarshalYAML() ([]byte, error)
	MarshalJSON() ([]byte, error)
}

type SwaggerConfig

type SwaggerConfig struct {
	ShowTopBar         bool
	HideCurl           bool
	JsonEditor         bool
	PreAuthorizeApiKey map[string]string

	// SettingsUI contains keys and plain javascript values of SwaggerUIBundle configuration.
	// Overrides default values.
	// See https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ for available options.
	SettingsUI map[string]string

	// Proxy enables proxying requests through swgui handler.
	// Can be useful if API is not directly available due to CORS policy.
	Proxy bool
}

SwaggerConfig holds the configuration for Swagger UI.

Directories

Path Synopsis
adapter
chiopenapi module
echoopenapi module
fiberopenapi module
ginopenapi module
httpopenapi module
muxopenapi module
adapters
chiopenapi module
echoopenapi module
fiberopenapi module
ginopenapi module
httpopenapi module
module
specui module
pkg

Jump to

Keyboard shortcuts

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