marshmallow-code / marshmallow-code/flask-smorest

How to pass null/None?

Open
#323 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
717
Forks
77
Avg merge
7h 49m
Merged PRs (30d)
3

Description

I'm unsure where responsibility for this lies; flask-smorest, webargs, openapi, etc. But I cannot make a field nullable. i.e. I want to pass a value to the API so that I receive `None` to SQLAlchemy, or pass nothing and have the field not passed to `kwargs` at all.

Here's the trimmed down code
```python

bp = WrappedBlueprint("Project", __name__, url_prefix="/api/projects")
# ma is a flask_marshmallow.Marshmallow object

class ProjectSchema(ma.SQLAlchemyAutoSchema)
class Meta:
model = Project
projectId = ma.auto_field(required=True)
clientId = ma.auto_field(allow_none=True)

@bp.route("/")
class ProjectResource(MethodView):
@bp.response(HTTPStatus.OK, ProjectSchema())
@bp.arguments(ProjectSchema(only=("clientId",), partial=True), location="query", as_kwargs=True)
@bp.arguments(ProjectSchema(only=("projectId",)), location="path", as_kwargs=True)
def patch(self, **kwargs):
# I want kwargs = {"clientId": None} if None was passed
# or not be present at all if nothing was passed
pass
```

If I perform a patch request via python using requests `requests.patch("http://127.0.0.1:7524/api/projects/125", params={"clientId": None})` the `clientId` never makes it into the kwargs passed to the MethodView's patch function.

I'm at a loss as to how to get the functionality I need for something that I thought would be trivial.

This may or may not be useful, but the openapi.json that is generated does indeed specify `nullable: true`
```
...
"patch": {
"parameters":
[
...
{
"in": "query",
"name": "clientId",
"required": false,
"description": "The ID of the client this project is associated with",
"schema":
{
"type": "integer",
"nullable": true
}
}
]
}
```
However the resulting apidocs page refuses to accept `null`, `None`, `"null"`, etc.

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 by reproducing the PATCH request against the MethodView.patch entry point using the shown ProjectSchema and query parameters. Trace how the clientId query value moves through the bp.arguments decorators and compare it with the generated openapi.json and apidocs behavior. Done means distinguishing an explicitly supplied null value from an omitted clientId in kwargs.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, openapi, python, sqlalchemy
Domain
api, backend
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.