aminghadersohi opened a new pull request, #42196:
URL: https://github.com/apache/superset/pull/42196

   ### SUMMARY
   `get_form_data()` crashed with an unhandled `werkzeug.exceptions.BadRequest` 
whenever it ran inside a request context where the `Content-Type` header 
claimed JSON but the body wasn't actually parseable JSON. This happens for any 
caller that reaches `get_form_data()` outside a normal chart-data POST — for 
example the `filter_values()` / `get_filters()` Jinja macros used in chart and 
dataset SQL templates, when rendered from a request context that isn't a real 
chart-data request.
   
   `request.is_json` only inspects the `Content-Type` header; it does not 
guarantee the body is valid, non-empty JSON. `get_form_data()` already has a 
helper, `loads_request_json()`, that gracefully falls back to `{}` on a JSON 
parse failure — but that guard was only applied to the `request.form` / 
`request.args` fallback paths, not to the primary 
`request.get_json(cache=True)` call, which is the one that was actually raising.
   
   This PR extracts the JSON-body parsing into a small 
`get_request_json_body()` helper that catches `BadRequest` and falls back to 
`{}`, matching the existing `loads_request_json()` behavior.
   
   Internal tracking: SC-114072
   
   ### TESTING INSTRUCTIONS
   - Added 
`tests/unit_tests/views/test_utils.py::test_get_form_data_handles_non_json_body_with_json_content_type`,
 which builds a request context with `Content-Type: application/json` and a 
non-JSON body, and asserts `get_form_data()` returns `({}, None)` instead of 
raising.
   - `pytest tests/unit_tests/views/test_utils.py 
tests/unit_tests/jinja_context_test.py -q` — 104 passed.
   - Manually reproduced the crash against `master` 
(`request.get_json(cache=True)` raising `BadRequest`) and confirmed the fix 
resolves it via `app.test_request_context(data="not-json-at-all", 
content_type="application/json")`.
   
   ### ADDITIONAL INFORMATION
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration (follow approval process in 
[SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] Migration is atomic, supports rollback & is backwards-compatible
     - [ ] Confirm DB migration upgrade and downgrade tested
     - [ ] Runtime estimates and downtime expectations provided
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to