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

Add description and enum values for body parameter #417

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flask_restx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def __init__(
self.url_scheme = url_scheme
if app is not None:
self.app = app
self.init_app(app)
self.init_app(app, **kwargs)
# super(Api, self).__init__(app, **kwargs)

def init_app(self, app, **kwargs):
Expand Down
26 changes: 22 additions & 4 deletions flask_restx/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,43 @@ def build_request_body_parameters_schema(body_params):
'type': 'object',
'properties': [
'parameter1': {
'type': 'integer'
'type': 'integer',
'description': 'Some description',
'enum': [0, 1]
},
'parameter2': {
'type': 'string'
'type': 'string',
'description': 'Some description',
'enum': ['a', 'b', 'c']
}
]
}
}
"""

properties = {}
required_params = []
for param in body_params:
properties[param["name"]] = {"type": param.get("type", "string")}
properties[param["name"]] = {
"type": param.get("type", "string"),
"description": param.get("description"),
"enum": param.get("enum")
}

if param.get("required"):
required_params.append(param["name"])

schema = {
"type": "object",
"required": required_params if required_params else None,
"properties": properties,
}

return {
"name": "payload",
"required": True,
"in": "body",
"schema": {"type": "object", "properties": properties},
"schema": schema,
}


Expand Down
8 changes: 6 additions & 2 deletions tests/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3265,7 +3265,7 @@ def post(self):
def test_build_request_body_parameters_schema(self):
parser = restx.reqparse.RequestParser()
parser.add_argument("test", type=int, location="headers")
parser.add_argument("test1", type=int, location="json")
parser.add_argument("test1", type=int, help="Test 1 parameter", choices=[0, 1], location="json")
parser.add_argument("test2", location="json")

body_params = [p for p in parser.__schema__ if p["in"] == "body"]
Expand All @@ -3276,8 +3276,12 @@ def test_build_request_body_parameters_schema(self):
assert result["in"] == "body"
assert result["schema"]["type"] == "object"
assert result["schema"]["properties"]["test1"]["type"] == "integer"
assert result["schema"]["properties"]["test1"]["description"] == "Test 1 parameter"
assert result["schema"]["properties"]["test1"]["enum"] == [0, 1]
assert result["schema"]["properties"]["test2"]["type"] == "string"

assert result["schema"]["properties"]["test2"]["description"] is None
assert result["schema"]["properties"]["test2"]["enum"] is None

def test_expect_unused_model(self, app, api, client):
from flask_restx import fields

Expand Down