Skip to content

Add requestBodies scope support for OpenAPI#2716

Merged
koxudaxi merged 3 commits intomainfrom
feature/add-requestbodies-scope
Dec 21, 2025
Merged

Add requestBodies scope support for OpenAPI#2716
koxudaxi merged 3 commits intomainfrom
feature/add-requestbodies-scope

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 20, 2025

Fixes: #1749

Summary by CodeRabbit

  • New Features
    • Added support for generating code models from OpenAPI request body components. When the RequestBodies scope is enabled, request bodies in your OpenAPI specification are now processed to generate corresponding data models, including support for referenced bodies and multiple content types.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 20, 2025

Walkthrough

This PR adds support for generating Pydantic models from OpenAPI request body components. A new RequestBodies scope is introduced to the OpenAPIScope enum, and the OpenAPI parser is extended to process components/requestBodies, resolving references and extracting schemas from content media types for model generation.

Changes

Cohort / File(s) Change Summary
Core feature: Enum and parser
src/datamodel_code_generator/__init__.py, src/datamodel_code_generator/parser/openapi.py
Added RequestBodies = "requestbodies" enum member to OpenAPIScope. Implemented parser logic to iterate requestBodies, resolve $ref, extract schemas from content media types, and recursively parse them.
Test input specifications
tests/data/openapi/request_bodies_scope.yaml, tests/data/openapi/request_bodies_scope_empty.yaml, tests/data/openapi/request_bodies_scope_with_ref.yaml
Added three OpenAPI 3.0.0 specification files defining requestBodies components: one with inline schemas (CreatePet, UpdatePet), one empty, and one with $ref references (BasePet, CreatePet).
Expected test outputs
tests/data/expected/main/openapi/request_bodies_scope.py, tests/data/expected/main/openapi/request_bodies_scope_with_ref.py
Added two Pydantic model files with expected generated output: CreatePet, PetUpdate, UpdatePet models with optional fields and RootModel usage.
Test cases
tests/main/openapi/test_main_openapi.py, tests/parser/test_openapi.py
Added two end-to-end tests in test_main_openapi.py and five parser tests in test_openapi.py covering requestBodies scope handling, reference resolution, empty requestBodies, missing schemas, and requestBody-level $ref.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

  • Parser logic extends existing patterns (similar to schemas/webhooks handling) with straightforward iteration and schema extraction
  • Enum addition is minimal
  • Test additions are homogeneous and follow established conventions

Suggested labels

safe-to-fix

Suggested reviewers

  • ilovelinux

Poem

🐰 A new scope hops into view—request bodies now pass through!
The parser munches schemas, resolves each ref with care,
From components they spring forth, Pydantic models fair! ✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding support for requestBodies scope in OpenAPI code generation, which directly addresses the linked issue #1749.
Linked Issues check ✅ Passed The PR successfully implements the objective from issue #1749 by adding the RequestBodies enum member to OpenAPIScope, enabling the parser to handle OpenAPI requestBodies components and generate models accordingly.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing requestBodies scope support: enum addition, parser logic, test fixtures, and test cases. No unrelated or out-of-scope modifications are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/add-requestbodies-scope

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 20, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.34%. Comparing base (f4b074f) to head (a2e7df0).
⚠️ Report is 1 commits behind head on main.

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           
Flag Coverage Δ
unittests 99.34% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 20, 2025

CodSpeed Performance Report

Merging #2716 will not alter performance

Comparing feature/add-requestbodies-scope (a2e7df0) with main (f4b074f)

Summary

✅ 59 untouched
⏩ 10 skipped1

Footnotes

  1. 10 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@koxudaxi
Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 20, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 $ref at 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 requestbodies with other scopes (e.g., schemas and requestbodies together) to ensure the new scope works correctly alongside existing scopes. This would follow the pattern of test_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

📥 Commits

Reviewing files that changed from the base of the PR and between f4b074f and a2e7df0.

📒 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 $ref at 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 $ref resolution (combined with Schemas scope)
  • Empty requestBodies handling
  • Content without schema is properly skipped
  • RequestBody-level $ref resolution

The 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_time ensures deterministic output, and the test parameters are appropriate.

@koxudaxi koxudaxi merged commit 9f7e40d into main Dec 21, 2025
38 checks passed
@koxudaxi koxudaxi deleted the feature/add-requestbodies-scope branch December 21, 2025 01:20
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.

generate request body

1 participant