openapi

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 25 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const MaxSpecBytes = 50 << 20

MaxSpecBytes bounds a spec fetched from a URL: the read used to be unbounded, so a hostile or broken endpoint could stream until memory ran out.

Variables

This section is empty.

Functions

func CountOperations

func CountOperations(data []byte, opts Options) (int64, error)

CountOperations counts the number of operations in an OpenAPI spec. If UseSpecServers is true, multiplies by the number of servers.

func DetectFormatFromContent

func DetectFormatFromContent(data []byte) string

DetectFormatFromContent detects format by examining content.

func DetectFormatFromContentType

func DetectFormatFromContentType(contentType string) string

DetectFormatFromContentType detects format from Content-Type header.

func IsOpenAPISpec

func IsOpenAPISpec(data []byte) bool

IsOpenAPISpec returns true if data looks like an OpenAPI 3.x or Swagger 2.x spec.

func LoadSpec

func LoadSpec(input string) ([]byte, string, error)

LoadSpec loads an OpenAPI/Swagger spec from a file path or URL. Returns the content bytes and detected format extension (.json, .yaml, .yml).

func LoadSpecWithClient added in v0.5.7

func LoadSpecWithClient(input string, client *http.Client) ([]byte, string, error)

LoadSpecWithClient is LoadSpec fetching a URL through client (nil = a client honouring the proxy environment). It used http.DefaultClient, which ignored --proxy.

func ParseForServer

func ParseForServer(data []byte, opts ServerOptions, callback func(*ServerScanRequest)) error

ParseForServer parses an OpenAPI spec and calls callback with ServerScanRequest.

func ParseOpenAPI

func ParseOpenAPI(data []byte, opts Options, callback ResultCallback) error

ParseOpenAPI parses an OpenAPI 3.0 spec and generates HttpRequestResponse.

func ParseSwagger

func ParseSwagger(data []byte, ext string, opts Options, callback ResultCallback) error

ParseSwagger parses a Swagger 2.0 spec, converts it to OpenAPI 3.0, and generates HttpRequestResponse. It auto-detects wrapper structures (swaggerDoc) and spec version (OpenAPI 3.x vs Swagger 2.x).

func ParseSwaggerForServer

func ParseSwaggerForServer(data []byte, ext string, opts ServerOptions, callback func(*ServerScanRequest)) error

ParseSwaggerForServer parses a Swagger spec and calls callback with ServerScanRequest.

func SpecServerURLs added in v0.5.7

func SpecServerURLs(input string) ([]string, error)

SpecServerURLs returns the absolute server URLs a spec declares, in spec order, with server variables replaced by their defaults. Relative server URLs are skipped: they name no host. Swagger 2.0 host/basePath/schemes are converted the way ParseSwagger converts them.

Types

type Format

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

Format implements formats.Format interface for OpenAPI/Swagger specs.

func New

func New() *Format

New creates a new OpenAPI Format.

func (*Format) Count

func (f *Format) Count(input string) (int64, error)

Count returns the number of operations in the OpenAPI spec. If UseSpecServers is true, multiplies by number of servers.

func (*Format) Name

func (f *Format) Name() string

Name returns the name of the format.

func (*Format) Parse

func (f *Format) Parse(input string, callback formats.ParseReqRespCallback) error

Parse parses an OpenAPI/Swagger spec file and calls callback for each request.

func (*Format) SetOpenAPIOptions

func (f *Format) SetOpenAPIOptions(opts Options)

SetOpenAPIOptions sets OpenAPI-specific options.

func (*Format) SetOptions

func (f *Format) SetOptions(options formats.InputFormatOptions)

SetOptions sets the generic format options.

func (*Format) Stats added in v0.5.3

func (f *Format) Stats() ParseStats

Stats reports the outcome of the last Parse.

type Options

type Options struct {
	// BaseURL overrides servers in spec (e.g., https://api.example.com)
	BaseURL string

	// UseSpecServers allows using servers defined in spec when BaseURL is not provided
	UseSpecServers bool
	// HTTPClient fetches a spec given as a URL (nil = proxy-environment
	// client). Callers pass one built from the scan's --proxy.
	HTTPClient *http.Client

	// PreserveSpecServerPath, when BaseURL is set, keeps the scheme/host from BaseURL
	// (so the in-scope target host is enforced) but uses the path prefix declared by
	// the spec's own servers / Swagger basePath instead of discarding it. Without this,
	// a spec served at host/openapi.json that declares servers:[{url:"https://x/api/v3"}]
	// would generate requests to host/<op> (404 noise) instead of host/api/v3/<op>.
	// Used by spec auto-ingestion (discovery + api-spec-ingest); the explicit
	// `import`/`scan -i` paths keep the full-override behaviour.
	PreserveSpecServerPath bool

	// Headers are custom headers to add to all requests
	Headers map[string]string

	// Variables are parameter values (e.g., api_key=abc123)
	Variables map[string]string

	// DefaultFallbackValue is used for required params without examples
	DefaultFallbackValue string

	// RequiredOnly only uses required fields when generating requests
	RequiredOnly bool

	// SkipFormatValidation is used to skip format validation
	SkipFormatValidation bool

	// FieldTypeDefaults provides configurable default values per field type.
	// Used when OpenAPI specs lack examples. Keys are type names (e.g. "email", "uuid").
	FieldTypeDefaults map[string][]string

	// Stats, when set, receives what the parse produced and what it could not
	// (never-silent: operations dropped are counted, not just debug-logged).
	Stats *ParseStats
}

Options contains options for parsing OpenAPI specs.

type ParseStats added in v0.5.3

type ParseStats struct {
	// Emitted is the number of requests handed to the callback.
	Emitted int
	// SynthesizedParams counts required parameters with no example, default or
	// usable schema that were filled with a type-based placeholder instead of
	// dropping their operation.
	SynthesizedParams int
	// SkippedOperations counts operations (or body variants) that produced no
	// request at all.
	SkippedOperations int
}

ParseStats counts the outcome of one spec parse.

type ResultCallback

type ResultCallback func(*httpmsg.HttpRequestResponse) bool

ResultCallback is called for each generated HttpRequestResponse.

type ServerOptions

type ServerOptions struct {
	Options

	// EnableModules is the list of modules to enable for scan requests
	EnableModules []string

	// WebhookURL is the webhook URL for results
	WebhookURL string
}

ServerOptions extends Options with server-mode specific fields.

type ServerScanRequest

type ServerScanRequest struct {
	URL           string
	RawRequest    string
	EnableModules []string
	WebhookURL    string
}

ServerScanRequest is a lightweight scan request type used by the adapter to avoid importing pkg/server/api (which would create a cycle).

Jump to

Keyboard shortcuts

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