fiberopenapi

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 8 Imported by: 0

README

fiberopenapi

CI codecov Go Reference Go Report Card License

fiberopenapi is a minimal adapter for the Fiber web framework that connects your routes to an OpenAPI 3.x specification using oaswrap/spec.

This package lets you define your Fiber routes and generate OpenAPI docs automatically — with simple, chainable options.

The underlying spec builder uses swaggest/openapi-go — a robust OpenAPI 3.0/3.1 generator for Go with support for struct tags.

✨ Features

  • ✅ Integrates Fiber routes with an OpenAPI spec.
  • ✅ Uses oaswrap/spec powered by swaggest/openapi-go.
  • ✅ Supports struct tags for request/response models — automatically maps field properties, examples, and validation.
  • ✅ Define common route metadata: summary, description, tags, security, request/response models.
  • ✅ Built-in validation and schema generation helpers.

📦 Installation

go get github.com/oaswrap/fiberopenapi

⚡️ Quick Start

package main

import (
	"log"

	"github.com/gofiber/fiber/v2"
	"github.com/oaswrap/fiberopenapi"
	"github.com/oaswrap/spec/option"
)

func main() {
	app := fiber.New()

	// Setup OpenAPI router
	r := fiberopenapi.NewRouter(app,
		option.WithTitle("Example API"),
		option.WithVersion("1.0.0"),
		option.WithDescription("Sample Fiber + OpenAPI"),
		option.WithDocsPath("/docs"),
	)

	// Define a simple group and route
	api := r.Group("/api")
	api.Post("/hello", helloHandler).With(
		option.Summary("Say Hello"),
		option.Request(new(HelloRequest)),
		option.Response(200, new(HelloResponse)),
	)

	// Write spec file (optional)
	if err := r.WriteSchemaTo("openapi.yaml"); err != nil {
		log.Fatal(err)
	}
	log.Println("✅ OpenAPI schema written to: openapi.yaml")

	log.Printf("🚀 OpenAPI docs available at: %s", "http://localhost:3000/docs")

	app.Listen(":3000")
}

// helloHandler handles the /api/hello route
func helloHandler(c *fiber.Ctx) error {
	var req HelloRequest
	if err := c.BodyParser(&req); err != nil {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
			"error": "Invalid request body",
		})
	}
	return c.JSON(HelloResponse{
		Message: "Hello, " + req.Name,
	})
}

// Request and response types
type HelloRequest struct {
	Name string `json:"name" example:"World" validate:"required"`
}

type HelloResponse struct {
	Message string `json:"message" example:"Hello, World"`
}

It will generate an OpenAPI spec for the defined routes

For more examples, check out the examples directory.

This example demonstrates:

  • Nested groups (/api/v1/auth, /api/v1/profile)
  • Multiple HTTP methods
  • With options for detailed spec generation
  • Use of struct tags to enrich models automatically.

📚 Documentation

🤝 Contributing

PRs and issues welcome!
If you find bugs or want to improve adapters or helpers, please open an issue.

📄 License

MIT

Made with ❤️ by oaswrap

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator added in v0.1.2

type Generator interface {
	Router

	// Validate checks for errors at OpenAPI router initialization.
	Validate() error

	// GenerateOpenAPISchema generates the OpenAPI schema in the specified format.
	GenerateOpenAPISchema(format ...string) ([]byte, error)
	// MarshalYAML marshals the OpenAPI schema to YAML format.
	MarshalYAML() ([]byte, error)
	// MarshalJSON marshals the OpenAPI schema to JSON format.
	MarshalJSON() ([]byte, error)

	// WriteSchemaTo writes the OpenAPI schema to a file.
	WriteSchemaTo(filePath string) error
}

Generator defines the interface for generating OpenAPI schemas.

func NewGenerator added in v0.1.2

func NewGenerator(r fiber.Router, opts ...option.OpenAPIOption) Generator

NewGenerator creates a new OpenAPI generator with the specified Fiber router and options.

It initializes the OpenAPI router and sets up the necessary routes for OpenAPI documentation.

func NewRouter

func NewRouter(r fiber.Router, opts ...option.OpenAPIOption) Generator

NewRouter creates a new OpenAPI router with the specified Fiber router and options.

It initializes the OpenAPI generator and sets up the necessary routes for OpenAPI documentation.

type Route

type Route interface {
	// Name sets the name for the route.
	Name(name string) Route
	// With applies the given options to the route.
	With(opts ...option.OperationOption) Route
}

Route represents a single route in the OpenAPI specification.

type Router

type Router interface {
	// Use applies middleware to the router.
	Use(args ...any) Router

	// Get registers a GET route.
	Get(path string, handler ...fiber.Handler) Route
	// Head registers a HEAD route.
	Head(path string, handler ...fiber.Handler) Route
	// Post registers a POST route.
	Post(path string, handler ...fiber.Handler) Route
	// Put registers a PUT route.
	Put(path string, handler ...fiber.Handler) Route
	// Patch registers a PATCH route.
	Patch(path string, handler ...fiber.Handler) Route
	// Delete registers a DELETE route.
	Delete(path string, handler ...fiber.Handler) Route
	// Connect registers a CONNECT route.
	Connect(path string, handler ...fiber.Handler) Route
	// Options registers an OPTIONS route.
	Options(path string, handler ...fiber.Handler) Route
	// Trace registers a TRACE route.
	Trace(path string, handler ...fiber.Handler) Route

	// Add registers a route with the specified method and path.
	Add(method, path string, handler ...fiber.Handler) Route
	// Static serves static files from the specified root directory.
	Static(prefix, root string, config ...fiber.Static) Router

	// Group creates a new sub-router with the specified prefix and handlers.
	// The prefix is prepended to all routes in the sub-router.
	Group(prefix string, handlers ...fiber.Handler) Router

	// Route creates a new sub-router with the specified prefix and applies options.
	Route(prefix string, fn func(router Router)) Router

	// With applies options to the router.
	// This allows you to configure tags, security, and visibility for the routes.
	With(opts ...option.GroupOption) Router
}

Router defines the interface for an OpenAPI router.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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