openapi

package module
v0.0.0-...-cbd040c Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JSONSchemaDialect = "https://json-schema.org/draft/2020-12/schema"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback map[string]PathItem

type CallbackOrRef

type CallbackOrRef struct {
	*Callback
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

type Components

type Components struct {
	Schemas         map[string]Schema              `json:"schemas,omitempty" yaml:"schemas,omitempty"`
	Responses       map[string]ResponseOrRef       `json:"responses,omitempty" yaml:"responses,omitempty"`
	Parameters      map[string]ParameterOrRef      `json:"parameters,omitempty" yaml:"parameters,omitempty"`
	Examples        map[string]ExampleOrRef        `json:"examples,omitempty" yaml:"examples,omitempty"`
	RequestBodies   map[string]RequestBodyOrRef    `json:"requestBodies,omitempty" yaml:"requestBodies,omitempty"`
	Headers         map[string]HeaderOrRef         `json:"headers,omitempty" yaml:"headers,omitempty"`
	SecuritySchemes map[string]SecuritySchemeOrRef `json:"securitySchemes,omitempty" yaml:"securitySchemes,omitempty"`
	Links           map[string]LinkOrRef           `json:"links,omitempty" yaml:"links,omitempty"`
	Callbacks       map[string]CallbackOrRef       `json:"callbacks,omitempty" yaml:"callbacks,omitempty"`
	PathItems       map[string]PathItem            `json:"pathItems,omitempty" yaml:"pathItems,omitempty"`
	MediaTypes      map[string]MediaTypeOrRef      `json:"mediaTypes,omitempty" yaml:"mediaTypes,omitempty"`
}

type Config

type Config struct {
	Version           string                `json:"openapi" yaml:"openapi"`
	Self              string                `json:"$self,omitempty" yaml:"$self,omitempty"`
	Info              *Info                 `json:"info" yaml:"info"`
	JSONSchemaDialect string                `json:"jsonSchemaDialect,omitempty" yaml:"jsonSchemaDialect,omitempty"`
	Servers           []Server              `json:"servers,omitempty" yaml:"servers,omitempty"`
	Tags              []Tag                 `json:"tags,omitempty" yaml:"tags,omitempty"`
	Security          []map[string][]string `json:"security,omitempty" yaml:"security,omitempty"`
	ExternalDocs      *ExternalDocs         `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
	Paths             Paths                 `json:"paths" yaml:"paths"`
	Components        *Components           `json:"components,omitempty" yaml:"components,omitempty"`
}

func (*Config) MarshalJSON

func (c *Config) MarshalJSON() ([]byte, error)

MarshalJSON converts the entire OpenAPI configuration to formatted JSON. Automatically sets defaults before marshaling if not already set. Returns the JSON bytes or an error if marshaling fails.

func (*Config) MarshalYaml

func (c *Config) MarshalYaml() ([]byte, error)

MarshalYaml converts the entire OpenAPI configuration to YAML format. Returns the YAML bytes or an error if marshaling fails.

func (*Config) SetDefaults

func (c *Config) SetDefaults()

SetDefaults initializes required OpenAPI configuration fields with default values. Sets OpenAPI version to 3.2.0, ensures Info is initialized, creates empty paths/components if nil.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks that the OpenAPI configuration is valid. Currently performs no validation and always returns nil.

type Contact

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

type Discriminator

type Discriminator struct {
	PropertyName   string            `json:"propertyName" yaml:"propertyName"`
	Mapping        map[string]string `json:"mapping,omitempty" yaml:"mapping,omitempty"`
	DefaultMapping string            `json:"defaultMapping,omitempty" yaml:"defaultMapping,omitempty"`
}

type Encoding

type Encoding struct {
	ContentType  string                 `json:"contentType,omitempty" yaml:"contentType,omitempty"`
	Headers      map[string]HeaderOrRef `json:"headers,omitempty" yaml:"headers,omitempty"`
	Encoding     map[string]Encoding    `json:"encoding,omitempty" yaml:"encoding,omitempty"`
	Prefix       *Encoding              `json:"prefix,omitempty" yaml:"prefix,omitempty"`
	ItemEncoding *Encoding              `json:"itemEncoding,omitempty" yaml:"itemEncoding,omitempty"`
}

type Example

type Example struct {
	Summary         string `json:"summary,omitempty" yaml:"summary,omitempty"`
	Description     string `json:"description,omitempty" yaml:"description,omitempty"`
	DefaultValue    any    `json:"defaultValue,omitempty" yaml:"defaultValue,omitempty"`
	SerializedValue any    `json:"serializedValue,omitempty" yaml:"serializedValue,omitempty"`
	ExternalValue   string `json:"externalValue,omitempty" yaml:"externalValue,omitempty"`
	DataValue       any    `json:"dataValue,omitempty" yaml:"dataValue,omitempty"`
}

type ExampleOrRef

type ExampleOrRef struct {
	*Example
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

type ExternalDocs

type ExternalDocs struct {
	Description string `json:"description,omitempty" yaml:"description,omitempty"`
	URL         string `json:"url" yaml:"url"`
}
type Header struct {
	Description string                    `json:"description,omitempty" yaml:"description,omitempty"`
	Required    bool                      `json:"required,omitempty" yaml:"required,omitempty"`
	Deprecated  bool                      `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
	Example     any                       `json:"example,omitempty" yaml:"example,omitempty"`
	Examples    map[string]ExampleOrRef   `json:"examples,omitempty" yaml:"examples,omitempty"`
	Style       string                    `json:"style,omitempty" yaml:"style,omitempty"`
	Explode     *bool                     `json:"explode,omitempty" yaml:"explode,omitempty"`
	Schema      *SchemaOrRef              `json:"schema,omitempty" yaml:"schema,omitempty"`
	Content     map[string]MediaTypeOrRef `json:"content,omitempty" yaml:"content,omitempty"`
}

type HeaderOrRef

type HeaderOrRef struct {
	*Header
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

type Info

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

type License

type License struct {
	Name       string `json:"name" yaml:"name"`
	Identifier string `json:"identifier,omitempty" yaml:"identifier,omitempty"`
	URL        string `json:"url,omitempty" yaml:"url,omitempty"`
}
type Link struct {
	OperationRef string         `json:"operationRef,omitempty" yaml:"operationRef,omitempty"`
	OperationId  string         `json:"operationId,omitempty" yaml:"operationId,omitempty"`
	Parameters   map[string]any `json:"parameters,omitempty" yaml:"parameters,omitempty"`
	RequestBody  any            `json:"requestBody,omitempty" yaml:"requestBody,omitempty"`
	Description  string         `json:"description,omitempty" yaml:"description,omitempty"`
}

type LinkOrRef

type LinkOrRef struct {
	*Link
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

type MediaType

type MediaType struct {
	Schema     *SchemaOrRef            `json:"schema,omitempty" yaml:"schema,omitempty"`
	ItemSchema *SchemaOrRef            `json:"itemSchema,omitempty" yaml:"itemSchema,omitempty"`
	Example    any                     `json:"example,omitempty" yaml:"example,omitempty"`
	Examples   map[string]ExampleOrRef `json:"examples,omitempty" yaml:"examples,omitempty"`
}

type MediaTypeOrRef

type MediaTypeOrRef struct {
	*MediaType
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

type OAuthFlow

type OAuthFlow struct {
	AuthorizationURL       string                 `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"`
	DeviceAuthorizationURL string                 `json:"deviceAuthorizationUrl,omitempty" yaml:"deviceAuthorizationUrl,omitempty"`
	TokenURL               string                 `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"`
	RefreshURL             string                 `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"`
	Scopes                 map[string]string      `json:"scopes" yaml:"scopes"`
	Extensions             map[string]interface{} `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}

type OAuthFlows

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

type Operation

type Operation struct {
	Tags         []string                 `json:"tags,omitempty" yaml:"tags,omitempty"`
	Summary      string                   `json:"summary,omitempty" yaml:"summary,omitempty"`
	Description  string                   `json:"description,omitempty" yaml:"description,omitempty"`
	ExternalDocs *ExternalDocs            `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
	OperationID  string                   `json:"operationId,omitempty" yaml:"operationId,omitempty"`
	Parameters   []ParameterOrRef         `json:"parameters,omitempty" yaml:"parameters,omitempty"`
	RequestBody  *RequestBodyOrRef        `json:"requestBody,omitempty" yaml:"requestBody,omitempty"`
	Responses    map[string]ResponseOrRef `json:"responses" yaml:"responses"`
	Callbacks    map[string]CallbackOrRef `json:"callbacks,omitempty" yaml:"callbacks,omitempty"`
	Deprecated   bool                     `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
	Security     []map[string][]string    `json:"security,omitempty" yaml:"security,omitempty"`
	Servers      []Server                 `json:"servers,omitempty" yaml:"servers,omitempty"`
}

type Parameter

type Parameter struct {
	Name          string                  `json:"name" yaml:"name"`
	In            string                  `json:"in" yaml:"in"` // "query", "header", "path", "cookie"
	Description   string                  `json:"description,omitempty" yaml:"description,omitempty"`
	Required      bool                    `json:"required,omitempty" yaml:"required,omitempty"`
	Deprecated    bool                    `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
	Style         string                  `json:"style,omitempty" yaml:"style,omitempty"`
	Explode       *bool                   `json:"explode,omitempty" yaml:"explode,omitempty"`
	AllowReserved bool                    `json:"allowReserved,omitempty" yaml:"allowReserved,omitempty"`
	Schema        *SchemaOrRef            `json:"schema,omitempty" yaml:"schema,omitempty"`
	Example       any                     `json:"example,omitempty" yaml:"example,omitempty"`
	Examples      map[string]ExampleOrRef `json:"examples,omitempty" yaml:"examples,omitempty"`
	Content       map[string]MediaType    `json:"content,omitempty" yaml:"content,omitempty"`
}

type ParameterOrRef

type ParameterOrRef struct {
	*Parameter
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

type PathItem

type PathItem struct {
	Summary              string                `json:"summary,omitempty" yaml:"summary,omitempty"`
	Description          string                `json:"description,omitempty" yaml:"description,omitempty"`
	Get                  *Operation            `json:"get,omitempty" yaml:"get,omitempty"`
	Put                  *Operation            `json:"put,omitempty" yaml:"put,omitempty"`
	Post                 *Operation            `json:"post,omitempty" yaml:"post,omitempty"`
	Delete               *Operation            `json:"delete,omitempty" yaml:"delete,omitempty"`
	Options              *Operation            `json:"options,omitempty" yaml:"options,omitempty"`
	Head                 *Operation            `json:"head,omitempty" yaml:"head,omitempty"`
	Patch                *Operation            `json:"patch,omitempty" yaml:"patch,omitempty"`
	Trace                *Operation            `json:"trace,omitempty" yaml:"trace,omitempty"`
	Query                *Operation            `json:"query,omitempty" yaml:"query,omitempty"`
	AdditionalOperations map[string]*Operation `json:"additionalOperations,omitempty" yaml:"additionalOperations,omitempty"`
	Servers              []Server              `json:"servers,omitempty" yaml:"servers,omitempty"`
	Parameters           []ParameterOrRef      `json:"parameters,omitempty" yaml:"parameters,omitempty"`
}

type Paths

type Paths map[string]PathItem

func (*Paths) AddOperation

func (ps *Paths) AddOperation(path string, method string, operation Operation)

AddOperation adds an HTTP operation (GET, POST, PUT, etc.) to a path in the OpenAPI specification. The method parameter should be lowercase (get, post, put, delete, patch, options, head, trace). Creates a new PathItem if the path doesn't exist.

func (*Paths) SetPathInfo

func (ps *Paths) SetPathInfo(path string, summary string, description string, parameters []ParameterOrRef, servers []Server)

SetPathInfo sets or updates path-level information in the OpenAPI specification. Allows setting common parameters, servers, and descriptions that apply to all operations on a path. Creates a new PathItem if the path doesn't exist.

type Reference

type Reference struct {
	Ref         string `json:"$ref" yaml:"$ref"`
	Summary     string `json:"summary,omitempty" yaml:"summary,omitempty"`
	Description string `json:"description,omitempty" yaml:"description,omitempty"`
}

type RequestBody

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

type RequestBodyOrRef

type RequestBodyOrRef struct {
	*RequestBody
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

type Response

type Response struct {
	Summary     string                 `json:"summary,omitempty" yaml:"summary,omitempty"`
	Description string                 `json:"description,omitempty" yaml:"description,omitempty"`
	Headers     map[string]HeaderOrRef `json:"headers,omitempty" yaml:"headers,omitempty"`
	Content     map[string]MediaType   `json:"content,omitempty" yaml:"content,omitempty"`
	Links       map[string]LinkOrRef   `json:"links,omitempty" yaml:"links,omitempty"`
}

type ResponseOrRef

type ResponseOrRef struct {
	*Response
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

type Schema

type Schema struct {
	Schema        string         `json:"$schema,omitempty" yaml:"$schema,omitempty"`
	Discriminator *Discriminator `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
	XML           *XML           `json:"xml,omitempty" yaml:"xml,omitempty"`
	ExternalDocs  *ExternalDocs  `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`

	// Core JSON Schema fields (2020-12 subset commonly used in OAS 3.x)
	Title            string   `json:"title,omitempty" yaml:"title,omitempty"`
	MultipleOf       *float64 `json:"multipleOf,omitempty,omitzero" yaml:"multipleOf,omitempty,omitzero"`
	Maximum          *float64 `json:"maximum,omitempty,omitzero" yaml:"maximum,omitempty,omitzero"`
	ExclusiveMaximum *float64 `json:"exclusiveMaximum,omitempty,omitzero" yaml:"exclusiveMaximum,omitempty,omitzero"`
	Minimum          *float64 `json:"minimum,omitempty,omitzero" yaml:"minimum,omitempty,omitzero"`
	ExclusiveMinimum *float64 `json:"exclusiveMinimum,omitempty,omitzero" yaml:"exclusiveMinimum,omitempty,omitzero"`
	MaxLength        *int     `json:"maxLength,omitempty,omitzero" yaml:"maxLength,omitempty,omitzero"`
	MinLength        *int     `json:"minLength,omitempty,omitzero" yaml:"minLength,omitempty,omitzero"`
	Pattern          string   `json:"pattern,omitempty" yaml:"pattern,omitempty"`

	// Arrays
	Items       *SchemaOrRef `json:"items,omitempty" yaml:"items,omitempty"`
	MaxItems    *int         `json:"maxItems,omitempty,omitzero" yaml:"maxItems,omitempty,omitzero"`
	MinItems    *int         `json:"minItems,omitempty,omitzero" yaml:"minItems,omitempty,omitzero"`
	UniqueItems bool         `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`

	// Objects
	Properties map[string]SchemaOrRef `json:"properties,omitempty" yaml:"properties,omitempty"`
	// AdditionalProperties can be bool or SchemaOrRef
	AdditionalProperties any      `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
	Required             []string `json:"required,omitempty" yaml:"required,omitempty"`
	MaxProperties        *int     `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
	MinProperties        *int     `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`

	// General
	Type        string             `json:"type,omitempty" yaml:"type,omitempty"`
	Format      string             `json:"format,omitempty" yaml:"format,omitempty"`
	Enum        []any              `json:"enum,omitempty" yaml:"enum,omitempty"`
	Const       any                `json:"const,omitempty" yaml:"const,omitempty"`
	Nullable    bool               `json:"nullable,omitempty" yaml:"nullable,omitempty"`
	ReadOnly    bool               `json:"readOnly,omitempty" yaml:"readOnly,omitempty"`
	WriteOnly   bool               `json:"writeOnly,omitempty" yaml:"writeOnly,omitempty"`
	Description string             `json:"description,omitempty" yaml:"description,omitempty"`
	Default     any                `json:"default,omitempty" yaml:"default,omitempty"`
	Example     any                `json:"example,omitempty" yaml:"example,omitempty"`
	Examples    map[string]Example `json:"examples,omitempty" yaml:"examples,omitempty"`

	// Composition
	AllOf []SchemaOrRef `json:"allOf,omitempty" yaml:"allOf,omitempty"`
	OneOf []SchemaOrRef `json:"oneOf,omitempty" yaml:"oneOf,omitempty"`
	AnyOf []SchemaOrRef `json:"anyOf,omitempty" yaml:"anyOf,omitempty"`
	Not   *SchemaOrRef  `json:"not,omitempty" yaml:"not,omitempty"`

	// Misc OAS-specific
	Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
}

func (*Schema) ToJSON

func (s *Schema) ToJSON() (string, error)

ToJSON converts a Schema to formatted JSON string with indentation. Returns the JSON string or an error if marshaling fails.

func (*Schema) ToJSONCompact

func (s *Schema) ToJSONCompact() (string, error)

ToJSONCompact converts a Schema to compact JSON string without indentation. Returns the JSON string or an error if marshaling fails.

func (*Schema) ToYAML

func (s *Schema) ToYAML() (string, error)

ToYAML converts a Schema to YAML string. Returns the YAML string or an error if marshaling fails.

type SchemaOrRef

type SchemaOrRef struct {
	*Schema
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

func (SchemaOrRef) MarshalToJSON

func (s SchemaOrRef) MarshalToJSON() ([]byte, error)

MarshalToJSON marshals a SchemaOrRef to JSON format. Handles both schema references ($ref) and inline schema definitions.

func (SchemaOrRef) MarshalToYAML

func (s SchemaOrRef) MarshalToYAML() ([]byte, error)

MarshalToYAML marshals a SchemaOrRef to YAML format. Handles both schema references ($ref) and inline schema definitions.

type SecurityScheme

type SecurityScheme struct {
	Type              string         `json:"type" yaml:"type"` // "apiKey", "http", "oauth2", "openIdConnect"
	Description       string         `json:"description,omitempty" yaml:"description,omitempty"`
	Name              string         `json:"name,omitempty" yaml:"name,omitempty"`
	In                string         `json:"in,omitempty" yaml:"in,omitempty"` // "query", "header", "cookie"
	Scheme            string         `json:"scheme,omitempty" yaml:"scheme,omitempty"`
	BearerFormat      string         `json:"bearerFormat,omitempty" yaml:"bearerFormat,omitempty"`
	Flows             *OAuthFlows    `json:"flows,omitempty" yaml:"flows,omitempty"`
	OpenIdConnectURL  string         `json:"openIdConnectUrl,omitempty" yaml:"openIdConnectUrl,omitempty"`
	OAuth2MetadataUrl string         `json:"oauth2MetadataUrl,omitempty" yaml:"oauth2MetadataUrl,omitempty"`
	Extensions        map[string]any `json:"extensions,omitempty" yaml:"extensions,omitempty"`
	Deprecated        bool           `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
}

type SecuritySchemeOrRef

type SecuritySchemeOrRef struct {
	*SecurityScheme
	Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

type Server

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

type ServerVariable

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

type Tag

type Tag struct {
	Name         string        `json:"name" yaml:"name"`
	Summary      string        `json:"summary,omitempty" yaml:"summary,omitempty"`
	Description  string        `json:"description,omitempty" yaml:"description,omitempty"`
	ExternalDocs *ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
	Parent       string        `json:"parent,omitempty" yaml:"parent,omitempty"`
	Kind         string        `json:"kind,omitempty" yaml:"kind,omitempty"`
}

type XML

type XML struct {
	NodeType  string `json:"nodeType,omitempty" yaml:"nodeType,omitempty"`
	Name      string `json:"name,omitempty" yaml:"name,omitempty"`
	Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
	Prefix    string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
}

Jump to

Keyboard shortcuts

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