Skip to content

Client: URL-encode the query parameters - #2444

Merged
ml-evs merged 3 commits into
Materials-Consortia:mainfrom
anikeya9:fix/client-url-encoding
Oct 25, 2026
Merged

ml-evs merged 3 commits into
Materials-Consortia:mainfrom
anikeya9:fix/client-url-encoding

Conversation

@anikeya9

@anikeya9 anikeya9 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2443.

OptimadeClient._build_url put the query parameter values into the URL as they are, so +, & and # in a filter kept their URL meaning (space, parameter separator, fragment) and the server received a different filter. The spec asks the client to URL-encode the filter parameter ("URL Encoding"; "Filtering": "the contents of the filter parameter is URL-encoded by the client").

Changes

  • _build_url percent-encodes the values of filter, response_fields, sort and other_params with urllib.parse.quote(..., safe=","). Commas are kept, as they separate list values (response_fields=id,elements). The next links from the servers are not changed.
  • New tests in tests/server/test_client.py:
    • test_client_url_encoding: _build_url with chemical_formula_descriptive = "A+B&C#D" and a + response field decodes back (parse_qs) to exactly the same values;
    • test_client_awkward_nested_provider_field (sync and async): filters on _exmpl_stability.gga_gga+u_r2scan < 0 through the client against the test server and expects mpf_3819, the same field and result as the server test test_awkward_nested_provider_field added in Use 1.2 grammar by default and relax rules on nested fields #2242 (which sends %2B by hand). Without the encoding the server answers 400. Marked xfail on Elasticsearch, like the server test, since that backend does not support nested provider fields.

Tests

  • pytest tests/server/test_client.py: 140 passed; both new tests fail without the change.
  • CI: the pre-commit.ci failure is unrelated: its runner image has no python3.10, which default_language_version pins ("failed to find interpreter for Builtin discover of python_spec='python3.10'"); the same hooks pass in the GitHub Actions pre-commit job.
  • pre-commit run on the two files: all hooks pass.
  • Against Materials Project, the nested fields from Unable to create filters with "+" in nested property name #2182 can now be queried with the client:
    OptimadeClient(base_urls=["https://optimade.materialsproject.org"]).count(
        "_mp_stability.gga_gga+u.energy_above_hull < 0.1"
    )
    # before: 400 "Unable to parse filter _mp_stability.gga_gga u.energy_above_hull < 0.1"
    # after:  104259

@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.21%. Comparing base (ecb4b60) to head (c174b48).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2444   +/-   ##
=======================================
  Coverage   90.21%   90.21%           
=======================================
  Files          78       78           
  Lines        5037     5039    +2     
=======================================
+ Hits         4544     4546    +2     
  Misses        493      493           
Flag Coverage Δ
project 90.21% <100.00%> (+<0.01%) ⬆️
validator 90.21% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ml-evs ml-evs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks useful, thanks @anikeya9 !

@ml-evs
ml-evs enabled auto-merge (squash) October 25, 2026 21:45
@ml-evs
ml-evs merged commit 7db767d into Materials-Consortia:main Oct 25, 2026
12 checks passed
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.

Client does not URL-encode the filter parameter

2 participants