From d67612a85521df86ff4fd70ada4ae908a6b54bfc Mon Sep 17 00:00:00 2001 From: tallison Date: Mon, 26 Oct 2026 16:24:40 -0400 Subject: [PATCH] swap jackson for gson in the GeoGazetteerClient --- .../tika-parser-nlp-module/pom.xml | 4 +- .../topic/gazetteer/GeoGazetteerClient.java | 22 +++++--- .../gazetteer/GeoGazetteerClientTest.java | 52 +++++++++++++++++++ 3 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/test/java/org/apache/tika/parser/geo/topic/gazetteer/GeoGazetteerClientTest.java diff --git a/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml index ece9707f6ec..4fc9abe0541 100644 --- a/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml +++ b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml @@ -98,8 +98,8 @@ commons-codec - com.google.code.gson - gson + com.fasterxml.jackson.core + jackson-databind org.slf4j diff --git a/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/main/java/org/apache/tika/parser/geo/topic/gazetteer/GeoGazetteerClient.java b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/main/java/org/apache/tika/parser/geo/topic/gazetteer/GeoGazetteerClient.java index e1965c937f8..b6a98a7eddc 100644 --- a/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/main/java/org/apache/tika/parser/geo/topic/gazetteer/GeoGazetteerClient.java +++ b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/main/java/org/apache/tika/parser/geo/topic/gazetteer/GeoGazetteerClient.java @@ -16,13 +16,14 @@ */ package org.apache.tika.parser.geo.topic.gazetteer; -import java.lang.reflect.Type; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; @@ -42,6 +43,10 @@ public class GeoGazetteerClient { private static final Logger LOG = LoggerFactory.getLogger(GeoGazetteerClient.class); + // gson ignored fields Location doesn't have + private static final ObjectMapper MAPPER = + new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + private String url; /** @@ -74,11 +79,7 @@ public Map> getLocations(List locations) { HttpResponse resp = httpClient.execute(httpGet); String respJson = IOUtils.toString(resp.getEntity().getContent(), StandardCharsets.UTF_8); - @SuppressWarnings("serial") Type typeDef = - new TypeToken>>() { - }.getType(); - - return new Gson().fromJson(respJson, typeDef); + return parseLocations(respJson); } catch (Exception e) { LOG.error(e.getMessage(), e); @@ -87,6 +88,11 @@ public Map> getLocations(List locations) { return null; } + static Map> parseLocations(String json) throws IOException { + return MAPPER.readValue(json, new TypeReference>>() { + }); + } + /** * Ping lucene-geo-gazetteer API * diff --git a/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/test/java/org/apache/tika/parser/geo/topic/gazetteer/GeoGazetteerClientTest.java b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/test/java/org/apache/tika/parser/geo/topic/gazetteer/GeoGazetteerClientTest.java new file mode 100644 index 00000000000..8d763d2bcb5 --- /dev/null +++ b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/test/java/org/apache/tika/parser/geo/topic/gazetteer/GeoGazetteerClientTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tika.parser.geo.topic.gazetteer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +public class GeoGazetteerClientTest { + + @Test + public void testParseLocations() throws Exception { + // lucene-geo-gazetteer sends coordinates as numbers, and fields Location doesn't have + String json = "{\"Pasadena\":[{\"name\":\"Pasadena\",\"countryCode\":\"US\",\"admin1Code\":\"CA\"," + + "\"admin2Code\":\"037\",\"latitude\":34.14778,\"longitude\":-118.14452,\"population\":141029}]," + + "\"Nowhere\":[]}"; + Map> locations = GeoGazetteerClient.parseLocations(json); + assertEquals(2, locations.size()); + assertTrue(locations.get("Nowhere").isEmpty()); + Location loc = locations.get("Pasadena").get(0); + assertEquals("Pasadena", loc.getName()); + assertEquals("US", loc.getCountryCode()); + assertEquals("CA", loc.getAdmin1Code()); + assertEquals("037", loc.getAdmin2Code()); + assertEquals("34.14778", loc.getLatitude()); + assertEquals("-118.14452", loc.getLongitude()); + } + + @Test + public void testNullLocations() throws Exception { + assertNull(GeoGazetteerClient.parseLocations("null")); + } +}