Documentation
¶
Overview ¶
Package scanner provides code scanning capabilities to extract OpenAPI information from Go source code using structured comments (directives).
Index ¶
- Constants
- type Config
- type ContactInfo
- type DiscriminatorInfo
- type EmbeddedTypeInfo
- type EnumInfo
- type ExternalDocsInfo
- type FieldInfo
- type LicenseInfo
- type MetaInfo
- type OAuthFlowInfo
- type OAuthFlowsInfo
- type Option
- type ResponseInfo
- type RouteInfo
- type Scanner
- type SecuritySchemeInfo
- type StructInfo
- type TagInfo
Constants ¶
const ( // MetaDirective marks a comment block as containing OpenAPI metadata MetaDirective = "swagger:meta" // ModelDirective marks a struct as an OpenAPI schema/model ModelDirective = "swagger:model" // ParameterDirective marks a struct as OpenAPI parameters ParameterDirective = "swagger:parameters" // RouteDirective marks a function as an API endpoint RouteDirective = "swagger:route" // EnumDirective marks a type as an enum EnumDirective = "swagger:enum" // IgnoreDirective marks a field to be ignored IgnoreDirective = "swagger:ignore" // OneOfModelDirective marks a struct as a oneOf schema (polymorphic union) OneOfModelDirective = "swagger:oneOf" // AnyOfModelDirective marks a struct as an anyOf schema AnyOfModelDirective = "swagger:anyOf" // OneOfOptionDirective marks an embedded field as a oneOf option OneOfOptionDirective = "swagger:oneOfOption" // AnyOfOptionDirective marks an embedded field as an anyOf option AnyOfOptionDirective = "swagger:anyOfOption" )
Core directives for top-level OpenAPI elements
const ( TitleDirective = "Title:" VersionDirective = "Version:" DescriptionDirective = "Description:" TermsOfServiceDirective = "TermsOfService:" ContactDirective = "Contact:" LicenseDirective = "License:" HostDirective = "Host:" BasePathDirective = "BasePath:" ExternalDocsDirective = "ExternalDocs:" TagsDirective = "Tags:" SecuritySchemesDirective = "SecuritySchemes:" SecurityDirective = "Security:" ConsumesDirective = "Consumes:" ProducesDirective = "Produces:" ResponsesDirective = "Responses:" ParametersDirective = "Parameters:" SummaryDirective = "Summary:" ServersDirective = "Servers:" )
Meta section directives
const ( OneOfDirective = "oneOf:" AllOfDirective = "allOf:" AnyOfDirective = "anyOf:" )
Schema composition directives (legacy inline style)
const ( ExampleDirective = "example:" DefaultDirective = "default:" RequiredDirective = "required:" NullableDirective = "nullable:" FormatDirective = "format:" InDirective = "in:" MinimumDirective = "min:" MaximumDirective = "max:" MinLengthDirective = "minLength:" MaxLengthDirective = "maxLength:" PatternDirective = "pattern:" MinItemsDirective = "minItems:" MaxItemsDirective = "maxItems:" UniqueItemsDirective = "uniqueItems:" ReadOnlyDirective = "readOnly:" WriteOnlyDirective = "writeOnly:" )
Field-level directives
const ( SummaryFieldDirective = "summary:" DescriptionFieldDirective = "description:" DeprecatedFieldDirective = "deprecated" IgnoredParametersDirective = "IgnoredParameters:" )
Route-specific directives
const ( // SpecDirective specifies which spec(s) an element belongs to // Format: spec: name1 name2 name3 SpecDirective = "spec:" // DefaultSpec is the name of the default spec for elements without spec: directive DefaultSpec = "default" )
Multi-spec directive
const ( SwaggerPrefix = "swagger:" DashPrefix = "-" )
Common prefixes and patterns
const ( MethodGet = "GET" MethodPost = "POST" MethodPut = "PUT" MethodDelete = "DELETE" MethodPatch = "PATCH" MethodHead = "HEAD" MethodOptions = "OPTIONS" )
HTTP methods
const ( ContentTypeJSON = "application/json" ContentTypeXML = "application/xml" ContentTypeForm = "application/x-www-form-urlencoded" ContentTypeMultipart = "multipart/form-data" )
Content types
const ( SecurityTypeAPIKey = "apiKey" SecurityTypeHTTP = "http" SecurityTypeOAuth2 = "oauth2" SecurityTypeOpenIDConnect = "openIdConnect" )
Security scheme types
const ( ParamInQuery = "query" ParamInHeader = "header" ParamInPath = "path" ParamInCookie = "cookie" )
Parameter locations
const ( TypeString = "string" TypeNumber = "number" TypeInteger = "integer" TypeBoolean = "boolean" TypeArray = "array" TypeObject = "object" )
Schema types
const ( FormatDate = "date" FormatDateTime = "date-time" FormatPassword = "password" FormatByte = "byte" // For base64 encoded files FormatBinary = "binary" // For file uploads in multipart/form-data FormatEmail = "email" FormatUUID = "uuid" FormatURI = "uri" FormatInt32 = "int32" FormatInt64 = "int64" FormatFloat = "float" FormatDouble = "double" )
String formats
const ( // DiscriminatorDirective specifies the property name for discriminator // Format: discriminator: propertyName DiscriminatorDirective = "discriminator:" )
Discriminator directives for oneOf/anyOf polymorphism
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Pattern is the package pattern to scan (e.g., "./...", "./api/...")
Pattern string
// Dir is the directory path to scan from
Dir string
// IgnorePaths contains path patterns to exclude during scanning
IgnorePaths []string
}
Config holds configuration options for the scanner.
type ContactInfo ¶
ContactInfo represents contact information for the API.
type DiscriminatorInfo ¶ added in v0.1.3
type DiscriminatorInfo struct {
PropertyName string // The property name that holds the discriminating value
Mapping map[string]string // Maps discriminator values to schema names
}
DiscriminatorInfo contains discriminator configuration for oneOf/anyOf schemas.
type EmbeddedTypeInfo ¶ added in v0.1.2
type EmbeddedTypeInfo struct {
Name string // Type name (e.g., "pagination.Pagination")
Index int // Position in the struct where it was declared
}
EmbeddedTypeInfo contains information about an embedded type with its position.
type EnumInfo ¶
type EnumInfo struct {
TypeName string
BaseType string
Example any
Values map[string]any
Description string
SourceFile string
}
EnumInfo contains information about an enum type.
type ExternalDocsInfo ¶
ExternalDocsInfo represents external documentation.
type FieldInfo ¶
type FieldInfo struct {
Name string
Type string
Description string
Default string
Example string
Required bool
Nullable bool
Validations map[string]string
Enum string
Tags map[string]string
IsArray bool
IsPointer bool
IsMap bool
MapKeyType string
IsRequestBody bool
In string // Parameter location: query, path, header, cookie, body
InlineStruct *StructInfo
IsInlineStruct bool
HasOmitempty bool
ExplicitRequired bool
ExplicitOptional bool
Index int // Position in the original struct declaration (for ordering)
}
FieldInfo contains information about a struct field.
type LicenseInfo ¶
LicenseInfo represents license information for the API.
type MetaInfo ¶
type MetaInfo struct {
Title string
Description string
Version string
TermsOfService string
Contact *ContactInfo
License *LicenseInfo
Host string
BasePath string
ExternalDocs *ExternalDocsInfo
Tags []*TagInfo
SecuritySchemes map[string]*SecuritySchemeInfo
Consumes []string
Produces []string
Schemes []string
Specs []string // Multi-spec: which specs this meta belongs to (empty = general/default)
}
MetaInfo contains OpenAPI specification metadata extracted from swagger:meta directive.
type OAuthFlowInfo ¶
type OAuthFlowInfo struct {
AuthorizationURL string
TokenURL string
RefreshURL string
Scopes map[string]string
}
OAuthFlowInfo represents a single OAuth2 flow configuration.
type OAuthFlowsInfo ¶
type OAuthFlowsInfo struct {
Implicit *OAuthFlowInfo
Password *OAuthFlowInfo
ClientCredentials *OAuthFlowInfo
AuthorizationCode *OAuthFlowInfo
}
OAuthFlowsInfo contains OAuth2 flow configurations.
type Option ¶
type Option func(*Config)
Option is a function type for configuring the Scanner.
func WithIgnorePaths ¶
WithIgnorePaths sets the path patterns to ignore during scanning.
func WithPattern ¶
WithPattern sets the package pattern to scan.
type ResponseInfo ¶
type ResponseInfo struct {
StatusCode string
Type string
Description string
IsArray bool
IsMap bool
MapKeyType string
}
ResponseInfo contains information about an API response.
type RouteInfo ¶
type RouteInfo struct {
Method string
Path string
Tags []string
OperationID string
Summary string
Description string
Deprecated bool
Responses []*ResponseInfo
Security []string
Consumes []string
Produces []string
IgnoredParameters []string
SourceFile string
Specs []string // Multi-spec: which specs this route belongs to (empty = default spec)
}
RouteInfo contains information about an API route/endpoint.
type Scanner ¶
type Scanner struct {
// Extracted data
Meta *MetaInfo // General meta (first meta without spec: directive)
Metas []*MetaInfo // All metas including spec-specific ones
Enums map[string]*EnumInfo
Structs map[string]*StructInfo
Routes map[string]*RouteInfo
// Type mappings
TypeToEnum map[string]string // Go type name -> enum name
TypeToStruct map[string]string // Go type name -> struct name
TypeAliases map[string]string // alias type name -> original type name (e.g., "model.FeeType" -> "workspace.FeeType")
// Source file mappings
EnumSources map[string]string // enum name -> source file
StructSources map[string]string // struct name -> source file
RouteSources map[string]string // operation ID -> source file
// contains filtered or unexported fields
}
Scanner scans Go source code for OpenAPI directives.
func (*Scanner) GetEnumForType ¶ added in v0.1.2
GetEnumForType finds the enum info for a type, resolving aliases if necessary.
func (*Scanner) ResolveTypeAlias ¶ added in v0.1.2
ResolveTypeAlias resolves a type name to its original type if it's an alias. Returns the original type name, or the input if it's not an alias.
type SecuritySchemeInfo ¶
type SecuritySchemeInfo struct {
Type string // apiKey, http, oauth2, openIdConnect
Description string
Name string // For apiKey
In string // For apiKey: query, header, cookie
Scheme string // For http: bearer, basic
BearerFormat string // For http bearer
Flows *OAuthFlowsInfo
OpenIdConnectUrl string
}
SecuritySchemeInfo represents a security scheme definition.
type StructInfo ¶
type StructInfo struct {
Name string
Fields []*FieldInfo
EmbeddedTypes []string // Embedded types that need to be resolved (e.g., "pagination.Pagination")
EmbeddedTypeInfos []*EmbeddedTypeInfo // Embedded types with position information
Description string
IsParameter bool
IsModel bool
SourceFile string
OneOf []string // Legacy: inline oneOf references from "oneOf:" directive
AllOf []string // Legacy: inline allOf references from "allOf:" directive
AnyOf []string // Legacy: inline anyOf references from "anyOf:" directive
Specs []string // Multi-spec: which specs this model belongs to (empty = all specs)
// New oneOf/anyOf model support (swagger:oneOf / swagger:anyOf)
IsOneOfModel bool // True if struct is marked with swagger:oneOf
IsAnyOfModel bool // True if struct is marked with swagger:anyOf
OneOfOptions []string // Types marked with swagger:oneOfOption
AnyOfOptions []string // Types marked with swagger:anyOfOption
Discriminator *DiscriminatorInfo // Discriminator configuration for polymorphism
}
StructInfo contains information about a struct marked as model or parameters.
type TagInfo ¶
type TagInfo struct {
Name string
Description string
ExternalDocs *ExternalDocsInfo
}
TagInfo represents a tag used to organize API operations.