Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"only" option not used in Flask-Rebar #240

Open
ghuser2021 opened this issue Jun 18, 2021 · 1 comment
Open

"only" option not used in Flask-Rebar #240

ghuser2021 opened this issue Jun 18, 2021 · 1 comment

Comments

@ghuser2021
Copy link

ghuser2021 commented Jun 18, 2021

Seems like Flask-Rebar is not checking for "only" option for fields in a model. May be it could check for the use of "only" and when encountered, warn or error that it is currently not supported.

@jab suggested I create this issue.

Following is an example:
@registry.handles(
rule="get_upload_status/<doc_id>",
method="GET",
response_body_schema={
200: Upload(
only=[
"upload_status",
"updated_time",
]
)
},
tags=["General"],
)

@jab
Copy link
Collaborator

jab commented Jun 18, 2021

Just to clarify what the problem is, when only is used in a Marshmallow schema for a particular endpoint, Flask-Rebar omits all the other fields in the corresponding model definition in the generated OpenAPI spec, even if other endpoints use the same Marshmallow schema without only or with a different set of only fields.

Here is a minimal, reproducible example:

# app.py
from marshmallow import Schema, fields
from flask_rebar import Rebar
from flask import Flask


class FooSchema(Schema):
    bar = fields.Int()
    baz = fields.Int()


rebar = Rebar()
registry = rebar.create_handler_registry(prefix="/api")


foo = {"bar": 1, "baz": 2}


@registry.handles(
    rule="/foo",
    response_body_schema=FooSchema(),
)
def foo():
    return foo


@registry.handles(
    rule="/foo/baz",
    response_body_schema=FooSchema(only=["baz"])
)
def only_baz():
    return foo


app = Flask("repro")
rebar.init_app(app)
$ curl localhost:5000/api/swagger
...
  "definitions": {
    "FooSchema": {
      "properties": {
        "baz": {
          "type": "integer"
        }
      },
...

Note that the bar field is missing for FooSchema in the generated OpenAPI spec.

Also note that this is exhibiting "last one wins" behavior. If you swap the order in which the foo() and the only_baz() endpoints are defined in app.py, you'll get a different result generated for FooSchema in the OpenAPI spec.

@ghuser2021 said he could work around the issue by not using only.

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

No branches or pull requests

2 participants