Fastapi: [QUESTION] response_model_exclude in docs

Created on 17 Jan 2020  路  3Comments  路  Source: tiangolo/fastapi

when using response_model_exclude=[fields] is it possible to exclude those fields from displaying in the docs as well?

question

All 3 comments

As far as I'm aware, there is no automatic way to do this.

You should be able to specify the schema manually through schema_extra.

I also think it should be possible to automate after generating the app's openapi schema (by calling app.openapi() by walking the routes, looking at the response_model_exclude, and comparing that against the schema (removing keys where appropriate).

Given the potential security-oriented nature of this request, I think it would be a good thing to support, so if someone wants to implement this I would be happy to review the pull request. And if it ends up being too complex or otherwise not a great fit for the main fastapi package, I'd be happy to incorporate it as an openapi-related utility into fastapi-utils.

I think the best way to approach it would be to create a model that doesn't have the fields that you want to exclude.

And other models could inherit from it and add the extra fields in some contexts where needed.

response_model_exclude is, more or less, a dirty, quick, trick to solve something quickly and lazily... :man_shrugging: :sweat_smile: ...But if that is a bit more important, I would suggest making it explicit in a model.

Makes sense! )

Was this page helpful?
0 / 5 - 0 ratings