-
-
Notifications
You must be signed in to change notification settings - Fork 54
fix: handle malformed api_constraints without crashing endpoint (#273) #285
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
Gokul-social
wants to merge
6
commits into
openml:main
Choose a base branch
from
Gokul-social:fix/273-graceful-api-constraints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f08c487
fix(tasktype): handle malformed api_constraints safely without crashi…
Gokul-social ea00485
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 871472f
Merge branch 'openml:main' into fix/273-graceful-api-constraints
Gokul-social 41f336e
fix: restore Any import and resolve lint/type issues
Gokul-social 4fca9fb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 343da1b
Merge branch 'openml:main' into fix/273-graceful-api-constraints
Gokul-social File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import logging | ||
|
|
||
| import pytest | ||
|
|
||
| from routers.openml.tasktype import parse_api_constraints | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("api_constraints", "expected_data_type"), | ||
| [ | ||
| # 1. Valid JSON string with data_type | ||
| ('{"data_type": "matrix"}', "matrix"), | ||
| # 2. Valid dict with data_type | ||
| ({"data_type": "matrix"}, "matrix"), | ||
| # 3. Malformed JSON string → None | ||
| ("{bad json", None), | ||
| # 4. Empty string → None | ||
| ("", None), | ||
| # 5. Valid JSON/dict without data_type → None | ||
| ('{"other_key": "val"}', None), | ||
| ({"other_key": "val"}, None), | ||
| # 6. data_type present but empty string → None | ||
Gokul-social marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ('{"data_type": ""}', None), | ||
| ({"data_type": ""}, None), | ||
| # 7. Non-dict JSON (list) → None | ||
| ('["array"]', None), | ||
| # 8. None → None | ||
| (None, None), | ||
| # 9. Non-dict JSON (int) → None | ||
| ("42", None), | ||
| # 10. data_type is non-string (int) → None | ||
| ('{"data_type": 123}', None), | ||
| ({"data_type": 123}, None), | ||
| ], | ||
| ids=[ | ||
| "valid_json_string", | ||
| "valid_dict", | ||
| "malformed_json", | ||
| "empty_string", | ||
| "json_missing_data_type", | ||
| "dict_missing_data_type", | ||
| "json_empty_data_type", | ||
| "dict_empty_data_type", | ||
| "non_dict_json_list", | ||
| "none_value", | ||
| "non_dict_json_int", | ||
| "json_non_string_data_type", | ||
| "dict_non_string_data_type", | ||
| ], | ||
| ) | ||
| def test_parse_api_constraints( | ||
| api_constraints: object, | ||
| expected_data_type: str | None, | ||
| ) -> None: | ||
| result = parse_api_constraints( | ||
| api_constraints, | ||
| task_type_id=1, | ||
| input_name="source_data", | ||
| ) | ||
| assert result == expected_data_type | ||
|
|
||
|
|
||
| def test_parse_api_constraints_unsupported_type() -> None: | ||
| """Unsupported types (e.g. int, list passed directly) should return None.""" | ||
| result = parse_api_constraints( | ||
| 12345, | ||
| task_type_id=1, | ||
| input_name="source_data", | ||
| ) | ||
| assert result is None | ||
|
|
||
|
|
||
| class TestParseApiConstraintsLogging: | ||
| """Verify correct log levels are emitted for different anomaly types.""" | ||
|
|
||
| def test_malformed_json_logs_warning(self, caplog: pytest.LogCaptureFixture) -> None: | ||
| with caplog.at_level(logging.WARNING): | ||
| parse_api_constraints( | ||
| "{bad json", | ||
| task_type_id=1, | ||
| input_name="source_data", | ||
| ) | ||
| assert any("malformed_json" in r.message for r in caplog.records) | ||
|
|
||
| def test_empty_string_logs_warning(self, caplog: pytest.LogCaptureFixture) -> None: | ||
| with caplog.at_level(logging.WARNING): | ||
| parse_api_constraints( | ||
| "", | ||
| task_type_id=1, | ||
| input_name="source_data", | ||
| ) | ||
| assert any("empty_string" in r.message for r in caplog.records) | ||
|
|
||
| def test_non_dict_json_logs_warning(self, caplog: pytest.LogCaptureFixture) -> None: | ||
| with caplog.at_level(logging.WARNING): | ||
| parse_api_constraints( | ||
| '["array"]', | ||
| task_type_id=1, | ||
| input_name="source_data", | ||
| ) | ||
| assert any("non_dict_json" in r.message for r in caplog.records) | ||
|
|
||
| def test_unsupported_type_logs_warning(self, caplog: pytest.LogCaptureFixture) -> None: | ||
| with caplog.at_level(logging.WARNING): | ||
| parse_api_constraints( | ||
| 12345, | ||
| task_type_id=1, | ||
| input_name="source_data", | ||
| ) | ||
| assert any("unsupported_type" in r.message for r in caplog.records) | ||
|
|
||
| def test_missing_data_type_logs_debug(self, caplog: pytest.LogCaptureFixture) -> None: | ||
| with caplog.at_level(logging.DEBUG): | ||
| parse_api_constraints( | ||
| '{"other_key": "val"}', | ||
| task_type_id=1, | ||
| input_name="source_data", | ||
| ) | ||
| assert any("missing_data_type" in r.message for r in caplog.records) | ||
|
|
||
| def test_valid_constraint_no_warning(self, caplog: pytest.LogCaptureFixture) -> None: | ||
| with caplog.at_level(logging.DEBUG): | ||
| result = parse_api_constraints( | ||
| '{"data_type": "matrix"}', | ||
| task_type_id=1, | ||
| input_name="source_data", | ||
| ) | ||
| assert result == "matrix" | ||
| assert not any(r.name == "routers.openml.tasktype" for r in caplog.records) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.