example

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package example provides OpenAPI Example Object support.

Examples demonstrate expected request/response formats for API documentation. They can contain inline values or reference external URLs.

Basic usage:

import "github.com/talav/openapi/example"

// Create an inline example
example.New("user-found", map[string]any{"id": 42, "status": "active"})

// Add descriptive metadata
example.New("error-case", map[string]any{"error": "not found"},
	example.WithSummary("Resource not found"),
	example.WithDescription("Returned when the requested resource does not exist"),
)

// Reference an external example file
example.NewExternal("full-dataset", "https://example.com/data/full.json")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Example

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

Example represents an OpenAPI Example Object. https://spec.openapis.org/oas/v3.1.0#example-object

Examples contain either an inline value or an external URL reference. Per the spec, these two options are mutually exclusive.

func New

func New(name string, value any, opts ...Option) Example

New creates an example with an inline value.

The name serves as the key in the examples map and must be unique within the parent request body or response object.

The value will be serialized to JSON in the generated OpenAPI document. Use functional options to add summary and description metadata.

Examples:

example.New("success", map[string]any{"status": "ok"})
example.New("with-details", data, example.WithSummary("Detailed response"))

func NewExternal

func NewExternal(name, url string, opts ...Option) Example

NewExternal creates an example that references an external URL.

This is useful for large examples that would bloat the specification, binary or XML content that cannot be embedded, or examples shared across multiple API specifications.

Examples:

example.NewExternal("large-payload", "https://example.com/samples/large.json")
example.NewExternal("xml-sample", "https://example.com/samples/data.xml",
	example.WithSummary("XML response format"))

func (Example) Description

func (example Example) Description() string

Description returns the detailed description.

func (Example) ExternalValue

func (example Example) ExternalValue() string

ExternalValue returns the external URL, or empty string for inline examples.

func (Example) IsExternal

func (example Example) IsExternal() bool

IsExternal reports whether this example references an external URL.

func (Example) Name

func (example Example) Name() string

Name returns the example's unique identifier.

func (Example) Summary

func (example Example) Summary() string

Summary returns the short description.

func (Example) Value

func (example Example) Value() any

Value returns the inline example value, or nil for external examples.

type Option

type Option func(*Example)

Option configures an Example using the functional options pattern.

func WithDescription

func WithDescription(description string) Option

WithDescription adds a detailed explanation to the example. Supports CommonMark formatting for rich text documentation.

func WithSummary

func WithSummary(summary string) Option

WithSummary adds a short description to the example. This typically appears as a title in documentation tools like Swagger UI.

Jump to

Keyboard shortcuts

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