inference

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package inference provides algorithms for inferring OpenAPI schemas from HTTP traffic.

Index

Constants

View Source
const (
	TypeString  = "string"
	TypeInteger = "integer"
	TypeNumber  = "number"
	TypeBoolean = "boolean"
	TypeArray   = "array"
	TypeObject  = "object"
)

Type constants

View Source
const (
	FormatUUID     = "uuid"
	FormatEmail    = "email"
	FormatDateTime = "date-time"
	FormatDate     = "date"
	FormatTime     = "time"
	FormatURI      = "uri"
	FormatIPv4     = "ipv4"
	FormatIPv6     = "ipv6"
)

Format constants

Variables

This section is empty.

Functions

func EndpointKey

func EndpointKey(method, pathTemplate string) string

EndpointKey creates a unique key for an endpoint (method + path template).

func InferPathTemplate

func InferPathTemplate(path string) (template string, params map[string]string)

InferPathTemplate is a convenience function for inferring path templates. It creates a new PathInferrer and calls InferTemplate.

func NormalizePath

func NormalizePath(path string) string

NormalizePath normalizes a path for comparison. Removes trailing slashes and lowercases.

func ProcessBody

func ProcessBody(store *SchemaStore, body any)

ProcessBody extracts schema information from a JSON body into a SchemaStore.

Types

type BodyData

type BodyData struct {
	ContentType string
	Schema      *SchemaStore
}

BodyData tracks request/response body schema.

func NewBodyData

func NewBodyData(contentType string) *BodyData

NewBodyData creates a new BodyData.

type DetectedSecurityScheme

type DetectedSecurityScheme struct {
	Type         string // http, apiKey, oauth2
	Scheme       string // bearer, basic (for http type)
	Name         string // header name (for apiKey type)
	In           string // header, query, cookie
	BearerFormat string // JWT, etc.
	Count        int    // number of times observed
}

DetectedSecurityScheme represents a detected security scheme.

type EndpointClusterer

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

EndpointClusterer groups IR records by endpoint (method + path template).

func NewEndpointClusterer

func NewEndpointClusterer() *EndpointClusterer

NewEndpointClusterer creates a new EndpointClusterer.

func (*EndpointClusterer) AddRecord

func (c *EndpointClusterer) AddRecord(method, path string, pathTemplate string, pathParams map[string]string,
	query map[string]any, headers map[string]string, requestBody any, requestContentType string,
	status int, responseBody any, responseContentType string, responseHeaders map[string]string,
	host string, scheme string)

AddRecord processes an IR record and adds it to the appropriate endpoint.

func (*EndpointClusterer) Finalize

func (c *EndpointClusterer) Finalize()

Finalize completes the inference process (e.g., marking optional fields).

func (*EndpointClusterer) GetResult

func (c *EndpointClusterer) GetResult() *InferenceResult

GetResult returns the inference result.

type EndpointData

type EndpointData struct {
	Method       string
	PathTemplate string
	PathParams   map[string]*ParamData // parameter name -> data
	QueryParams  map[string]*ParamData // parameter name -> data
	HeaderParams map[string]*ParamData // header name -> data
	RequestBody  *BodyData             // request body schema
	Responses    map[int]*ResponseData // status code -> response data
	RequestCount int                   // number of requests observed
}

EndpointData represents aggregated data for a single API endpoint.

func NewEndpointData

func NewEndpointData(method, pathTemplate string) *EndpointData

NewEndpointData creates a new EndpointData.

type Engine

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

Engine orchestrates the inference process.

func NewEngine

func NewEngine(options EngineOptions) *Engine

NewEngine creates a new inference engine.

func (*Engine) Finalize

func (e *Engine) Finalize() *InferenceResult

Finalize completes the inference process.

func (*Engine) ProcessReader

func (e *Engine) ProcessReader(reader ir.IRReader) error

ProcessReader processes all records from an IRReader. Reads until io.EOF is returned.

func (*Engine) ProcessRecord

func (e *Engine) ProcessRecord(record *ir.IRRecord)

ProcessRecord processes a single IR record.

func (*Engine) ProcessRecords

func (e *Engine) ProcessRecords(records []ir.IRRecord)

ProcessRecords processes a slice of IR records.

type EngineOptions

type EngineOptions struct {
	// IncludeErrorResponses includes 4xx/5xx responses in the spec
	IncludeErrorResponses bool

	// MinStatusCode is the minimum status code to include (default: 100)
	MinStatusCode int

	// MaxStatusCode is the maximum status code to include (default: 599)
	MaxStatusCode int

	// SkipEmptyBodies skips recording empty request/response bodies
	SkipEmptyBodies bool
}

EngineOptions configures the inference engine.

func DefaultEngineOptions

func DefaultEngineOptions() EngineOptions

DefaultEngineOptions returns the default engine options.

type InferenceResult

type InferenceResult struct {
	Endpoints        map[string]*EndpointData           // key: "METHOD /path/template"
	Hosts            []string                           // observed hosts
	Schemes          []string                           // observed schemes (http, https)
	SecuritySchemes  map[string]*DetectedSecurityScheme // detected authentication schemes
	PaginationParams map[string]*PaginationParam        // detected pagination parameters
	RateLimitHeaders map[string]*RateLimitHeader        // detected rate limit headers
}

InferenceResult holds the complete inference results.

func InferFromDir

func InferFromDir(dir string) (*InferenceResult, error)

InferFromDir reads all IR files from a directory and returns inference results.

func InferFromFile

func InferFromFile(path string) (*InferenceResult, error)

InferFromFile reads an IR file and returns inference results.

func InferFromReader

func InferFromReader(reader ir.IRReader) (*InferenceResult, error)

InferFromReader is a convenience function that processes records from an IRReader.

func InferFromRecords

func InferFromRecords(records []ir.IRRecord) *InferenceResult

InferFromRecords is a convenience function that processes records and returns results.

func NewInferenceResult

func NewInferenceResult() *InferenceResult

NewInferenceResult creates a new InferenceResult.

type PaginationDetector

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

PaginationDetector detects pagination patterns from query parameters.

func NewPaginationDetector

func NewPaginationDetector() *PaginationDetector

NewPaginationDetector creates a new PaginationDetector.

func (*PaginationDetector) DetectFromQuery

func (d *PaginationDetector) DetectFromQuery(query map[string]any)

DetectFromQuery analyzes query parameters for pagination patterns.

func (*PaginationDetector) GetParams

func (d *PaginationDetector) GetParams() map[string]*PaginationParam

GetParams returns all detected pagination parameters.

type PaginationParam

type PaginationParam struct {
	Name        string
	Type        string   // offset, cursor, page
	Examples    []string // observed values
	Min         *int     // minimum observed value
	Max         *int     // maximum observed value
	Description string
}

PaginationParam represents a detected pagination parameter.

type ParamData

type ParamData struct {
	Name     string
	Examples []any
	Type     string // string, integer, number, boolean
	Format   string // uuid, email, date-time, etc.
	Required bool
	// contains filtered or unexported fields
}

ParamData tracks parameter values and infers type/format.

func NewParamData

func NewParamData(name string) *ParamData

NewParamData creates a new ParamData.

func (*ParamData) AddValue

func (p *ParamData) AddValue(value any)

AddValue adds a value to the parameter.

type PathInferrer

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

PathInferrer handles path template inference.

func NewPathInferrer

func NewPathInferrer() *PathInferrer

NewPathInferrer creates a new PathInferrer with default settings.

func (*PathInferrer) InferTemplate

func (p *PathInferrer) InferTemplate(path string) (template string, params map[string]string)

InferTemplate converts a concrete path to a parameterized template. Returns the template and extracted parameter values.

type RateLimitDetector

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

RateLimitDetector detects rate limiting patterns from response headers.

func NewRateLimitDetector

func NewRateLimitDetector() *RateLimitDetector

NewRateLimitDetector creates a new RateLimitDetector.

func (*RateLimitDetector) DetectFromHeaders

func (d *RateLimitDetector) DetectFromHeaders(headers map[string]string)

DetectFromHeaders analyzes response headers for rate limiting patterns.

func (*RateLimitDetector) GetHeaders

func (d *RateLimitDetector) GetHeaders() map[string]*RateLimitHeader

GetHeaders returns all detected rate limit headers.

type RateLimitHeader

type RateLimitHeader struct {
	Name        string
	Description string
	Type        string // integer, string
	Example     string
	Count       int
}

RateLimitHeader represents a detected rate limit header.

type ResponseData

type ResponseData struct {
	StatusCode  int
	ContentType string
	Headers     map[string]*ParamData
	Body        *SchemaStore
}

ResponseData tracks response information for a status code.

func NewResponseData

func NewResponseData(statusCode int) *ResponseData

NewResponseData creates a new ResponseData.

type SchemaNode

type SchemaNode struct {
	Type       string                 // string, integer, number, boolean, array, object
	Format     string                 // uuid, email, date-time, etc.
	Properties map[string]*SchemaNode // for objects
	Items      *SchemaNode            // for arrays
	Required   []string               // required properties
	Nullable   bool                   // can be null
	Examples   []any                  // example values
	Enum       []string               // enum values for strings with few unique values
}

SchemaNode represents a node in the inferred schema tree.

func BuildSchemaTree

func BuildSchemaTree(store *SchemaStore) *SchemaNode

BuildSchemaTree converts a SchemaStore into a hierarchical SchemaNode tree.

func MergeSchemas

func MergeSchemas(a, b *SchemaNode) *SchemaNode

MergeSchemas merges two schema nodes, combining their properties.

type SchemaStore

type SchemaStore struct {
	Examples map[string][]any  // path -> unique example values
	Types    map[string]string // path -> inferred type (string, number, integer, boolean, array, object)
	Optional map[string]bool   // path -> true if not present in all observations
	Nullable map[string]bool   // path -> true if null was observed
	Formats  map[string]string // path -> detected format (email, uuid, date-time, uri, etc.)
	// contains filtered or unexported fields
}

SchemaStore tracks JSON field paths and their observed values. Paths use dot notation (e.g., "user.address.city") with array markers (e.g., "items[].name").

func NewSchemaStore

func NewSchemaStore() *SchemaStore

NewSchemaStore creates a new SchemaStore with default settings.

func (*SchemaStore) AddObservation

func (s *SchemaStore) AddObservation()

AddObservation records a new observation of the schema. This increments the total count for optionality tracking.

func (*SchemaStore) AddValue

func (s *SchemaStore) AddValue(path string, value any)

AddValue adds a value at a given path.

func (*SchemaStore) FinalizeOptional

func (s *SchemaStore) FinalizeOptional()

FinalizeOptional marks paths as optional if they weren't seen in all observations.

func (*SchemaStore) GetPaths

func (s *SchemaStore) GetPaths() []string

GetPaths returns all tracked paths.

type SecurityDetector

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

SecurityDetector detects authentication schemes from request headers.

func NewSecurityDetector

func NewSecurityDetector() *SecurityDetector

NewSecurityDetector creates a new SecurityDetector.

func (*SecurityDetector) DetectFromHeaders

func (d *SecurityDetector) DetectFromHeaders(headers map[string]string)

DetectFromHeaders analyzes request headers for security schemes.

func (*SecurityDetector) GetSchemes

func (d *SecurityDetector) GetSchemes() map[string]*DetectedSecurityScheme

GetSchemes returns all detected security schemes.

type SegmentType

type SegmentType int

SegmentType represents the type of a path segment.

const (
	SegmentLiteral SegmentType = iota
	SegmentNumericID
	SegmentUUID
	SegmentHash
	SegmentObjectID
	SegmentBase64ID
	SegmentDate
	SegmentUnknownID
)

Jump to

Keyboard shortcuts

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