-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Detail Bug Report
Summary
- Context:
SearchResponsePlace.javais a record that represents a place returned by the Apple Maps Search API. - Bug: The
countryandcountryCodefields are defined as mandatoryStringcomponents and checked withObjects.requireNonNullin the constructor. - Actual vs. expected: The constructor throws a
NullPointerExceptionifcountryorcountryCodeis missing from the API response, whereas these fields should be optional to accommodate places in international waters or disputed territories. - Impact: Any search or reverse geocode request that returns a place without country information will cause the entire operation to fail with a
NullPointerExceptionduring JSON deserialization.
Code with bug
00010| public record SearchResponsePlace(
...
00018| String country,
00019| String countryCode,
...
00036| public SearchResponsePlace {
...
00045| country = Objects.requireNonNull(country, "country"); // <-- BUG 🔴 [Mandatory check for potentially missing API field]
00046| countryCode = Objects.requireNonNull(countryCode, "countryCode"); // <-- BUG 🔴 [Mandatory check for potentially missing API field]
00047| }Failing test
@Test
void deserializationFailsWhenCountryIsMissing() {
String json = """
{
"name": "Test Place",
"coordinate": {
"latitude": 37.3349,
"longitude": -122.0091
},
"countryCode": "US"
}
""";
// This fails with NullPointerException because the record constructor requires 'country'
objectMapper.readValue(json, SearchResponsePlace.class);
}Output: com.fasterxml.jackson.databind.exc.ValueInstantiationException (caused by java.lang.NullPointerException: country)
Recommended fix
Change country and countryCode to Optional<String> and normalize in the constructor.
public record SearchResponsePlace(
...
Optional<String> country, // <-- FIX 🟢
Optional<String> countryCode, // <-- FIX 🟢
...
) {
public SearchResponsePlace {
...
country = normalizeOptional(country);
countryCode = normalizeOptional(countryCode);
...
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working