|
| 1 | +# Add OpenAPI Decorators to Flask-RESTx Endpoints |
| 2 | + |
| 3 | +This example demonstrates how to use Codegen to automatically add OpenAPI decorators (`@response` and `@expect`) to Flask-RESTx API endpoints. The migration script analyzes existing code patterns and adds appropriate decorators to improve API documentation. |
| 4 | + |
| 5 | +> [!NOTE] |
| 6 | +> This codemod helps maintain consistent API documentation by automatically analyzing endpoint behavior and adding appropriate OpenAPI decorators. |
| 7 | +
|
| 8 | +## How the Migration Script Works |
| 9 | + |
| 10 | +The script automates the documentation process in several key steps: |
| 11 | + |
| 12 | +1. **Resource Class Detection** |
| 13 | + ```python |
| 14 | + for cls in codebase.classes: |
| 15 | + if cls.is_subclass_of("Resource"): |
| 16 | + # Process Flask-RESTx resource classes |
| 17 | + ``` |
| 18 | + - Identifies Flask-RESTx resource classes |
| 19 | + - Analyzes HTTP method handlers (get, post, put, patch, delete) |
| 20 | + - Determines which decorators are missing |
| 21 | + |
| 22 | +2. **Response Analysis** |
| 23 | + ```python |
| 24 | + response_schemas = analyze_method_returns(method) |
| 25 | + ``` |
| 26 | + - Analyzes return statements |
| 27 | + - Extracts response codes and schemas |
| 28 | + - Handles error responses from `http_error` calls |
| 29 | + - Processes existing `@doc` decorators |
| 30 | + |
| 31 | +3. **Parameter Analysis** |
| 32 | + ```python |
| 33 | + expect_schema = analyze_method_params(method) |
| 34 | + ``` |
| 35 | + - Analyzes request parameter usage |
| 36 | + - Detects JSON request body schemas |
| 37 | + - Processes existing `@expect` decorators |
| 38 | + |
| 39 | +## Why This Makes Documentation Easy |
| 40 | + |
| 41 | +1. **Automated Analysis** |
| 42 | + - Automatically detects API patterns |
| 43 | + - Infers response and request schemas |
| 44 | + - No manual documentation required |
| 45 | + |
| 46 | +2. **Consistent Documentation** |
| 47 | + - Ensures all endpoints are documented |
| 48 | + - Maintains consistent decorator usage |
| 49 | + - Preserves existing decorators |
| 50 | + |
| 51 | +3. **Intelligent Schema Detection** |
| 52 | + - Analyzes model fields |
| 53 | + - Detects request parameter types |
| 54 | + - Handles nested objects |
| 55 | + |
| 56 | +## Common Documentation Patterns |
| 57 | + |
| 58 | +### Response Decorators |
| 59 | +```python |
| 60 | +# Before |
| 61 | +@ns.route('/endpoint') |
| 62 | +class MyResource(Resource): |
| 63 | + def get(self): |
| 64 | + return {'data': result} |
| 65 | + |
| 66 | +# After |
| 67 | +@ns.route('/endpoint') |
| 68 | +class MyResource(Resource): |
| 69 | + @ns.response(200, 'Success', {'data': {'type': 'any'}}) |
| 70 | + def get(self): |
| 71 | + return {'data': result} |
| 72 | +``` |
| 73 | + |
| 74 | +### Request Expect Decorators |
| 75 | +```python |
| 76 | +# Before |
| 77 | +@ns.route('/endpoint') |
| 78 | +class MyResource(Resource): |
| 79 | + def post(self): |
| 80 | + data = request.json['name'] |
| 81 | + return {'status': 'success'} |
| 82 | + |
| 83 | +# After |
| 84 | +@ns.route('/endpoint') |
| 85 | +class MyResource(Resource): |
| 86 | + @ns.expect({'name': {'type': 'any', 'required': True}}) |
| 87 | + @ns.response(200, 'Success', {'status': {'type': 'any'}}) |
| 88 | + def post(self): |
| 89 | + data = request.json['name'] |
| 90 | + return {'status': 'success'} |
| 91 | +``` |
| 92 | + |
| 93 | +## Key Benefits to Note |
| 94 | + |
| 95 | +1. **Better API Documentation** |
| 96 | + - Clear response schemas |
| 97 | + - Documented request parameters |
| 98 | + - Improved API explorer experience |
| 99 | + |
| 100 | +2. **Consistent Error Handling** |
| 101 | + - Documented error responses |
| 102 | + - Clear status codes |
| 103 | + - Better client integration |
| 104 | + |
| 105 | +3. **Time Savings** |
| 106 | + - Automated decorator generation |
| 107 | + - Reduced manual documentation work |
| 108 | + - Easier maintenance |
| 109 | + |
| 110 | +## Running the Migration |
| 111 | + |
| 112 | +```bash |
| 113 | +# Install Codegen |
| 114 | +pip install codegen |
| 115 | + |
| 116 | +# Run the migration |
| 117 | +python run.py |
| 118 | +``` |
| 119 | + |
| 120 | +The script will: |
| 121 | +1. Initialize the codebase |
| 122 | +2. Find Flask-RESTx resource classes |
| 123 | +3. Analyze methods and add decorators |
| 124 | +4. Print detailed analytics about missing decorators |
| 125 | + |
| 126 | +## Learn More |
| 127 | + |
| 128 | +- [Flask-RESTx Documentation](https://flask-restx.readthedocs.io/) |
| 129 | +- [OpenAPI Specification](https://swagger.io/specification/) |
| 130 | +- [Codegen Documentation](https://docs.codegen.com) |
| 131 | + |
| 132 | +## Contributing |
| 133 | + |
| 134 | +Feel free to submit issues and enhancement requests! |
0 commit comments