Open robertofalk opened 1 year ago
I can't dig into the flask-restx
code on this right now to investigate the source of the bug, but this is definitely related to the nested model not being registered automatically when not using the docs. If you tell it to include all models using the global config, it works as expected.
from flask import Flask
from flask_restx import Api, Resource, Namespace
from flask_restx.fields import String, Nested
app = Flask(__name__)
api = Api(app)
# Tell RESTX to include all models.
app.config["RESTX_INCLUDE_ALL_MODELS"] = True
ns = Namespace('app', path='/', validate=True)
todo_nested_model = ns.model("nested model", {
'field_c': String(example="<field c>")
})
todo_model_request = ns.model('tenantEventRequest', {
'field_a': String(required=True, example="<field a>"),
'field_b': Nested(todo_nested_model)
})
@ns.route('/todo', doc=False)
class HiddenTodo(Resource):
@ns.expect(todo_model_request, validate=True)
def post(self):
return "All good", 200
if __name__ == '__main__':
api.add_namespace(ns)
app.run()
curl --request POST \
--url http://localhost:5000/todo \
--header 'content-type: application/json' \
--header 'user-agent: vscode-restclient' \
--data '{"field_a": "a","field_b": {"field_c": "c"}}'
"All good"
I confirm that using app.config["RESTX_INCLUDE_ALL_MODELS"] = True
solves the problem.
Code
Please note the
doc=False
, in the route definition.Repro Steps
field_a
attribute.Expected Behavior
The 3rd request should succeed (200 - "All good"), because there is nothing wrong with the json.
Actual Behavior
The json validation fails and the error is not treated.
Error Messages/Stack Trace
Environment
Additional Context
One more interesting behaviour, if I define a second route, not hidden this time, and use the same ID for the namespace model ("nested model" in the example above), the payload validation will take this model during the validation, even if the hidden endpoint is called, and the request will succeed:
Now the request simply works: