Core Concepts
Understanding how Bytedocs works under the hood
Understanding these core concepts will help you get the most out of Bytedocs and troubleshoot any issues.
How Bytedocs Works
Bytedocs uses a multi-stage process to generate your API documentation:
1. Source Code Analysis
When Bytedocs starts, it analyzes your source code using Abstract Syntax Trees (AST):
- Go: Uses
go/parserandgo/astpackages - PHP: Uses PHP Reflection API and AST parser
- Node.js: Uses TypeScript compiler or Babel parser
- Python: Uses
astmodule - Rust: Uses
syncrate for syntax analysis
2. Route Discovery
Bytedocs connects to your framework's routing system:
Go (Gin example):
Laravel:
3. Handler Analysis
For each route, Bytedocs analyzes the handler function:
- Parameters: Path params, query strings, headers
- Request Body: Types, validation rules, required fields
- Response: Status codes, return types, examples
- Documentation: Comments, annotations, docblocks
4. Type Inference
Bytedocs builds complete schemas by analyzing types:
Example (Go):
Extracted Schema:
5. Schema Generation
All extracted information is converted into OpenAPI 3.0.3 schemas:
- Paths: All routes with parameters
- Components: Reusable schemas
- Security: Authentication schemes
- Servers: Multiple environment URLs
Auto-Detection Mechanism
How Auto-Detection Works
Step 1: Register Framework Hook
When you call SetupGinDocs() or equivalent:
Step 2: Lazy Analysis
On first request to /docs:
- Get all registered routes from framework
- For each route, find the handler function
- Locate source file using reflection
- Parse source code with AST
- Extract metadata (params, body, response)
- Build schema from extracted data
- Cache results for subsequent requests
Step 3: Serve Documentation
The generated documentation is:
- Cached in memory
- Served as JSON via API
- Rendered in React UI
What Gets Auto-Detected
✅ Automatically Detected:
- HTTP methods (GET, POST, PUT, DELETE, PATCH)
- Route paths with parameters
- Path parameters (
:id,{id}) - Query parameters (from handler code)
- Request headers (from handler code)
- Request body types and validation
- Response types and status codes
- Nested object structures
- Array and map types
- Enum types
- Required vs optional fields
❌ Not Auto-Detected (requires annotations):
- Complex business logic descriptions
- Custom authentication schemes
- Rate limiting information
- Deprecation notices
- External API dependencies
Request/Response Detection
Request Body Detection
Bytedocs detects request bodies by analyzing binding calls:
Go (Gin):
Laravel:
Node.js (Express):
Response Detection
Status Codes:
Response Types:
Multiple Response Types:
Struct Tag System
Supported Tags
JSON Tags
Example Tags
Validation Tags
Format Tags
Description Tags
Tag Processing Order
- JSON tag - Determines field name
- Example tag - Sets example value
- Binding/Validation tags - Marks required, adds constraints
- Format tag - Sets OpenAPI format
- Description tag - Adds field description
- Comments - Fallback description source
OpenAPI Schema Generation
Generated OpenAPI Structure
Schema Reusability
Bytedocs automatically identifies reusable schemas:
Caching Strategy
Memory Cache
Generated documentation is cached in memory:
Cache Invalidation
Cache is cleared when:
- Application restarts
- Config changes detected
- Manual clear command (Laravel:
php artisan bytedocs:clear)
Production Optimization
In production, disable auto-detect for better performance:
Pre-generate documentation:
Error Handling
Common Error Scenarios
1. Handler Not Found
Cause: Handler function source file not accessible
Solution: Ensure handler files are in the same module
2. Type Not Resolved
Cause: Complex type or external package
Solution: Use manual schema registration
3. Circular Reference
Cause: Recursive struct definitions
Solution: Bytedocs automatically detects and handles with $ref
Debug Mode
Enable debug logging:
This will log:
- Routes discovered
- Handler analysis results
- Schema generation steps
- Errors and warnings
Performance Considerations
Analysis Performance
- First Request: 100-500ms (depending on API size)
- Subsequent Requests: < 1ms (served from cache)
- Large APIs: 1000+ endpoints analyzed in < 2 seconds
Memory Usage
- Small API (< 50 endpoints): ~5MB
- Medium API (50-200 endpoints): ~15MB
- Large API (200-1000 endpoints): ~50MB
Optimization Tips
- Use caching in production
- Exclude unnecessary routes
- Pre-generate documentation
- Disable auto-detect in production
- Use manual registration for complex routes
Security Considerations
What Bytedocs Exposes
✅ Exposed:
- Route paths and methods
- Parameter names and types
- Request/response schemas
- Validation rules
❌ Not Exposed:
- Handler implementation logic
- Database queries
- Business logic
- Secrets or credentials
- Internal variable names
Protecting Documentation
- Enable Authentication:
- IP Whitelisting:
- Disable in Production:
What's Next?
- Configuration - Detailed configuration options
- Framework Guides - Framework-specific features
- Features - Advanced features like AI and scenarios
- Examples - Real-world examples