Add requestBodies scope support for OpenAPI#2716
Conversation
WalkthroughThis PR adds support for generating Pydantic models from OpenAPI request body components. A new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2716 +/- ##
=======================================
Coverage 99.34% 99.34%
=======================================
Files 82 82
Lines 11615 11656 +41
Branches 1406 1410 +4
=======================================
+ Hits 11539 11580 +41
Misses 45 45
Partials 31 31
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
CodSpeed Performance ReportMerging #2716 will not alter performanceComparing Summary
Footnotes
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/datamodel_code_generator/parser/openapi.py (1)
822-843: LGTM! Implementation follows established patterns.The requestBodies processing correctly:
- Resolves
$refat the requestBody level- Iterates through content media types
- Extracts and parses schemas via
parse_raw_obj- Constructs detailed paths for debugging
The approach mirrors the Schemas scope implementation with appropriate additional logic for the nested structure of requestBodies (content → media type → schema).
Optional consideration: If a single requestBody defines multiple media types with substantially different schemas (uncommon but possible), all would generate models with the same
body_name, potentially causing conflicts. While rare in practice (most APIs use the same schema across media types), you may want to verify this scenario in the future or document the expected behavior.tests/main/openapi/test_main_openapi.py (1)
4309-4319: LGTM! Good coverage of $ref handling in requestBodies.The test validates that $ref references within requestBodies are correctly resolved. This is an important edge case and complements the basic test well.
Consider adding a test that combines
requestbodieswith other scopes (e.g.,schemasandrequestbodiestogether) to ensure the new scope works correctly alongside existing scopes. This would follow the pattern oftest_main_openapi_body_and_parameters(line 1604) which tests multiple scopes together.Example test structure
def test_main_openapi_request_bodies_with_schemas_scope(output_file: Path) -> None: """Test requestbodies scope combined with schemas scope.""" run_main_and_assert( input_path=OPEN_API_DATA_PATH / "request_bodies_with_schemas.yaml", output_path=output_file, input_file_type="openapi", assert_func=assert_file_content, expected_file="request_bodies_with_schemas.py", extra_args=["--openapi-scopes", "schemas", "requestbodies", "--output-model-type", "pydantic_v2.BaseModel"], )
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
src/datamodel_code_generator/__init__.py(1 hunks)src/datamodel_code_generator/parser/openapi.py(1 hunks)tests/data/expected/main/openapi/request_bodies_scope.py(1 hunks)tests/data/expected/main/openapi/request_bodies_scope_with_ref.py(1 hunks)tests/data/openapi/request_bodies_scope.yaml(1 hunks)tests/data/openapi/request_bodies_scope_empty.yaml(1 hunks)tests/data/openapi/request_bodies_scope_with_ref.yaml(1 hunks)tests/main/openapi/test_main_openapi.py(1 hunks)tests/parser/test_openapi.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
tests/data/expected/main/openapi/request_bodies_scope.py (2)
tests/data/expected/main/openapi/request_bodies_scope_with_ref.py (1)
CreatePet(10-11)src/datamodel_code_generator/model/base.py (1)
name(615-617)
tests/data/expected/main/openapi/request_bodies_scope_with_ref.py (2)
tests/data/expected/main/openapi/request_bodies_scope.py (1)
CreatePet(10-12)src/datamodel_code_generator/model/base.py (1)
name(615-617)
src/datamodel_code_generator/parser/openapi.py (4)
src/datamodel_code_generator/__init__.py (1)
OpenAPIScope(238-246)src/datamodel_code_generator/__main__.py (1)
get(112-114)src/datamodel_code_generator/reference.py (1)
get(831-833)src/datamodel_code_generator/parser/jsonschema.py (1)
parse_raw_obj(3030-3040)
tests/parser/test_openapi.py (2)
src/datamodel_code_generator/parser/openapi.py (1)
OpenAPIParser(175-867)src/datamodel_code_generator/__init__.py (1)
OpenAPIScope(238-246)
🪛 Checkov (3.2.334)
tests/data/openapi/request_bodies_scope.yaml
[high] 1-37: Ensure that the global security field has rules defined
(CKV_OPENAPI_4)
tests/data/openapi/request_bodies_scope_empty.yaml
[high] 1-15: Ensure that the global security field has rules defined
(CKV_OPENAPI_4)
tests/data/openapi/request_bodies_scope_with_ref.yaml
[high] 1-21: Ensure that the global security field has rules defined
(CKV_OPENAPI_4)
🔇 Additional comments (8)
tests/data/expected/main/openapi/request_bodies_scope.py (1)
1-20: LGTM! Generated models follow established patterns.The expected output correctly demonstrates the generated Pydantic models from requestBodies components, including a RootModel for schema references.
tests/data/openapi/request_bodies_scope.yaml (1)
1-35: LGTM! Test specification covers key scenarios.This OpenAPI spec appropriately tests requestBodies generation with inline schemas, schema references, and edge cases (empty content).
The static analysis security warning is a false positive—this is test data, not a production API specification.
tests/data/openapi/request_bodies_scope_with_ref.yaml (1)
1-19: LGTM! Validates $ref handling at requestBody level.This spec correctly tests reference resolution when one requestBody references another, ensuring the parser handles
$refat the requestBody level (not just at the schema level).The static analysis security warning is a false positive for test data.
tests/data/openapi/request_bodies_scope_empty.yaml (1)
1-13: LGTM! Tests empty requestBodies edge case.This spec validates the parser handles empty
requestBodies: {}gracefully without errors.src/datamodel_code_generator/__init__.py (1)
246-246: LGTM! Enum addition follows conventions.The new
RequestBodies = "requestbodies"member correctly extends OpenAPIScope with lowercase value matching the existing pattern (schemas, paths, webhooks, etc.).tests/data/expected/main/openapi/request_bodies_scope_with_ref.py (1)
1-15: LGTM! Expected output validates $ref resolution.The generated models correctly represent both CreatePet and BasePet, demonstrating that the parser properly resolves requestBody-level references.
tests/parser/test_openapi.py (1)
980-1036: LGTM! Comprehensive test coverage.The new test suite thoroughly validates the requestBodies scope functionality:
- Basic model generation from requestBodies
- Schema-level
$refresolution (combined with Schemas scope)- Empty requestBodies handling
- Content without schema is properly skipped
- RequestBody-level
$refresolutionThe tests cover both positive cases and important edge cases, ensuring robust behavior.
tests/main/openapi/test_main_openapi.py (1)
4296-4307: LGTM! Well-structured test for requestBodies scope.The test correctly validates the new requestBodies scope functionality and follows established patterns in the codebase. The use of
freeze_timeensures deterministic output, and the test parameters are appropriate.
Fixes: #1749
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.