Swag is a documentation tool for Go that generates standardized API specification files by parsing declarative annotations within source code. It functions by analyzing source files to extract metadata from comments and function signatures, transforming them into machine-readable formats such as JSON or YAML. This process ensures that technical documentation remains synchronized with the underlying code structure throughout the development lifecycle.
The tool distinguishes itself through its ability to perform static source code parsing and type-system reflection, which allows it to map complex data structures and generic type definitions directly to API schemas. It supports the definition of security requirements, including OAuth2 and API key authentication, by translating embedded metadata into standardized security definitions. This enables the clear communication of access requirements and protected endpoints to external documentation consumers.
Beyond core generation, the project provides utilities for managing API contracts and documentation consistency. It includes configuration options to override default type mappings or filter specific fields, as well as command-line tools to enforce standardized formatting of documentation comments across a codebase.