Skip to content

Commit 6f8ecb2

Browse files
author
Doug Borg
committed
refactor: reduce complexity in model_generator.type_converter (fixes #98)
## Summary Refactored `type_converter` function to eliminate high cyclomatic complexity, code duplication, and fragile import aggregation. The function is now more maintainable, testable, and easier to extend with future OpenAPI 3.1 features. ## Changes ### Extracted Helper Functions - `_normalize_schema_type()`: Unified handling of DataType enum, strings, and lists - `_is_type()`: Simplified type checking across all schema type representations - `_handle_format_conversions()`: Consolidated UUID/datetime logic (eliminated 3x duplication) - `_wrap_optional()`: Replaced scattered pre_type/post_type pattern - `_collect_unique_imports()`: Safe import aggregation without blind indexing - `_convert_primitive_type()`: Handles string, int, float, bool, object, null types - `_convert_array_type()`: Isolated array type handling with items processing - `_convert_composite_schema()`: Unified allOf/oneOf/anyOf composition logic ### Main Function Improvements - Reduced from ~270 lines to 53 lines (80% reduction) - Cyclomatic complexity reduced from >16 to <10 - Removed noqa C901 suppression comment - Clear separation of concerns with early returns ## Benefits - **Maintainability**: Each responsibility isolated in well-documented helpers - **Safety**: No more direct indexing of potentially empty import lists - **Testability**: Individual functions can be tested independently - **Extensibility**: Adding new types/formats now straightforward - **Zero behavioral changes**: All 250 tests pass (55 model_generator specific) ## Metrics | Metric | Before | After | Improvement | |---------------------------|--------|-------|--------------------| | Function size | ~270 | 53 | 80% reduction | | Cyclomatic complexity | >16 | <10 | Below threshold | | Code duplication (UUID) | 3x | 1x | 100% eliminated | | Import aggregation safety | Fragile| Safe | No IndexError risk | Closes #98
1 parent f356cf0 commit 6f8ecb2

File tree

1 file changed

+286
-242
lines changed

1 file changed

+286
-242
lines changed

0 commit comments

Comments
 (0)