Skip to content

[Detail Bug] SearchInput does not URL-encode location parameters, causing invalid URIs #55

@detail-app

Description

@detail-app

Detail Bug Report

https://app.detail.dev/org_befd6425-a158-4e24-9d4d-1e5c08769515/bugs/bug_69e92089-008e-489d-90c6-a8a26beaf920

Summary

  • Context: SearchInput generates the query string for Apple Maps Search API requests, including location-based hints like searchLocation, searchRegion, and userLocation.
  • Bug: Unlike other input classes (e.g., EtaInput), SearchInput fails to URL-encode the values of its location-based parameters in toQueryString().
  • Actual vs. expected: Currently, coordinate strings are added raw; they should be URL-encoded to ensure the resulting string is a valid URI, consistent with how q, language, and pageToken are handled.
  • Impact: If a location string contains a space or other special character (allowed by the SearchLocation constructor), toQueryString() produces an invalid query string that causes URI.create() to throw an IllegalArgumentException, crashing the request.

Code with bug

searchLocation.ifPresent(value -> parameters.add(formatParameter(PARAMETER_SEARCH_LOCATION, value.toQueryString()))); // <-- BUG 🔴 Missing encode()
searchRegion.ifPresent(value -> parameters.add(formatParameter(PARAMETER_SEARCH_REGION, value.toQueryString())));
userLocation.ifPresent(value -> parameters.add(formatParameter(PARAMETER_USER_LOCATION, value.toQueryString())));

Failing test

@Test
void testToQueryStringWithSpaceInLocation() {
    SearchInput input = SearchInput.builder("cafe")
        .searchLocation(new SearchLocation("37.33, -122.03"))
        .build();

    String queryString = input.toQueryString();
    // queryString contains "searchLocation=37.33, -122.03"

    assertThrows(IllegalArgumentException.class, () -> {
        URI.create("https://maps-api.apple.com/v1/search" + queryString);
    });
}

Output: IllegalArgumentException: Illegal character in query at index ... (due to unencoded space in the query string)

Recommended fix

searchLocation.ifPresent(value -> parameters.add(formatParameter(PARAMETER_SEARCH_LOCATION, encode(value.toQueryString())))); // <-- FIX 🟢
searchRegion.ifPresent(value -> parameters.add(formatParameter(PARAMETER_SEARCH_REGION, encode(value.toQueryString()))));
userLocation.ifPresent(value -> parameters.add(formatParameter(PARAMETER_USER_LOCATION, encode(value.toQueryString()))));

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions