Configuration Reference

June 24, 2026 · View on GitHub

go-apispec auto-detects your framework and infers routes, types, and schemas with no configuration. A config file is only needed to override OpenAPI metadata, map custom types, scope the analysis, or teach the analyzer a non-standard router.

  • Config file: pass --config apispec.yaml (-c). It is parsed by spec.LoadAPISpecConfig and merged over the auto-detected framework defaults — you only specify what you want to change.
  • Inspect the effective config: apispec --output-config effective.yaml writes the fully-merged config (your file + detected framework defaults) so you can see exactly what the analyzer used.

Precedence

From highest to lowest:

  1. A *spec.APISpecConfig passed directly to the programmatic API.
  2. Your --config file (merged per section over framework defaults — a section you provide replaces that section's default; a section you omit is inherited).
  3. Auto-detected framework defaults (Default<Framework>Config()).

CLI metadata flags (--title, --api-version, license/contact, …) fill the info block. --short-names overrides shortNames only when the flag is explicitly passed.

File structure

Every section is optional. A minimal file usually sets just info and maybe typeMapping:

# apispec.yaml
info:
  title: My API
  version: 2.0.0
  description: Public REST API
  contact:
    name: API Team
    email: api@example.com
  license:
    name: Apache-2.0
    url: https://www.apache.org/licenses/LICENSE-2.0

servers:
  - url: https://api.example.com/v2

shortNames: true            # false → fully-qualified operationIds & schema names

defaults:
  requestContentType: application/json
  responseContentType: application/json
  responseStatus: 200

typeMapping:
  - goType: time.Time
    openapiType: { type: string, format: date-time }
  - goType: github.com/google/uuid.UUID
    openapiType: { type: string, format: uuid }

externalTypes:
  - name: go.mongodb.org/mongo-driver/bson/primitive.ObjectID
    openapiType: { type: string }
    description: 24-char hex ObjectID

overrides:
  - functionName: GetUser
    summary: Fetch a user
    responseStatus: 200
    responseType: User
    tags: [users]

securitySchemes:
  bearerAuth:
    type: http
    scheme: bearer
    bearerFormat: JWT

include:
  packages: ["internal/api/**"]
exclude:
  files: ["**/*_gen.go"]
  packages: ["**/mocks/**"]

Sections

OpenAPI metadata

Passed through to the spec verbatim:

KeyTypeNotes
infoobjecttitle, version, description, termsOfService, contact{name,url,email}, license{name,url}. A license block is emitted only when license.name is set.
serverslisturl, description, variables.
securitylistGlobal security requirement objects.
securitySchemesmapname → {type, scheme, bearerFormat, name, in, flows, …}. Supports http, apiKey, oauth2, openIdConnect. Auto-detected Bearer/Basic/apiKey schemes are merged in automatically; a config entry of the same name wins.
tagslistname, description, externalDocs.
externalDocsobjecturl, description.

shortNames

Controls how operationIds and schema component names are rendered (see Naming modes). Default short (true/unset). Set false for fully-qualified, module-path-prefixed names.

defaults

KeyDefaultEffect
requestContentTypeapplication/jsonMedia type for request bodies when not detected from code.
responseContentTypeapplication/jsonMedia type for responses when not detected (e.g. from w.Header().Set("Content-Type", …)).
responseStatus200Status used when a handler writes a body with no explicit code.

typeMapping

Override the schema for a Go type wherever it appears (including as a struct field or container element). Match is by the fully-qualified Go type name.

typeMapping:
  - goType: github.com/shopspring/decimal.Decimal
    openapiType: { type: string, format: decimal }

externalTypes

Declare third-party types the analyzer can't see into, so they render as a known schema instead of an empty object.

externalTypes:
  - name: github.com/gin-gonic/gin.H
    openapiType: { type: object, additionalProperties: true }

overrides

Per-operation overrides keyed by handler functionName. Anything set here wins over inference (and over doc-comment extraction for summary/description).

KeyEffect
functionNameHandler function name to match.
summary, descriptionOperation text.
responseStatusForce a status code.
responseTypeForce the response body type.
tagsOperation tags.

include / exclude

Gitignore-style glob filters scoping which code is analyzed. exclude wins over include on a conflict. Each has four lists:

include:
  files: []
  packages: ["internal/api/**", "internal/handlers/**"]
  functions: []
  types: []
exclude:
  files: ["**/*_test.go", "**/*_gen.go"]
  packages: ["**/internal/mocks/**"]

Note: the CLI also auto-excludes tests and mocks by default (--auto-exclude-tests, --auto-exclude-mocks) and restricts analysis to framework-reachable packages (--auto-include-framework-packages).

framework (advanced)

The pattern engine that maps Go calls to routes/params/bodies/responses. You rarely need this — the built-in Default<Framework>Config() patterns cover chi, gin, echo, fiber, gorilla/mux, and net/http, and multiple detected frameworks are merged automatically. Provide a framework block only to support a custom router or tweak a pattern. A section you specify replaces that section's default; omitted sections are inherited.

framework:
  routePatterns:
    - callRegex: "^(?i)(GET|POST|PUT|DELETE|PATCH)$"
      recvTypeRegex: "^github\\.com/gin-gonic/gin\\.\\*(Engine|RouterGroup)$"
      handlerArgIndex: 1
      methodFromCall: true
      pathFromArg: true
      handlerFromArg: true
  requestBodyPatterns: []
  responsePatterns: []
  paramPatterns: []
  mountPatterns: []
  contentTypePatterns: []

Each pattern embeds a common matcher (BasePattern):

KeyMatches against
callRegexThe called function/method name.
functionNameRegexThe enclosing function's name.
recvType / recvTypeRegexThe fully-qualified receiver type (e.g. github.com/go-chi/chi/v5.*Mux). The Regex form carries (/v\d)? in the defaults so versioned import paths match.
callerPkgPatterns / calleePkgPatternsCaller/callee package globs.
callerRecvTypePatterns / calleeRecvTypePatternsCaller/callee receiver-type globs.

Pattern-specific fields (arg indices, methodFromCall, pathFromArg, paramIn, isMount, …) vary by pattern kind; the authoritative reference is the Default<Framework>Config() source in internal/spec/config.go. The fastest way to author a custom pattern is to dump a framework's defaults with --output-config and edit from there.

Naming modes

ModeshortNamesoperationIdschema component
Short (default)true / unsetDocumentHandler.GetContentUser, APIResponse_User
Legacy / fully-qualifiedfalsegithub.com/org/app/http.Deps.DocumentHandler.GetContentmodule-path-qualified

Short mode strips the module path and resolves any resulting collisions by adding back the minimum package segments needed to disambiguate. Legacy mode keeps the full path (using the internal --> separator, normalized to . in component keys). Both modes are deterministic and have their own golden snapshots (expected_openapi.json vs expected_openapi_legacy.json).

Struct-tag configuration

Some behavior is configured in your Go source via struct tags, not the YAML file:

  • json tags drive field names, omitempty (→ field is optional), and ,string (→ scalar rendered as string).

  • validate: / binding: tags (go-playground/validator, gin binding) map to schema constraints: required → required; oneof=a b cenum; min/max/gte/lte → numeric bounds or string length; email/uuid/uriformat; dive,<rule> → item constraints on slices.

  • Field-level apispec tag — explicit overrides the flow analysis can't infer:

    TagsetID string `json:"tagsetId" apispec:"format=uuid,type=string"`
    
  • Struct-level apispec tag on a blank marker field — for constraints that span fields (useful for PATCH bodies):

    type UpdateProfile struct {
        _           struct{} `apispec:"minProperties=1,anyOf=displayName|bio|avatarURL"`
        DisplayName *string  `json:"displayName,omitempty"`
        Bio         *string  `json:"bio,omitempty"`
        AvatarURL   *string  `json:"avatarURL,omitempty"`
    }
    

    , separates key=value pairs; | separates field names inside anyOf.

FlagEffect
--config, -cPath to the YAML config (merged over framework defaults).
--output-config, -ocWrite the fully-merged effective config to a file.
--short-namesOverride shortNames (only when explicitly passed).
--title/-t, --api-version/-v, --description/-D, --terms/-T, --contact-name/-N, --contact-url/-U, --contact-email/-E, --license-name/-L, --license-url/-luFill the info block.
--include-package/--exclude-package (and -file/-function/-type variants)Repeatable glob filters, equivalent to the include/exclude sections.
--openapi-version, -OThe openapi: version field (default 3.1.1).

Run apispec --help for the full flag list.