Skip to content

Add requestBody component and reference resolution - #1086

Open
Fire162 wants to merge 1 commit into
marshmallow-code:devfrom
Fire162:add-request-body-component
Open

Fire162 wants to merge 1 commit into
marshmallow-code:devfrom
Fire162:add-request-body-component

Conversation

@Fire162

@Fire162 Fire162 commented Oct 14, 2026

Copy link
Copy Markdown

Resolves #886

Description

In OpenAPI 3+, request bodies can be defined as reusable components under #/components/requestBodies. This pull request adds full support for registering and resolving requestBody components in apispec:

  • Added request_body to COMPONENT_SUBSECTIONS[3] as requestBodies.
  • Added request_body component registry and lazy registration buffer to Components.
  • Implemented Components.request_body(component_id, component, lazy=False, **kwargs).
  • Updated Components._resolve_refs_in_operation to resolve requestBody references specified by component ID name to {"$ref": "#/components/requestBodies/<name>"} and safely inspect request_body.get("content", {}).
  • Added request_body_helper to BasePlugin and MarshmallowPlugin so Marshmallow schemas can be passed directly inside request body definitions.
  • Added comprehensive unit tests for request_body registration, chainability, duplicate naming, reference resolution, lazy loading, and Marshmallow plugin integration.

@lafrech lafrech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, thanks.

Just one minor comment.

Comment thread src/apispec/core.py
COMPONENT_SUBSECTIONS[self.openapi_version.major][k]: v
for k, v in self._subsections.items()
if v != {}
if v != {} and k in COMPONENT_SUBSECTIONS[self.openapi_version.major]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don' t think this is needed.

A KeyError is raised if user registers a request body with OpenAPI v2 since it is not supported (and therefore not in COMPONENT_SUBSECTIONS[self.openapi_version.major]). I belive this is fine. At least, this is what we've been doing with other components up till now.

Tests pass when removing this change.

This branch has not been deployed

No deployments
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.

Add requestBody component

2 participants