oasmiddleware

package
v1.0.0-beta.226 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRouteNotFound = errors.New("route not found")

Functions

func JsonBodyDecoder

func JsonBodyDecoder(body io.Reader, _ http.Header, _ *openapi3.SchemaRef, _ openapi3filter.EncodingFn) (any, error)

JsonBodyDecoder is meant to be used with openapi3filter.RegisterBodyDecoder to register a decoder for a custom vendor type like "application/konnect.foo+json"

func NewValidationRouter

func NewValidationRouter(ctx context.Context, doc *openapi3.T, opts *ValidationRouterOpts) (routers.Router, error)

NewValidationRouter creates a validation router to be injected in the middlewares to validate requests or responses. In a case of a bad spec it returns an error

func OasRouteNotFoundErrorHook

func OasRouteNotFoundErrorHook(err error, w http.ResponseWriter, r *http.Request) bool

OasRouteNotFoundErrorHook handles the error when a route is not found in a validation router. This will stop the request lifecycle and return an AIP compliant 404 response

func OasValidationErrorHook

func OasValidationErrorHook(ctx context.Context, err error, w http.ResponseWriter, r *http.Request) bool

OasValidationErrorHook handles the error when a request is not matching the OAS spec definition for a given route in the validation router. This will stop the request lifecycle and return an AIP compliant 400 response

func SanitizeSensitiveFieldValues

func SanitizeSensitiveFieldValues(err error) error

func ValidateRequest

func ValidateRequest(validationRouter routers.Router, opts ValidateRequestOption) func(h http.Handler) http.Handler

ValidateRequest is the middleware to be used to validate the request to the spec passed in for the validation router

func ValidateResponse

func ValidateResponse(validationRouter routers.Router, opts ValidateResponseOption) func(h http.Handler) http.Handler

ValidateResponse is the middleware to be used to validate the response to the spec passed in for the validation router

Types

type RequestNotFoundHookFunc

type RequestNotFoundHookFunc = func(error, http.ResponseWriter, *http.Request) bool

type RequestValidationErrorFunc

type RequestValidationErrorFunc = func(error, http.ResponseWriter, *http.Request) bool

type ResponseValidationFunc

type ResponseValidationFunc = func(error, *http.Request)

type ResponseWriterWrapper

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

ResponseWriterWrapper leverage to the original response write to be able to access the written body and the status code of the response. This is used in the response middleware but can also be used in a logging middleware to log the status code of the response.

func NewResponseWriterWrapper

func NewResponseWriterWrapper(w http.ResponseWriter) ResponseWriterWrapper

func (ResponseWriterWrapper) Body

func (rww ResponseWriterWrapper) Body() *bytes.Buffer

func (ResponseWriterWrapper) Header

func (rww ResponseWriterWrapper) Header() http.Header

Header function overwrites the http.ResponseWriter Header() function

func (ResponseWriterWrapper) StatusCode

func (rww ResponseWriterWrapper) StatusCode() *int

func (ResponseWriterWrapper) Unwrap

func (ResponseWriterWrapper) Write

func (rww ResponseWriterWrapper) Write(buf []byte) (int, error)

Write function overwrites the http.ResponseWriter Header() function

func (ResponseWriterWrapper) WriteHeader

func (rww ResponseWriterWrapper) WriteHeader(statusCode int)

WriteHeader function overwrites the http.ResponseWriter WriteHeader() function

type ValidateRequestOption

type ValidateRequestOption struct {
	// RouteNotFoundHook is called when the route is not found at the spec level
	// if the hook returns `true` the request flow is stopped
	RouteNotFoundHook RequestNotFoundHookFunc
	// RouteValidationErrorHook is called when the route parameters or body are
	// not validated. if the hook returns `true` the request flow is stopped
	RouteValidationErrorHook RequestValidationErrorFunc
	// FilterOptions are the openapi3filter option to pass to the underlying lib
	FilterOptions *openapi3filter.Options
}

ValidateRequestOption provides the hook functions and the openapi3filter option to be passed in to the underlying library

type ValidateResponseOption

type ValidateResponseOption struct {
	// ResponseValidationErrorHook is called when the route response body is not validated
	ResponseValidationErrorHook ResponseValidationFunc
	// FilterOptions are the openapi3filter option to pass to the underlying lib
	FilterOptions *openapi3filter.Options
}

ValidateResponseOption provides the hook function and the openapi3filter option to be passed in to the underlying library

type ValidationRouterOpts

type ValidationRouterOpts struct {
	// DeleteServers removes the `Servers` property from the parsed OAS spec to be used within test or even at runtime.
	// If you want to use it at runtime please read the doc for `ServerPrefix` first.
	DeleteServers bool
	// ServerPrefix adds a server entry with the desired prefix
	// eg: the service expose domain.com/foo/v0/entity . Your spec path are defined at the /entity level and then
	// /foo/v0 is part of the server entry in the OAS spec. If no prefix is provided, the validation router will either
	// take the whole server entry like domain.com/foo/v0/entity to validate or if `DeleteServers` is to true it will
	// only validate `/entity`
	ServerPrefix string
}

ValidationRouterOpts represents the options to be passed to the validation router for processing the requests.

Jump to

Keyboard shortcuts

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