openapi

package
v0.0.0-...-4ed06ba Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package openapi provides OpenAPI specification parsing utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsJSON

func IsJSON(data []byte) bool

IsJSON returns true if the data appears to be JSON

func IsYAML

func IsYAML(data []byte) bool

IsYAML returns true if the data appears to be YAML

func SubstituteEnvVars

func SubstituteEnvVars(data []byte) []byte

SubstituteEnvVars replaces environment variable placeholders in data Supports ${VAR} and $VAR syntax

func SubstituteEnvVarsInString

func SubstituteEnvVarsInString(input string) string

SubstituteEnvVarsInString substitutes environment variables in a string

Types

type Components

type Components struct {
	Schemas         map[string]*Schema         `json:"schemas,omitempty"`
	Responses       map[string]*Response       `json:"responses,omitempty"`
	Parameters      map[string]*Parameter      `json:"parameters,omitempty"`
	Examples        map[string]*Example        `json:"examples,omitempty"`
	RequestBodies   map[string]*RequestBody    `json:"requestBodies,omitempty"`
	Headers         map[string]*Header         `json:"headers,omitempty"`
	SecuritySchemes map[string]*SecurityScheme `json:"securitySchemes,omitempty"`
	Links           map[string]interface{}     `json:"links,omitempty"`
	Callbacks       map[string]interface{}     `json:"callbacks,omitempty"`
}

Components represents OpenAPI components

type Contact

type Contact struct {
	Name  string `json:"name,omitempty"`
	URL   string `json:"url,omitempty"`
	Email string `json:"email,omitempty"`
}

Contact represents OpenAPI contact object

type Example

type Example struct {
	Summary     string      `json:"summary,omitempty"`
	Description string      `json:"description,omitempty"`
	Value       interface{} `json:"value,omitempty"`
}

Example represents an OpenAPI example

type ExternalDocs

type ExternalDocs struct {
	Description string `json:"description,omitempty"`
	URL         string `json:"url"`
}

ExternalDocs represents external documentation

type Header struct {
	Description string  `json:"description,omitempty"`
	Schema      *Schema `json:"schema,omitempty"`
}

Header represents an OpenAPI header

type Info

type Info struct {
	Title          string   `json:"title"`
	Description    string   `json:"description,omitempty"`
	TermsOfService string   `json:"termsOfService,omitempty"`
	Contact        *Contact `json:"contact,omitempty"`
	License        *License `json:"license,omitempty"`
	Version        string   `json:"version"`
}

Info represents OpenAPI info object

type License

type License struct {
	Name string `json:"name"`
	URL  string `json:"url,omitempty"`
}

License represents OpenAPI license object

type MediaType

type MediaType struct {
	Schema   *Schema             `json:"schema,omitempty"`
	Example  interface{}         `json:"example,omitempty"`
	Examples map[string]*Example `json:"examples,omitempty"`
}

MediaType represents an OpenAPI media type

type OAuthFlow

type OAuthFlow struct {
	AuthorizationURL string            `json:"authorizationUrl,omitempty"`
	TokenURL         string            `json:"tokenUrl,omitempty"`
	RefreshURL       string            `json:"refreshUrl,omitempty"`
	Scopes           map[string]string `json:"scopes,omitempty"`
}

OAuthFlow represents an OAuth flow

type OAuthFlows

type OAuthFlows struct {
	Implicit          *OAuthFlow `json:"implicit,omitempty"`
	Password          *OAuthFlow `json:"password,omitempty"`
	ClientCredentials *OAuthFlow `json:"clientCredentials,omitempty"`
	AuthorizationCode *OAuthFlow `json:"authorizationCode,omitempty"`
}

OAuthFlows represents OAuth flows

type Operation

type Operation struct {
	ID          string                `json:"operationId"`
	Method      string                `json:"method"`
	Path        string                `json:"path"`
	Summary     string                `json:"summary,omitempty"`
	Description string                `json:"description,omitempty"`
	Tags        []string              `json:"tags,omitempty"`
	Parameters  []Parameter           `json:"parameters,omitempty"`
	RequestBody *RequestBody          `json:"requestBody,omitempty"`
	Responses   map[string]*Response  `json:"responses"`
	Security    []SecurityRequirement `json:"security,omitempty"`
}

Operation represents an OpenAPI operation

type Parameter

type Parameter struct {
	Name        string      `json:"name"`
	In          string      `json:"in"` // query, header, path, cookie
	Description string      `json:"description,omitempty"`
	Required    bool        `json:"required"`
	Schema      *Schema     `json:"schema,omitempty"`
	Example     interface{} `json:"example,omitempty"`
}

Parameter represents an OpenAPI parameter

type Parser

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

Parser handles parsing of OpenAPI specifications

func NewParser

func NewParser() *Parser

NewParser creates a new OpenAPI parser

func (*Parser) Parse

func (p *Parser) Parse(data []byte, basePath string) (*Spec, error)

Parse parses OpenAPI specification data

func (*Parser) ParseFile

func (p *Parser) ParseFile(path string) (*Spec, error)

ParseFile parses an OpenAPI specification from a file path

func (*Parser) ParseFileWithEnv

func (p *Parser) ParseFileWithEnv(path string) (*Spec, error)

ParseFileWithEnv parses an OpenAPI spec with environment variable substitution

func (*Parser) ParseString

func (p *Parser) ParseString(data string) (*Spec, error)

ParseString parses OpenAPI specification from string

type PathItem

type PathItem struct {
	Ref         string      `json:"$ref,omitempty"`
	Summary     string      `json:"summary,omitempty"`
	Description string      `json:"description,omitempty"`
	Get         *Operation  `json:"get,omitempty"`
	Put         *Operation  `json:"put,omitempty"`
	Post        *Operation  `json:"post,omitempty"`
	Delete      *Operation  `json:"delete,omitempty"`
	Options     *Operation  `json:"options,omitempty"`
	Head        *Operation  `json:"head,omitempty"`
	Patch       *Operation  `json:"patch,omitempty"`
	Trace       *Operation  `json:"trace,omitempty"`
	Parameters  []Parameter `json:"parameters,omitempty"`
}

PathItem represents an OpenAPI path item

func (*PathItem) GetOperation

func (pi *PathItem) GetOperation(method string) *Operation

GetOperation returns the operation for a given HTTP method

func (*PathItem) GetOperations

func (pi *PathItem) GetOperations() map[string]*Operation

GetOperations returns all operations for this path item

type RequestBody

type RequestBody struct {
	Description string               `json:"description,omitempty"`
	Required    bool                 `json:"required"`
	Content     map[string]MediaType `json:"content"`
}

RequestBody represents an OpenAPI request body

type Response

type Response struct {
	Description string               `json:"description"`
	Content     map[string]MediaType `json:"content,omitempty"`
	Headers     map[string]*Header   `json:"headers,omitempty"`
}

Response represents an OpenAPI response

type Schema

type Schema struct {
	Type        string             `json:"type,omitempty"`
	Format      string             `json:"format,omitempty"`
	Description string             `json:"description,omitempty"`
	Default     interface{}        `json:"default,omitempty"`
	Enum        []interface{}      `json:"enum,omitempty"`
	Required    []string           `json:"required,omitempty"`
	Properties  map[string]*Schema `json:"properties,omitempty"`
	Items       *Schema            `json:"items,omitempty"`
	Ref         string             `json:"$ref,omitempty"`
	Nullable    bool               `json:"nullable,omitempty"`
	ReadOnly    bool               `json:"readOnly,omitempty"`
	WriteOnly   bool               `json:"writeOnly,omitempty"`
	Deprecated  bool               `json:"deprecated,omitempty"`
	Example     interface{}        `json:"example,omitempty"`
	// Validation keywords
	MinLength            *int        `json:"minLength,omitempty"`
	MaxLength            *int        `json:"maxLength,omitempty"`
	Minimum              *float64    `json:"minimum,omitempty"`
	Maximum              *float64    `json:"maximum,omitempty"`
	ExclusiveMin         bool        `json:"exclusiveMinimum,omitempty"`
	ExclusiveMax         bool        `json:"exclusiveMaximum,omitempty"`
	MultipleOf           *float64    `json:"multipleOf,omitempty"`
	MinItems             *int        `json:"minItems,omitempty"`
	MaxItems             *int        `json:"maxItems,omitempty"`
	UniqueItems          bool        `json:"uniqueItems,omitempty"`
	MinProperties        *int        `json:"minProperties,omitempty"`
	MaxProperties        *int        `json:"maxProperties,omitempty"`
	Pattern              string      `json:"pattern,omitempty"`
	AllOf                []*Schema   `json:"allOf,omitempty"`
	AnyOf                []*Schema   `json:"anyOf,omitempty"`
	OneOf                []*Schema   `json:"oneOf,omitempty"`
	Not                  *Schema     `json:"not,omitempty"`
	AdditionalProperties interface{} `json:"additionalProperties,omitempty"`
}

Schema represents an OpenAPI schema

type SecurityRequirement

type SecurityRequirement map[string][]string

SecurityRequirement represents a security requirement

type SecurityScheme

type SecurityScheme struct {
	Type             string      `json:"type"`
	Description      string      `json:"description,omitempty"`
	Name             string      `json:"name,omitempty"`
	In               string      `json:"in,omitempty"`
	Scheme           string      `json:"scheme,omitempty"`
	BearerFormat     string      `json:"bearerFormat,omitempty"`
	Flows            *OAuthFlows `json:"flows,omitempty"`
	OpenIDConnectURL string      `json:"openIdConnectUrl,omitempty"`
}

SecurityScheme represents a security scheme

type Server

type Server struct {
	URL         string                     `json:"url"`
	Description string                     `json:"description,omitempty"`
	Variables   map[string]*ServerVariable `json:"variables,omitempty"`
}

Server represents an OpenAPI server

type ServerVariable

type ServerVariable struct {
	Enum        []string `json:"enum,omitempty"`
	Default     string   `json:"default"`
	Description string   `json:"description,omitempty"`
}

ServerVariable represents a server variable

type Spec

type Spec struct {
	OpenAPI    string                `json:"openapi"`
	Info       Info                  `json:"info"`
	Servers    []Server              `json:"servers,omitempty"`
	Paths      map[string]*PathItem  `json:"paths"`
	Components *Components           `json:"components,omitempty"`
	Security   []SecurityRequirement `json:"security,omitempty"`
	Tags       []Tag                 `json:"tags,omitempty"`
	Raw        interface{}           `json:"-"` // Raw parsed spec
}

Spec represents a parsed OpenAPI specification

func (*Spec) GetAllOperations

func (s *Spec) GetAllOperations() []*Operation

GetAllOperations returns all operations from the spec

func (*Spec) GetBaseURL

func (s *Spec) GetBaseURL() string

GetBaseURL returns the first server URL or empty string

func (*Spec) GetOperationByID

func (s *Spec) GetOperationByID(id string) (*Operation, error)

GetOperationByID returns an operation by its ID

type Tag

type Tag struct {
	Name         string        `json:"name"`
	Description  string        `json:"description,omitempty"`
	ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"`
}

Tag represents an OpenAPI tag

Jump to

Keyboard shortcuts

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