Skip to content

[FIX] Add monkey-patch for flask-restx swagger multipart form support #713

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

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

jkglasbrenner
Copy link
Collaborator

This change introduces a monkey-patch for the flask-restx Swagger.serialize_operation method to force Swagger to use "multipart/form-data" content type for multi-file uploads instead of "application/x-www-form-urlencoded", as required by our application. The patch applies the proposed changes from PR 542 in the flask-restx repository (python-restx/flask-restx#542), which is unlikely to be accepted into the main branch.

The monkey-patch includes the following safety measures:

  • A hash verification system that checks the source code of flask_restx.Swagger.serialize_operation before applying the patch. This prevents potential issues if the underlying library code changes.
  • Early exit logic that detects if the patch has already been applied, preventing double-patching scenarios that were causing issues during pytest execution (where imports remain in memory but fixtures are re-run).
  • Logging to document whether the patch is successfully applied or skipped because it was already present.

The patch is applied during application initialization through a new monkey_patch_flask_restx() function, which is called in the create_app() setup process. While this is not an ideal solution, the safeguards should sufficiently minimize risk in the absence of upstream support for this behavior.

This change introduces a monkey-patch for the flask-restx `Swagger.serialize_operation` method to
force Swagger to use "multipart/form-data" content type for multi-file uploads instead of
"application/x-www-form-urlencoded", as required by our application. The patch applies the proposed
changes from PR #542 in the flask-restx repository
(python-restx/flask-restx#542), which is unlikely to be accepted into the
main branch.

The monkey-patch includes the following safety measures:

- A hash verification system that checks the source code of
  `flask_restx.Swagger.serialize_operation` before applying the patch. This prevents potential
  issues if the underlying library code changes.
- Early exit logic that detects if the patch has already been applied, preventing double-patching
  scenarios that were causing issues during pytest execution (where imports remain in memory but
  fixtures are re-run).
- Logging to document whether the patch is successfully applied or skipped because it was already
  present.

The patch is applied during application initialization through a new `monkey_patch_flask_restx()`
function, which is called in the `create_app()` setup process. While this is not an ideal solution,
the safeguards should sufficiently minimize risk in the absence of upstream support for this
behavior.
@jkglasbrenner jkglasbrenner self-assigned this Jan 15, 2025
@jkglasbrenner jkglasbrenner added the bug Something isn't working label Jan 15, 2025
@jkglasbrenner jkglasbrenner merged commit 2d6bd81 into dev Jan 16, 2025
11 checks passed
@jkglasbrenner jkglasbrenner deleted the flask-restx-monkey-patch branch January 16, 2025 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant