Skip to content

Refactor Example Generation with Improved Type Handling and Unit Tests#5

Merged
rahulkaushal04 merged 13 commits intomainfrom
feature/add-example-request-body
May 9, 2025
Merged

Refactor Example Generation with Improved Type Handling and Unit Tests#5
rahulkaushal04 merged 13 commits intomainfrom
feature/add-example-request-body

Conversation

@rahulkaushal04
Copy link
Copy Markdown
Owner

Description

This PR refactors the test value and example generation logic across the codebase with improved type handling and comprehensive unit tests. Key changes include:

  1. Added generate_test_value function in utils.py to generate test values matching regex patterns
  2. Refactored example generation in RequestBody to support additional property types (string, number, boolean, object, array)
  3. Enhanced body.py with comprehensive type hints and docstrings
  4. Updated parameter handling to use the new test value generation
  5. Added comprehensive unit tests for all new functionality
  6. Simplified function signatures for improved maintainability
  7. Updated documentation for the new API references

Related Issue

Type of Change

  • Code refactoring (no functional changes)
  • Tests (adding or improving tests)
  • Documentation update
  • Performance improvement

Checklist

  • My code follows the code style of this project
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • My changes generate no new warnings or errors
  • I have checked for potential compatibility issues
  • I have reviewed my code for potential security issues

Screenshots (if applicable)

Additional Context

Testing Instructions

  1. Run the test suite to verify that all tests pass: pytest src/tests/

  2. Check that example generation works as expected by using the RequestBody and Parameter dataclasses

  3. Steps to reproduce or test the changes

  4. Expected behavior

Dependencies

…y to utilize it

- Introduced generate_test_value function for generating test values based on regex patterns and length constraints.
- Removed the previous implementation of generate_test_value from parameter.py.
- Updated test cases to reflect the new behavior of returning "example" for invalid patterns.
…ance RequestBody with example generation functionality

- Updated type hints in `body.py`, `endpoint.py`, `parameter.py`, `base_parser.py`, and `openapi_parser.py` to use built-in `dict` and `list` instead of `Dict` and `List`.
- Added `__post_init__` method in `RequestBody` to generate example values if not provided.
- Implemented helper functions for generating example JSON bodies and properties based on schema definitions.
…signatures and add support for object properties

- Updated `generate_json_example` to accept only properties, removing the resolved schema parameter.
- Introduced `generate_object_property` to handle object type properties in example generation.
- Enhanced example generation logic to include object properties based on schema definitions.
…l property types and improve structure

- Introduced a handler for different content types in `generate_example_body`.
- Added `generate_property_value` to streamline property value generation for various types.
- Enhanced `generate_object_property` to handle `additionalProperties` in schemas.
- Implemented `generate_array_property` to support array types in example generation.
- Updated the return type of `generate_test_value` to always return a string instead of an optional type.
- Enhanced the `generate_string_property` function to provide a default format value of "none" when no format is specified in the schema.
- Removed the API Reference link from the README.
- Simplified the security workflow by removing pip-audit and adjusting the Bandit scan configuration.
- Added detailed API reference documentation for the Parser module, including usage examples and method descriptions.
- Added .vscode and *.ipynb to .gitignore for better file management.
- Created MANIFEST.in to include requirements.txt and VERSION in the package distribution.
- Removed commented-out code in OpenAPIParser for improved readability and clarity.
- Enhanced request body handling in OpenAPIParser to prefer JSON content types and improved property resolution logic.
- Introduced new test cases for the generate_test_value function to validate correct behavior with valid and invalid patterns, as well as exception handling.
- Removed outdated test cases from test_parameter.py that were previously testing generate_test_value.
- Improved error handling in `generate_property_value` and related functions to ensure consistent fallback values.
- Streamlined the logic for generating example values for various property types, including strings, numbers, booleans, objects, and arrays.
- Introduced a new test suite in `test_body.py` to validate example generation across different schemas and property configurations, ensuring robustness and correctness.
…y.py

- Enhanced documentation for `generate_example_body`, `generate_json_example`, `generate_property_value`, `generate_string_property`, `generate_numerical_property`, `generate_object_property`, and `generate_array_property` functions to clarify their purpose, arguments, and return types.
- Updated setup.cfg to include mypy configuration for better type checking and unused config warnings.
- Modified the Bandit security scan command in the GitHub Actions workflow to use a custom configuration file (bandit.yaml) for improved scanning accuracy and flexibility.
- Changed the Python version from 3.8 to 3.9 in both the CI and security workflows to ensure compatibility with the latest dependencies and features.
@rahulkaushal04 rahulkaushal04 merged commit 94f8ecc into main May 9, 2025
4 checks passed
@rahulkaushal04 rahulkaushal04 deleted the feature/add-example-request-body branch May 9, 2025 15:28
rahulkaushal04 added a commit that referenced this pull request May 10, 2025
#5)

Add generate_test_value function and refactor type hints

- Implement generate_test_value in utils.py for regex-based test value generation
- Refactor dataclasses to use built-in `dict` and `list` types instead of `Dict` and `List`
- Update type hints across modules (body.py, endpoint.py, parameter.py, base_parser.py, openapi_parser.py)
- Add appropriate unit tests for the new utility function

Enhance RequestBody with intelligent example generation

- Add __post_init__ method to RequestBody to auto-generate examples when not provided
- Implement comprehensive example generation for JSON schemas based on property types
- Support various data types (string, number, integer, boolean, object, array) with format-specific examples
- Add support for schema constraints (min/max length, pattern, enum values, etc.)

Improve code quality and project structure

- Enhance error handling in example generation functions with consistent fallbacks
- Add detailed docstrings for all new functions with proper type annotations
- Update test cases to verify example generation behavior across different schemas
- Update project configuration files (.gitignore, MANIFEST.in, setup.cfg)

Update dependencies and CI configuration

- Update Python version from 3.8 to 3.9 in CI workflows for better compatibility
- Upgrade urllib3 to version 2.4.0 for security and dependency compatibility
- Modify Bandit security scan to use custom configuration
- Simplify security workflow by removing pip-audit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant