v3

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 10 Imported by: 11

Documentation

Overview

Package v3 represents all OpenAPI 3+ low-level models. Low-level models are more difficult to navigate than higher-level models, however they are packed with all the raw AST and node data required to perform any kind of analysis on the underlying data.

Every property is wrapped in a NodeReference or a KeyReference or a ValueReference.

Example (CreateLowLevelOpenAPIDocument)

How to create a low-level OpenAPI 3+ Document from an OpenAPI specification

// How to create a low-level OpenAPI 3 Document

// load petstore into bytes
petstoreBytes, _ := ioutil.ReadFile("../../../test_specs/petstorev3.json")

// read in specification
info, _ := datamodel.ExtractSpecInfo(petstoreBytes)

// build low-level document model
document, errors := CreateDocument(info)

// if something went wrong, a slice of errors is returned
if len(errors) > 0 {
	for i := range errors {
		fmt.Printf("error: %s\n", errors[i].Error())
	}
	panic("cannot build document")
}

// print out email address from the info > contact object.
fmt.Print(document.Info.Value.Contact.Value.Email.Value)
Output:

apiteam@swagger.io

Index

Examples

Constants

View Source
const (
	ComponentsLabel        = "components"
	SchemasLabel           = "schemas"
	EncodingLabel          = "encoding"
	HeadersLabel           = "headers"
	ParametersLabel        = "parameters"
	RequestBodyLabel       = "requestBody"
	RequestBodiesLabel     = "requestBodies"
	ResponsesLabel         = "responses"
	CallbacksLabel         = "callbacks"
	ContentLabel           = "content"
	PathsLabel             = "paths"
	WebhooksLabel          = "webhooks"
	JSONSchemaDialectLabel = "jsonSchemaDialect"
	GetLabel               = "get"
	PostLabel              = "post"
	PatchLabel             = "patch"
	PutLabel               = "put"
	DeleteLabel            = "delete"
	OptionsLabel           = "options"
	HeadLabel              = "head"
	TraceLabel             = "trace"
	LinksLabel             = "links"
	DefaultLabel           = "default"
	SecurityLabel          = "security"
	SecuritySchemesLabel   = "securitySchemes"
	OAuthFlowsLabel        = "flows"
	VariablesLabel         = "variables"
	ServersLabel           = "servers"
	ServerLabel            = "server"
	ImplicitLabel          = "implicit"
	PasswordLabel          = "password"
	ClientCredentialsLabel = "clientCredentials"
	AuthorizationCodeLabel = "authorizationCode"
)

Label definitions used to look up vales in yaml.Node tree.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback struct {
	Expression low.ValueReference[map[low.KeyReference[string]]low.ValueReference[*PathItem]]
	Extensions map[low.KeyReference[string]]low.ValueReference[any]
}

Callback represents a low-level Callback object for OpenAPI 3+.

A map of possible out-of band callbacks related to the parent operation. Each value in the map is a PathItem Object that describes a set of requests that may be initiated by the API provider and the expected responses. The key value used to identify the path item object is an expression, evaluated at runtime, that identifies a URL to use for the callback operation.

func (*Callback) Build

func (cb *Callback) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract extensions, expressions and PathItem objects for Callback

func (*Callback) FindExpression

func (cb *Callback) FindExpression(exp string) *low.ValueReference[*PathItem]

FindExpression will locate a string expression and return a ValueReference containing the located PathItem

type Components

Components represents a low-level OpenAPI 3+ Components Object, that is backed by a low-level one.

Holds a set of reusable objects for different aspects of the OAS. All objects defined within the components object will have no effect on the API unless they are explicitly referenced from properties outside the components object.

func (*Components) Build

func (co *Components) Build(root *yaml.Node, idx *index.SpecIndex) error

func (*Components) FindCallback

func (co *Components) FindCallback(callback string) *low.ValueReference[*Callback]

func (*Components) FindExample

func (co *Components) FindExample(example string) *low.ValueReference[*base.Example]

FindExample attempts tp

func (*Components) FindExtension

func (co *Components) FindExtension(ext string) *low.ValueReference[any]

FindExtension attempts to locate an extension with the supplied key

func (*Components) FindHeader

func (co *Components) FindHeader(header string) *low.ValueReference[*Header]
func (co *Components) FindLink(link string) *low.ValueReference[*Link]

func (*Components) FindParameter

func (co *Components) FindParameter(response string) *low.ValueReference[*Parameter]

FindParameter attempts to locate a Parameter from 'parameters' with a specific name

func (*Components) FindRequestBody

func (co *Components) FindRequestBody(requestBody string) *low.ValueReference[*RequestBody]

func (*Components) FindResponse

func (co *Components) FindResponse(response string) *low.ValueReference[*Response]

FindResponse attempts to locate a Response from 'responses' with a specific name

func (*Components) FindSchema

func (co *Components) FindSchema(schema string) *low.ValueReference[*base.SchemaProxy]

FindSchema attempts to locate a SchemaProxy from 'schemas' with a specific name

func (*Components) FindSecurityScheme

func (co *Components) FindSecurityScheme(sScheme string) *low.ValueReference[*SecurityScheme]

FindSecurityScheme attempts to locate a SecurityScheme from 'securitySchemes' with a specific name

type Document

type Document struct {

	// Version is the version of OpenAPI being used, extracted from the 'openapi: x.x.x' definition.
	// This is not a standard property of the OpenAPI model, it's a convenience mechanism only.
	Version low.ValueReference[string]

	// Info represents a specification Info definitions
	// Provides metadata about the API. The metadata MAY be used by tooling as required.
	// - https://spec.openapis.org/oas/v3.1.0#info-object
	Info low.NodeReference[*base.Info]

	// JsonSchemaDialect is a 3.1+ property that sets the dialect to use for validating *base.Schema definitions
	// The default value for the $schema keyword within Schema Objects contained within this OAS document.
	// This MUST be in the form of a URI.
	// - https://spec.openapis.org/oas/v3.1.0#schema-object
	JsonSchemaDialect low.NodeReference[string] // 3.1

	// Webhooks is a 3.1+ property that is similar to callbacks, except, this defines incoming webhooks.
	// The incoming webhooks that MAY be received as part of this API and that the API consumer MAY choose to implement.
	// Closely related to the callbacks feature, this section describes requests initiated other than by an API call,
	// for example by an out-of-band registration. The key name is a unique string to refer to each webhook,
	// while the (optionally referenced) Path Item Object describes a request that may be initiated by the API provider
	// and the expected responses. An example is available.
	Webhooks low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*PathItem]] // 3.1

	// Servers is a slice of Server instances which provide connectivity information to a target server. If the servers
	// property is not provided, or is an empty array, the default value would be a Server Object with a url value of /.
	// - https://spec.openapis.org/oas/v3.1.0#server-object
	Servers low.NodeReference[[]low.ValueReference[*Server]]

	// Paths contains all the PathItem definitions for the specification.
	// The available paths and operations for the API, The most important part of ths spec.
	// - https://spec.openapis.org/oas/v3.1.0#paths-object
	Paths low.NodeReference[*Paths]

	// Components is an element to hold various schemas for the document.
	// - https://spec.openapis.org/oas/v3.1.0#components-object
	Components low.NodeReference[*Components]

	// Security contains global security requirements/roles for the specification
	// A declaration of which security mechanisms can be used across the API. The list of values includes alternative
	// security requirement objects that can be used. Only one of the security requirement objects need to be satisfied
	// to authorize a request. Individual operations can override this definition. To make security optional,
	// an empty security requirement ({}) can be included in the array.
	// - https://spec.openapis.org/oas/v3.1.0#security-requirement-object
	Security low.NodeReference[*SecurityRequirement]

	// Tags is a slice of base.Tag instances defined by the specification
	// A list of tags used by the document with additional metadata. The order of the tags can be used to reflect on
	// their order by the parsing tools. Not all tags that are used by the Operation Object must be declared.
	// The tags that are not declared MAY be organized randomly or based on the tools’ logic.
	// Each tag name in the list MUST be unique.
	// - https://spec.openapis.org/oas/v3.1.0#tag-object
	Tags low.NodeReference[[]low.ValueReference[*base.Tag]]

	// ExternalDocs is an instance of base.ExternalDoc for.. well, obvious really, innit.
	// - https://spec.openapis.org/oas/v3.1.0#external-documentation-object
	ExternalDocs low.NodeReference[*base.ExternalDoc]

	// Extensions contains all custom extensions defined for the top-level document.
	Extensions map[low.KeyReference[string]]low.ValueReference[any]

	// Index is a reference to the *index.SpecIndex that was created for the document and used
	// as a guide when building out the Document. Ideal if further processing is required on the model and
	// the original details are required to continue the work.
	//
	// This property is not a part of the OpenAPI schema, this is custom to libopenapi.
	Index *index.SpecIndex
}

func CreateDocument

func CreateDocument(info *datamodel.SpecInfo) (*Document, []error)
Example
// How to create a low-level OpenAPI 3 Document

// load petstore into bytes
petstoreBytes, _ := ioutil.ReadFile("../../../test_specs/petstorev3.json")

// read in specification
info, _ := datamodel.ExtractSpecInfo(petstoreBytes)

// build low-level document model
document, errors := CreateDocument(info)

// if something went wrong, a slice of errors is returned
if len(errors) > 0 {
	for i := range errors {
		fmt.Printf("error: %s\n", errors[i].Error())
	}
	panic("cannot build document")
}

// print out email address from the info > contact object.
fmt.Print(document.Info.Value.Contact.Value.Email.Value)
Output:

apiteam@swagger.io

type Encoding

type Encoding struct {
	ContentType   low.NodeReference[string]
	Headers       low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Header]]
	Style         low.NodeReference[string]
	Explode       low.NodeReference[bool]
	AllowReserved low.NodeReference[bool]
}

Encoding represents a low-level OpenAPI 3+ Encoding object

func (*Encoding) Build

func (en *Encoding) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract all Header objects from supplied node.

func (*Encoding) FindHeader

func (en *Encoding) FindHeader(hType string) *low.ValueReference[*Header]

FindHeader attempts to locate a Header with the supplied name

Header represents a low-level OpenAPI 3+ Header object.

func (*Header) Build

func (h *Header) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract extensions, examples, schema and content/media types from node.

func (*Header) FindContent

func (h *Header) FindContent(ext string) *low.ValueReference[*MediaType]

FindContent will attempt to locate a MediaType definition, with a specified name

func (*Header) FindExample

func (h *Header) FindExample(eType string) *low.ValueReference[*base.Example]

FindExample will attempt to locate an Example with a specified name

func (*Header) FindExtension

func (h *Header) FindExtension(ext string) *low.ValueReference[any]

FindExtension will attempt to locate an extension with the supplied name

type Link struct {
	OperationRef low.NodeReference[string]
	OperationId  low.NodeReference[string]
	Parameters   low.KeyReference[map[low.KeyReference[string]]low.ValueReference[string]]
	RequestBody  low.NodeReference[string]
	Description  low.NodeReference[string]
	Server       low.NodeReference[*Server]
	Extensions   map[low.KeyReference[string]]low.ValueReference[any]
}

Link represents a low-level OpenAPI 3+ Link object.

The Link object represents a possible design-time link for a response. The presence of a link does not guarantee the caller’s ability to successfully invoke it, rather it provides a known relationship and traversal mechanism between responses and other operations.

Unlike dynamic links (i.e. links provided in the response payload), the OAS linking mechanism does not require link information in the runtime response.

For computing links, and providing instructions to execute them, a runtime expression is used for accessing values in an operation and using them as parameters while invoking the linked operation.

func (*Link) Build

func (l *Link) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract extensions and servers from the node.

func (*Link) FindExtension

func (l *Link) FindExtension(ext string) *low.ValueReference[any]

FindExtension will attempt to locate an extension with a specific key

func (*Link) FindParameter

func (l *Link) FindParameter(pName string) *low.ValueReference[string]

FindParameter will attempt to locate a parameter string value, using a parameter name input.

type MediaType

MediaType represents a low-level OpenAPI MediaType object.

Each Media Type Object provides schema and examples for the media type identified by its key.

func (*MediaType) Build

func (mt *MediaType) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract examples, extensions, schema and encoding from node.

func (*MediaType) FindExample

func (mt *MediaType) FindExample(eType string) *low.ValueReference[*base.Example]

FindExample will attempt to locate an Example with a specific name.

func (*MediaType) FindExtension

func (mt *MediaType) FindExtension(ext string) *low.ValueReference[any]

FindExtension will attempt to locate an extension with the supplied name.

func (*MediaType) FindPropertyEncoding

func (mt *MediaType) FindPropertyEncoding(eType string) *low.ValueReference[*Encoding]

FindPropertyEncoding will attempt to locate an Encoding value with a specific name.

func (*MediaType) GetAllExamples

func (mt *MediaType) GetAllExamples() map[low.KeyReference[string]]low.ValueReference[*base.Example]

GetAllExamples will extract all examples from the MediaType instance.

type OAuthFlow

type OAuthFlow struct {
	AuthorizationUrl low.NodeReference[string]
	TokenUrl         low.NodeReference[string]
	RefreshUrl       low.NodeReference[string]
	Scopes           low.KeyReference[map[low.KeyReference[string]]low.ValueReference[string]]
	Extensions       map[low.KeyReference[string]]low.ValueReference[any]
}

OAuthFlow represents a low-level OpenAPI 3+ OAuthFlow object.

func (*OAuthFlow) Build

func (o *OAuthFlow) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract extensions from the node.

func (*OAuthFlow) FindExtension

func (o *OAuthFlow) FindExtension(ext string) *low.ValueReference[any]

FindExtension attempts to locate an extension with a specified key

func (*OAuthFlow) FindScope

func (o *OAuthFlow) FindScope(scope string) *low.ValueReference[string]

FindScope attempts to locate a scope using a specified name.

type OAuthFlows

type OAuthFlows struct {
	Implicit          low.NodeReference[*OAuthFlow]
	Password          low.NodeReference[*OAuthFlow]
	ClientCredentials low.NodeReference[*OAuthFlow]
	AuthorizationCode low.NodeReference[*OAuthFlow]
	Extensions        map[low.KeyReference[string]]low.ValueReference[any]
}

OAuthFlows represents a low-level OpenAPI 3+ OAuthFlows object.

func (*OAuthFlows) Build

func (o *OAuthFlows) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract extensions and all OAuthFlow types from the supplied node.

func (*OAuthFlows) FindExtension

func (o *OAuthFlows) FindExtension(ext string) *low.ValueReference[any]

FindExtension will attempt to locate an extension with the supplied name.

type Operation

Operation is a low-level representation of an OpenAPI 3+ Operation object.

An Operation is perhaps the most important object of the entire specification. Everything of value happens here. The entire being for existence of this library and the specification, is this Operation.

func (*Operation) Build

func (o *Operation) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract external docs, parameters, request body, responses, callbacks, security and servers.

func (*Operation) FindCallback

func (o *Operation) FindCallback(callback string) *low.ValueReference[*Callback]

FindCallback will attempt to locate a Callback instance by the supplied name.

type Parameter

Parameter represents a high-level OpenAPI 3+ Parameter object, that is backed by a low-level one.

A unique parameter is defined by a combination of a name and location.

func (*Parameter) Build

func (p *Parameter) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract examples, extensions and content/media types.

func (*Parameter) FindContent

func (p *Parameter) FindContent(cType string) *low.ValueReference[*MediaType]

FindContent will attempt to locate a MediaType instance using the specified name.

func (*Parameter) FindExample

func (p *Parameter) FindExample(eType string) *low.ValueReference[*base.Example]

FindExample will attempt to locate a base.Example instance using the specified name.

func (*Parameter) FindExtension

func (p *Parameter) FindExtension(ext string) *low.ValueReference[any]

FindExtension attempts to locate an extension using the specified name.

type PathItem

PathItem represents a low-level OpenAPI 3+ PathItem object.

Describes the operations available on a single path. A Path Item MAY be empty, due to ACL constraints. The path itself is still exposed to the documentation viewer, but they will not know which operations and parameters are available.

func (*PathItem) Build

func (p *PathItem) Build(root *yaml.Node, idx *index.SpecIndex) error

Build extracts extensions, parameters, servers and each http method defined. everything is extracted asynchronously for speed.

func (*PathItem) FindExtension

func (p *PathItem) FindExtension(ext string) *low.ValueReference[any]

FindExtension attempts to find an extension

type Paths

type Paths struct {
	PathItems  map[low.KeyReference[string]]low.ValueReference[*PathItem]
	Extensions map[low.KeyReference[string]]low.ValueReference[any]
}

Paths represents a high-level OpenAPI 3+ Paths object, that is backed by a low-level one.

Holds the relative paths to the individual endpoints and their operations. The path is appended to the URL from the Server Object in order to construct the full URL. The Paths MAY be empty, due to Access Control List (ACL) constraints.

func (*Paths) Build

func (p *Paths) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract extensions and all PathItems. This happens asynchronously for speed.

func (*Paths) FindExtension

func (p *Paths) FindExtension(ext string) *low.ValueReference[any]

FindExtension will attempt to locate an extension using the specified string.

func (*Paths) FindPath

func (p *Paths) FindPath(path string) *low.ValueReference[*PathItem]

FindPath will attempt to locate a PathItem using the provided path string.

type RequestBody

type RequestBody struct {
	Description low.NodeReference[string]
	Content     low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*MediaType]]
	Required    low.NodeReference[bool]
	Extensions  map[low.KeyReference[string]]low.ValueReference[any]
}

RequestBody represents a low-level OpenAPI 3+ RequestBody object.

func (*RequestBody) Build

func (rb *RequestBody) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract extensions and MediaType objects from the node.

func (*RequestBody) FindContent

func (rb *RequestBody) FindContent(cType string) *low.ValueReference[*MediaType]

FindContent attempts to find content/MediaType defined using a specified name.

func (*RequestBody) FindExtension

func (rb *RequestBody) FindExtension(ext string) *low.ValueReference[any]

FindExtension attemps to locate an extension using the provided name.

type Response

Response represents a high-level OpenAPI 3+ Response object that is backed by a low-level one.

Describes a single response from an API Operation, including design-time, static links to operations based on the response.

func (*Response) Build

func (r *Response) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract headers, extensions, content and links from node.

func (*Response) FindContent

func (r *Response) FindContent(cType string) *low.ValueReference[*MediaType]

FindContent will attempt to locate a MediaType instance using the supplied key.

func (*Response) FindExtension

func (r *Response) FindExtension(ext string) *low.ValueReference[any]

FindExtension will attempt to locate an extension using the supplied key

func (*Response) FindHeader

func (r *Response) FindHeader(hType string) *low.ValueReference[*Header]

FindHeader will attempt to locate a Header instance using the supplied key.

func (r *Response) FindLink(hType string) *low.ValueReference[*Link]

FindLink will attempt to locate a Link instance using the supplied key.

type Responses

type Responses struct {
	Codes   map[low.KeyReference[string]]low.ValueReference[*Response]
	Default low.NodeReference[*Response]
}

Responses represents a low-level OpenAPI 3+ Responses object.

It's a container for the expected responses of an operation. The container maps a HTTP response code to the expected response.

The specification is not necessarily expected to cover all possible HTTP response codes because they may not be known in advance. However, documentation is expected to cover a successful operation response and any known errors.

The default MAY be used as a default response object for all HTTP codes that are not covered individually by the Responses Object.

The Responses Object MUST contain at least one response code, and if only one response code is provided it SHOULD be the response for a successful operation call.

func (*Responses) Build

func (r *Responses) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract default response and all Response objects for each code

func (*Responses) FindResponseByCode

func (r *Responses) FindResponseByCode(code string) *low.ValueReference[*Response]

FindResponseByCode will attempt to locate a Response using an HTTP response code.

type SecurityRequirement

type SecurityRequirement struct {
	ValueRequirements []low.ValueReference[map[low.KeyReference[string]][]low.ValueReference[string]]
}

SecurityRequirement is a low-level representation of an OpenAPI 3+ SecurityRequirement object.

It lists the required security schemes to execute this operation. The name used for each property MUST correspond to a security scheme declared in the Security Schemes under the Components Object.

Security Requirement Objects that contain multiple schemes require that all schemes MUST be satisfied for a request to be authorized. This enables support for scenarios where multiple query parameters or HTTP headers are required to convey security information.

When a list of Security Requirement Objects is defined on the OpenAPI Object or Operation Object, only one of the Security Requirement Objects in the list needs to be satisfied to authorize the request.

func (*SecurityRequirement) Build

func (sr *SecurityRequirement) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract all security requirements

func (*SecurityRequirement) FindRequirement

func (sr *SecurityRequirement) FindRequirement(name string) []low.ValueReference[string]

FindRequirement will attempt to locate a security requirement string from a supplied name.

type SecurityScheme

type SecurityScheme struct {
	Type             low.NodeReference[string]
	Description      low.NodeReference[string]
	Name             low.NodeReference[string]
	In               low.NodeReference[string]
	Scheme           low.NodeReference[string]
	BearerFormat     low.NodeReference[string]
	Flows            low.NodeReference[*OAuthFlows]
	OpenIdConnectUrl low.NodeReference[string]
	Extensions       map[low.KeyReference[string]]low.ValueReference[any]
}

SecurityScheme represents a low-level OpenAPI 3+ SecurityScheme object.

Defines a security scheme that can be used by the operations.

Supported schemes are HTTP authentication, an API key (either as a header, a cookie parameter or as a query parameter), mutual TLS (use of a client certificate), OAuth2’s common flows (implicit, password, client credentials and authorization code) as defined in RFC6749 (https://www.rfc-editor.org/rfc/rfc6749), and OpenID Connect Discovery. Please note that as of 2020, the implicit flow is about to be deprecated by OAuth 2.0 Security Best Current Practice. Recommended for most use case is Authorization Code Grant flow with PKCE.

func (*SecurityScheme) Build

func (ss *SecurityScheme) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract OAuthFlows and extensions from the node.

func (*SecurityScheme) FindExtension

func (ss *SecurityScheme) FindExtension(ext string) *low.ValueReference[any]

FindExtension attempts to locate an extension using the supplied key.

type Server

Server represents a low-level OpenAPI 3+ Server object.

func (*Server) Build

func (s *Server) Build(root *yaml.Node, idx *index.SpecIndex) error

Build will extract server variables from the supplied node.

func (*Server) FindVariable

func (s *Server) FindVariable(serverVar string) *low.ValueReference[*ServerVariable]

FindVariable attempts to locate a ServerVariable instance using the supplied key.

type ServerVariable

type ServerVariable struct {
	Enum        []low.NodeReference[string]
	Default     low.NodeReference[string]
	Description low.NodeReference[string]
}

ServerVariable represents a low-level OpenAPI 3+ ServerVariable object.

ServerVariable is an object representing a Server Variable for server URL template substitution. - https://spec.openapis.org/oas/v3.1.0#server-variable-object

This is the only struct that is not Buildable, it's not used by anything other than a Server instance, and it has nothing to build that requires it to be buildable.

Jump to

Keyboard shortcuts

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