spec

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2025 License: MIT Imports: 18 Imported by: 19

README ยถ

oaswrap/spec

CI codecov Go Reference Go Report Card License

oaswrap/spec lets you build OpenAPI 3.x specs in pure Go โ€” framework-agnostic and easy to integrate.

Describe your API operations, paths, and schemas once, then plug them into any router.
No handlers, no routing โ€” just pure OpenAPI generation.

Powered by swaggest/openapi-go for robust schema generation.

โœจ Features

  • โœ… Programmatically build OpenAPI 3.x specs in pure Go.
  • โœ… No runtime server โ€” only schema generation logic.
  • โœ… Designed for framework adapters (Gin, Echo, Fiber, etc.).
  • โœ… Supports struct tags for request/response models.
  • โœ… Export specs to JSON or YAML, and validate before publishing.

Need an integration? Check out these official adapters:

๐Ÿ“ฆ Installation

go get github.com/oaswrap/spec

๐Ÿš€ Usage Example

package main

import (
	"log"

	"github.com/oaswrap/spec"
	"github.com/oaswrap/spec/option"
)

func main() {
	// Create a new OpenAPI router with basic info and security scheme
	r := spec.NewRouter(
		option.WithTitle("My API"),
		option.WithVersion("1.0.0"),
		option.WithDescription("Example API"),
		option.WithServer("https://api.example.com"),
		option.WithSecurity("bearerAuth", option.SecurityHTTPBearer("Bearer")),
	)

	// Versioned API group
	v1 := r.Group("/api/v1")

	// Auth routes
	v1.Route("/auth", func(r spec.Router) {
		r.Post("/login",
			option.Summary("User Login"),
			option.Request(new(LoginRequest)),
			option.Response(200, new(Response[Token])),
		)

		r.Get("/me",
			option.Summary("Get Profile"),
			option.Security("bearerAuth"),
			option.Response(200, new(Response[User])),
		)
	}, option.GroupTags("Authentication"))

	// Generate the OpenAPI file
	if err := r.WriteSchemaTo("openapi.yaml"); err != nil {
		log.Fatal(err)
	}

	log.Println("โœ… OpenAPI schema generated at openapi.yaml")
}

// Example request & response structs

type LoginRequest struct {
	Username string `json:"username" required:"true"`
	Password string `json:"password" required:"true"`
}

type Token struct {
	AccessToken string `json:"access_token"`
}

type User struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

type Response[T any] struct {
	Status int `json:"status"`
	Data   T   `json:"data"`
}

โœจ Live example: View the generated spec on Rest.Wiki.

๐Ÿ“š Documentation

  • All core configuration, router, server, and security options are defined in the option package.
  • See the full API reference on pkg.go.dev for detailed usage, examples, and type definitions.
  • This library uses swaggest/openapi-go under the hood โ€” see its docs for advanced struct tagging and schema reflection tips.

๐Ÿ“„ License

This project is licensed under the MIT License.

๐Ÿค Contributing

PRs and issues are welcome! โค๏ธ
Made with care by Ahmad Faiz

Documentation ยถ

Index ยถ

Constants ยถ

This section is empty.

Variables ยถ

This section is empty.

Functions ยถ

This section is empty.

Types ยถ

type Generator ยถ

type Generator interface {
	Router

	// Config returns the OpenAPI configuration used by the Generator.
	Config() *specopenapi.Config

	// GenerateSchema generates the OpenAPI schema in the specified format.
	// By default, it generates YAML. Pass "json" to generate JSON instead.
	GenerateSchema(formats ...string) ([]byte, error)

	// MarshalYAML returns the OpenAPI specification marshaled as YAML.
	MarshalYAML() ([]byte, error)

	// MarshalJSON returns the OpenAPI specification marshaled as JSON.
	MarshalJSON() ([]byte, error)

	// Validate checks whether the OpenAPI specification is valid.
	Validate() error

	// WriteSchemaTo writes the OpenAPI schema to a file.
	// The format is inferred from the file extension: ".yaml" for YAML, ".json" for JSON.
	WriteSchemaTo(path string) error
}

Generator defines an interface for building and exporting OpenAPI specifications.

func NewGenerator ยถ

func NewGenerator(opts ...option.OpenAPIOption) Generator

NewGenerator returns a new Generator instance using the given OpenAPI options.

It initializes the OpenAPI reflector and configuration.

func NewRouter ยถ added in v0.1.3

func NewRouter(opts ...option.OpenAPIOption) Generator

NewRouter returns a new Router instance using the given OpenAPI options.

It is equivalent to NewGenerator.

See also: NewGenerator.

type Route ยถ added in v0.1.3

type Route interface {
	// With applies additional operation options to the route.
	With(opts ...option.OperationOption) Route
}

Route represents a single API route in the OpenAPI specification.

type Router ยถ added in v0.1.3

type Router interface {
	// Get registers a GET operation for the given path and options.
	Get(path string, opts ...option.OperationOption) Route

	// Post registers a POST operation for the given path and options.
	Post(path string, opts ...option.OperationOption) Route

	// Put registers a PUT operation for the given path and options.
	Put(path string, opts ...option.OperationOption) Route

	// Delete registers a DELETE operation for the given path and options.
	Delete(path string, opts ...option.OperationOption) Route

	// Patch registers a PATCH operation for the given path and options.
	Patch(path string, opts ...option.OperationOption) Route

	// Options registers an OPTIONS operation for the given path and options.
	Options(path string, opts ...option.OperationOption) Route

	// Head registers a HEAD operation for the given path and options.
	Head(path string, opts ...option.OperationOption) Route

	// Trace registers a TRACE operation for the given path and options.
	Trace(path string, opts ...option.OperationOption) Route

	// Add registers an operation for the given HTTP method, path, and options.
	Add(method, path string, opts ...option.OperationOption) Route

	// Route registers a nested route under the given pattern.
	// The provided function receives a Router to define sub-routes.
	Route(pattern string, fn func(router Router), opts ...option.GroupOption) Router

	// Group creates a new sub-router with the given path prefix and group options.
	Group(pattern string, opts ...option.GroupOption) Router

	// Use applies one or more group options to the router.
	Use(opts ...option.GroupOption) Router
}

Router defines methods for registering API routes and operations in an OpenAPI specification. It lets you describe HTTP methods, paths, and options.

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
internal
module
specui module
Package option provides functional options for configuring OpenAPI generation, including server setup, group settings, operation options, and reflector behavior.
Package option provides functional options for configuring OpenAPI generation, including server setup, group settings, operation options, and reflector behavior.
pkg

Jump to

Keyboard shortcuts

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