Help us shape the future of Go! Take the 2026 Go Developer Survey

openapi3filter

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2018 License: MIT Imports: 13 Imported by: 530

Documentation

Overview

Package openapi3filter validates that requests and inputs request an OpenAPI 3 specification file.

Index

Constants

This section is empty.

Variables

View Source
var DefaultJSONMediaTypes = []string{
	"application/json",
}

Definition of MediaType to be interpreted as JSON

View Source
var DefaultOptions = &Options{}
View Source
var (
	ErrAuthenticationServiceMissing = errors.New("Request validator doesn't have an authentication service defined")
)
View Source
var JSONPrefixes = []string{
	")]}',\n",
}

Functions

func TrimJSONPrefix

func TrimJSONPrefix(data []byte) []byte

TrimJSONPrefix trims one of the possible prefixes

func ValidateParameter

func ValidateParameter(c context.Context, input *RequestValidationInput, parameter *openapi3.Parameter) error

func ValidateRequest

func ValidateRequest(c context.Context, input *RequestValidationInput) error

func ValidateRequestBody

func ValidateRequestBody(c context.Context, input *RequestValidationInput, requestBody *openapi3.RequestBody) error

func ValidateResponse

func ValidateResponse(c context.Context, input *ResponseValidationInput) error

func ValidateSecurityRequirements

func ValidateSecurityRequirements(c context.Context, input *RequestValidationInput, srs openapi3.SecurityRequirements) error

ValidateSecurityRequirements validates a multiple OpenAPI 3 security requirements. Returns nil if one of them inputed. Otherwise returns an error describing the security failures.

Types

type AuthenticationInput

type AuthenticationInput struct {
	RequestValidationInput *RequestValidationInput
	SecuritySchemeName     string
	SecurityScheme         *openapi3.SecurityScheme
	Scopes                 []string
}

func (*AuthenticationInput) NewError

func (input *AuthenticationInput) NewError(err error) error

type Options

type Options struct {
	ExcludeRequestBody    bool
	ExcludeResponseBody   bool
	IncludeResponseStatus bool
	AuthenticationFunc    func(c context.Context, input *AuthenticationInput) error
}

type RequestError

type RequestError struct {
	Input       *RequestValidationInput
	Parameter   *openapi3.Parameter
	RequestBody *openapi3.RequestBody
	Status      int
	Reason      string
	Err         error
}

func (*RequestError) Error

func (err *RequestError) Error() string

func (*RequestError) HTTPStatus

func (err *RequestError) HTTPStatus() int

type RequestValidationInput

type RequestValidationInput struct {
	Request     *http.Request
	PathParams  map[string]string
	QueryParams url.Values
	Route       *Route
	Options     *Options
}

func (*RequestValidationInput) GetQueryParams

func (input *RequestValidationInput) GetQueryParams() url.Values

type ResponseError

type ResponseError struct {
	Input  *ResponseValidationInput
	Reason string
	Err    error
}

func (*ResponseError) Error

func (err *ResponseError) Error() string

type ResponseValidationInput

type ResponseValidationInput struct {
	RequestValidationInput *RequestValidationInput
	Status                 int
	Header                 http.Header
	Body                   io.ReadCloser
	Options                *Options
}

func (*ResponseValidationInput) SetBodyBytes

func (input *ResponseValidationInput) SetBodyBytes(value []byte) *ResponseValidationInput

type Route

type Route struct {
	Swagger   *openapi3.Swagger
	Server    *openapi3.Server
	Path      string
	PathItem  *openapi3.PathItem
	Method    string
	Operation *openapi3.Operation

	// For developers who want use the router for handling too
	Handler http.Handler
}

type RouteError

type RouteError struct {
	Route  Route
	Reason string
}

func (*RouteError) Error

func (err *RouteError) Error() string

type Router

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

Router maps a HTTP request to an OpenAPI operation.

func NewRouter

func NewRouter() *Router

NewRouter creates a new router.

If the given Swagger has servers, router will use them. All operations of the Swagger will be added to the router.

func (*Router) AddRoute

func (router *Router) AddRoute(route *Route) error

AddRoute adds a route in the router.

func (*Router) AddSwagger

func (router *Router) AddSwagger(swagger *openapi3.Swagger) error

AddSwagger adds all operations in the OpenAPI specification.

func (*Router) AddSwaggerFromFile

func (router *Router) AddSwaggerFromFile(path string) error

AddSwaggerFromFile loads the Swagger file and adds it using AddSwagger.

func (*Router) FindRoute

func (router *Router) FindRoute(method string, url *url.URL) (*Route, map[string]string, error)

func (*Router) WithSwagger

func (router *Router) WithSwagger(swagger *openapi3.Swagger) *Router

WithSwagger adds all operations in the OpenAPI specification. Panics on any error.

func (*Router) WithSwaggerFromFile

func (router *Router) WithSwaggerFromFile(path string) *Router

WithSwaggerFromFile loads the Swagger file and adds it using WithSwagger. Panics on any error.

type Routers

type Routers []*Router

Routers maps a HTTP request to a Router.

func (Routers) FindRoute

func (routers Routers) FindRoute(method string, url *url.URL) (*Router, *Route, map[string]string, error)

type SecurityRequirementsError

type SecurityRequirementsError struct {
	SecurityRequirements openapi3.SecurityRequirements
	Errors               []error
}

func (*SecurityRequirementsError) Error

func (err *SecurityRequirementsError) Error() string

Jump to

Keyboard shortcuts

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