Skip to content

Issue 177 #542

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

TraivsBrookes96
Copy link

Updates to allow files lists, using the action="append" argument in the parser. Adds unit tests to confirm that this works, not only whilst the file list is present, but when other arguments are alongside the file list in a single form.

… accounts for multiple arguments as part of the form and sets the correct "consumes" values so that "multipart/form-data" gets submitted, as with a single file, when a list of files is present.
Ensuring formatting matches that of previous updates to this document
Copy link

@spartan124 spartan124 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can personally attest that this works, I was having issues uploading multi-part files and stumbled across this. had to add the line of code before the issues with swagger stopped. I think this PR should be approved to save time.

jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 10, 2025
This is a terrible, horrible, no good, very bad patch that I wish I didn't have to do. However, its
the only way to get the Swagger behavior we need. It applies the patch in this PR
python-restx/flask-restx#542 on the flask-restx repo, however it seems
unlikely it will ever be accepted.
jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 14, 2025
This is a terrible, horrible, no good, very bad patch that I wish I didn't have to do. However, its
the only way to get the Swagger behavior we need. It applies the patch in this PR
python-restx/flask-restx#542 on the flask-restx repo, however it seems
unlikely it will ever be accepted.
jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 14, 2025
This is a terrible, horrible, no good, very bad patch that I wish I didn't have to do. However, its
the only way to get the Swagger behavior we need. It applies the patch in this PR
python-restx/flask-restx#542 on the flask-restx repo, however it seems
unlikely it will ever be accepted.
jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 14, 2025
This is a terrible, horrible, no good, very bad patch that I wish I didn't have to do. However, its
the only way to get the Swagger behavior we need. It applies the patch in this PR
python-restx/flask-restx#542 on the flask-restx repo, however it seems
unlikely it will ever be accepted.

Checking for the post-patch hash is necessary when running pytest, as the unit tests run in the same
process, meaning the imports remain in memory, but many of the fixtures are re-run for each test.
This includes the fixture that sets up the main Flask application, so the monkey patch function was
being re-executed more than once, and the hash check was failing. The fix now checks if the code
matches the patched hash first, and if it does assumes the patch has already been applied and exits
the monkey patching function early.

Add logging messages documenting the success or failure of applying the patch.
jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 14, 2025
This is a terrible, horrible, no good, very bad patch that I wish I didn't have to do. However, its
the only way to get the Swagger behavior we need. It applies the patch in this PR
python-restx/flask-restx#542 on the flask-restx repo, however it seems
unlikely it will ever be accepted.

Checking for the post-patch hash is necessary when running pytest, as the unit tests run in the same
process, meaning the imports remain in memory, but many of the fixtures are re-run for each test.
This includes the fixture that sets up the main Flask application, so the monkey patch function was
being re-executed more than once, and the hash check was failing. The fix now checks if the code
matches the patched hash first, and if it does assumes the patch has already been applied and exits
the monkey patching function early.

Add logging messages documenting the success or failure of applying the patch.
jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 14, 2025
This is a terrible, horrible, no good, very bad patch that I wish I didn't have to do. However, its
the only way to get the Swagger behavior we need. It applies the patch in this PR
python-restx/flask-restx#542 on the flask-restx repo, however it seems
unlikely it will ever be accepted.

Checking for the post-patch hash is necessary when running pytest, as the unit tests run in the same
process, meaning the imports remain in memory, but many of the fixtures are re-run for each test.
This includes the fixture that sets up the main Flask application, so the monkey patch function was
being re-executed more than once, and the hash check was failing. The fix now checks if the code
matches the patched hash first, and if it does assumes the patch has already been applied and exits
the monkey patching function early.

Add logging messages documenting the success or failure of applying the patch.
jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 14, 2025
This is a terrible, horrible, no good, very bad patch that I wish I didn't have to do. However, its
the only way to get the Swagger behavior we need. It applies the patch in this PR
python-restx/flask-restx#542 on the flask-restx repo, however it seems
unlikely it will ever be accepted.

Checking for the post-patch hash is necessary when running pytest, as the unit tests run in the same
process, meaning the imports remain in memory, but many of the fixtures are re-run for each test.
This includes the fixture that sets up the main Flask application, so the monkey patch function was
being re-executed more than once, and the hash check was failing. The fix now checks if the code
matches the patched hash first, and if it does assumes the patch has already been applied and exits
the monkey patching function early.

Add logging messages documenting the success or failure of applying the patch.
jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 14, 2025
This is a terrible, horrible, no good, very bad patch that I wish I didn't have to do. However, its
the only way to get the Swagger behavior we need. It applies the patch in this PR
python-restx/flask-restx#542 on the flask-restx repo, however it seems
unlikely it will ever be accepted.

Checking for the post-patch hash is necessary when running pytest, as the unit tests run in the same
process, meaning the imports remain in memory, but many of the fixtures are re-run for each test.
This includes the fixture that sets up the main Flask application, so the monkey patch function was
being re-executed more than once, and the hash check was failing. The fix now checks if the code
matches the patched hash first, and if it does assumes the patch has already been applied and exits
the monkey patching function early.

Add logging messages documenting the success or failure of applying the patch.
jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 15, 2025
This is a terrible, horrible, no good, very bad patch that I wish I didn't have to do. However, its
the only way to get the Swagger behavior we need. It applies the patch in this PR
python-restx/flask-restx#542 on the flask-restx repo, however it seems
unlikely it will ever be accepted.

Checking for the post-patch hash is necessary when running pytest, as the unit tests run in the same
process, meaning the imports remain in memory, but many of the fixtures are re-run for each test.
This includes the fixture that sets up the main Flask application, so the monkey patch function was
being re-executed more than once, and the hash check was failing. The fix now checks if the code
matches the patched hash first, and if it does assumes the patch has already been applied and exits
the monkey patching function early.

Add logging messages documenting the success or failure of applying the patch.
jkglasbrenner added a commit to usnistgov/dioptra that referenced this pull request Jan 15, 2025
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 added a commit to usnistgov/dioptra that referenced this pull request Jan 16, 2025
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.
JustKuzya pushed a commit to usnistgov/dioptra that referenced this pull request Jan 29, 2025
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.
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

Successfully merging this pull request may close these issues.

2 participants