factory

package
v2.1.93 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

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

Factory generates mock requests and responses based on an OpenAPI spec. It wraps the registry and generator for convenient programmatic use.

func NewFactory

func NewFactory(specBytes []byte, opts ...FactoryOption) (*Factory, error)

NewFactory creates a Factory from raw OpenAPI spec bytes. Default replacement contexts (common, fake, words) are loaded automatically.

func (*Factory) FindOperation added in v2.1.72

func (f *Factory) FindOperation(path, method string) *schema.Operation

FindOperation returns the parsed operation for the given spec path and method.

func (*Factory) MatchPath added in v2.1.72

func (f *Factory) MatchPath(requestPath, method string) (string, bool)

MatchPath resolves a concrete request path (e.g., /users/42) to the corresponding OpenAPI spec path pattern (e.g., /users/{id}). Returns the spec path and true if a match is found.

func (*Factory) Operations

func (f *Factory) Operations() []typedef.RouteInfo

Operations returns route info for all available operations.

func (*Factory) Request

func (f *Factory) Request(path, method string, ctx map[string]any) (schema.GeneratedRequest, error)

Request generates a mock request for the given spec path and method. Returns a GeneratedRequest with path (param values filled), contentType, headers, and body. ctx is an optional replacement context for controlling generated values.

func (*Factory) RequestBody

func (f *Factory) RequestBody(path, method string, ctx map[string]any) (json.RawMessage, error)

RequestBody generates just the request body bytes for the given spec path and method. ctx is an optional replacement context for controlling generated values.

func (*Factory) Response

func (f *Factory) Response(path, method string, ctx map[string]any) (schema.ResponseData, error)

Response generates a mock response for the given spec path and method. path should be the OpenAPI path pattern (e.g., "/users/{id}"). ctx is an optional replacement context for controlling generated values.

func (*Factory) ResponseBody

func (f *Factory) ResponseBody(path, method string, ctx map[string]any) (json.RawMessage, error)

ResponseBody generates just the response body bytes for the given spec path and method. ctx is an optional replacement context for controlling generated values.

func (*Factory) ResponseBodyFromRequest

func (f *Factory) ResponseBodyFromRequest(r *http.Request, ctx map[string]any) (json.RawMessage, error)

ResponseBodyFromRequest generates response body bytes matching the given HTTP request. It automatically matches the request path (e.g., /users/42) to the corresponding spec path pattern (e.g., /users/{id}). ctx is an optional replacement context for controlling generated values.

func (*Factory) ResponseFromRequest

func (f *Factory) ResponseFromRequest(r *http.Request, ctx map[string]any) (schema.ResponseData, error)

ResponseFromRequest generates a mock response matching the given HTTP request. It automatically matches the request path (e.g., /users/42) to the corresponding spec path pattern (e.g., /users/{id}) and generates a response. ctx is an optional replacement context for controlling generated values.

type FactoryOption

type FactoryOption func(*factoryConfig)

FactoryOption configures a Factory.

func WithCodegenConfig

func WithCodegenConfig(cfg codegen.Configuration) FactoryOption

WithCodegenConfig sets the codegen configuration used for spec parsing. When not provided, the default codegen configuration is used.

func WithServiceContext

func WithServiceContext(contextYAML []byte) FactoryOption

WithServiceContext sets a service-specific context YAML for value replacements.

func WithSpecOptions

func WithSpecOptions(opts *config.SpecOptions) FactoryOption

WithSpecOptions sets OpenAPI spec parsing options.

Jump to

Keyboard shortcuts

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