Swagger Documentation Guide
This guide outlines the standards for configuring and maintaining Swagger API documentation within our codebase. Following these guidelines ensures consistent, clear, and well-organized documentation across all services.
General Rules for Swagger Configuration
Using Swagger Annotations:
- Annotation Priority: Where possible, use Swagger annotations (such as
@ApiOperation,@ApiResponses, and@ApiParam) directly in the code to define API metadata, parameter details, and response examples. - When to Use: Use annotations for descriptions and examples that are concise, particularly for parameterized details or small response examples.
- Annotation Priority: Where possible, use Swagger annotations (such as
Swagger Customizer:
- Purpose: Use the customizer to manage complex configurations or examples that would clutter the code if embedded directly.
- When to Use: If response examples, parameter values, or configurations are extensive or involve nested structures, use a customizer. This keeps annotations manageable and improves code readability.
Documentation Conventions
Metadata Documentation
Endpoint Descriptions:
- Use
@ApiOperationto describe the functionality of each endpoint clearly. Keep descriptions concise and action-oriented (e.g., "Creates a new contract," "Retrieves a list of contracts").
- Use
Response and Parameter Documentation:
- Specify all possible responses with
@ApiResponses, noting the status code and brief description of each. For example, describe a 201 status as "Successful creation" or a 400 error as "Invalid input." - Define parameters with
@ApiParam, detailing attributes likerequiredstatus and brief examples if they enhance clarity.
- Specify all possible responses with
Examples and Complex Responses
- Using the AbstractSampleObjectFactory:
- Complex examples and sample responses should be created using factory methods. Developers can find
AbstractSampleObjectFactoryin theadb-commonmodule. This approach keeps the main code cleaner and makes reusable examples accessible across services. - When adding new examples, ensure each factory method provides a clear description of the sample data’s context and a representative value.
- Complex examples and sample responses should be created using factory methods. Developers can find
Organizing Code with Factory Classes
- Purpose of Factory Classes:
- Factory classes manage examples for specific response types or request structures, especially when data structures are extensive or nested. This approach enables code reuse and consistency.
- Naming and Documentation:
- Name each factory class according to the type of object it creates examples for (e.g.,
TicketSampleObjectFactoryfor Ticket-related examples). - Document each example with a descriptive method name and comments explaining its use (e.g., a creation request example vs. a response example). This gives developers clarity on when and how to use each method.
- Name each factory class according to the type of object it creates examples for (e.g.,
Summary of Rules
Annotations for Simplicity:
- Use Swagger annotations for direct metadata, parameters, and simple examples.
Factory Methods for Complexity:
- For complex or reusable examples, create and reference factory methods within a dedicated factory class, ensuring code clarity and ease of maintenance.
Customizer for Special Configurations:
- If additional customization or configuration is needed beyond the capabilities of annotations (such as global settings or additional response handling), apply a Swagger customizer to handle these requirements centrally.