marshmallow-code / marshmallow-code/apispec

Add requestBody component

Open
#886 14 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.2k
Forks
202
Avg merge
3h 38m
Merged PRs (30d)
3

Description

Hello there.

I am trying to create a path operation using ApiSpec as follows:
**apiSpec.path(
path = "/my-item",
operations = dict(
post = dict(
tags = ["MyItem"],
operationId = "createMyItem",
description = "Create MyItem",
requestBody ={ "$ref": "#/components/requestBodies/MyItem" } ,
responses = {'200': {'$ref': '#/components/responses/OK'}, '500': {'$ref': '#/components/responses/GeneralError'}}
)
))**

When I try to pass the ResponseBody as ref, as above, I get below error:

**File "c:\workspace\.venv\Lib\site-packages\apispec\core.py", line 549, in path
self.components.resolve_refs_in_path(self._paths[path])
File "c:\workspace\.venv\Lib\site-packages\apispec\core.py", line 410, in resolve_refs_in_path
self._resolve_refs_in_operation(path[method])
File "c:\workspace\.venv\Lib\site-packages\apispec\core.py", line 382, in _resolve_refs_in_operation
self._resolve_refs_in_request_body(operation["requestBody"])
File "c:\workspace\.venv\Lib\site-packages\apispec\core.py", line 352, in _resolve_refs_in_request_body
for media_type in request_body["content"].values():
~~~~~~~~~~~~^^^^^^^^^^^
KeyError: 'content'**

I could pass the whole ResponseBody schema, which is already present in my apiSpec.options['components']['requestBodies']['MyItem'], but this would add duplication for each of my path operations and I would like to avoid this.

Could you advice if it is possible to pass a $ref for RequestBody, which should result in:

_post:
description: Create MyItem
operationId: createMyItem
requestBody:
$ref: '#/components/requestBodies/MyItem'_

In my generated .yaml file?

Also some doc with more real case example would be very handy to work with the ApiSpec library. I checked examples in the [ApiSpec read the docs](https://apispec.readthedocs.io/en/latest/index.html#) but I could not find proper example of how to add / manipulate operation to a path etc.

Thank you in advance :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in apispec/core.py at _resolve_refs_in_operation and _resolve_refs_in_request_body, using the reported KeyError as the entry point. Compare the generated specification with the requested requestBody $ref output, then review the linked documentation examples for operation and path usage. Done means references work without duplication and the relevant usage is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api, documentation
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.