From d867396d0ca9a44f877607e2feaf270bf8bd7992 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 8 Apr 2026 21:09:35 +0000 Subject: [PATCH] Fix CSV import data loss and restore null response check LocationFileImporter.startImport(): - Every BUFF_SIZE-th record (lines 0, 100, 200, ...) was silently skipped because the flush branch did not process the current line - The final partial buffer was never flushed after the loop exited - Rewrote loop to process every line and flush remaining records IpStackHttp.find(): - Restored null check on response body removed during the RestTemplate -> RestClient migration (8463bc8), preventing NullPointerException when bodyTo() returns null Co-Authored-By: Georgii Glinskiy --- .../geo/importer/LocationFileImporter.java | 34 +++++++++++-------- .../geo/integrations/ipstack/IpStackHttp.java | 3 ++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/main/java/fun/wilddev/geo/importer/LocationFileImporter.java b/src/main/java/fun/wilddev/geo/importer/LocationFileImporter.java index 25d97e9..368e649 100644 --- a/src/main/java/fun/wilddev/geo/importer/LocationFileImporter.java +++ b/src/main/java/fun/wilddev/geo/importer/LocationFileImporter.java @@ -57,32 +57,36 @@ public int startImport() throws FileReaderException { String[] line; List buff = new ArrayList<>(BUFF_SIZE); - for (int i = 0; (line = csv.readNext()) != null; i++) { + while ((line = csv.readNext()) != null) { - if (i % BUFF_SIZE == 0) { + if (line.length < 3) { - locationRepository.saveAll(buff); - buff.clear(); - - log.debug("Flushed"); + log.debug("Bad entry, skipping ..."); + continue; + } - } else { + LocationD location = new LocationD(new Country(line[1], line[0]), line[2]); - if (line.length < 3) { + buff.add(location); + counter++; - log.debug("Bad entry, skipping ..."); - continue; - } + log.info("Location read: {}", location); - LocationD location = new LocationD(new Country(line[1], line[0]), line[2]); + if (buff.size() >= BUFF_SIZE) { - buff.add(location); - counter++; + locationRepository.saveAll(buff); + buff.clear(); - log.info("Location read: {}", location); + log.debug("Flushed"); } } + if (!buff.isEmpty()) { + + locationRepository.saveAll(buff); + log.debug("Flushed remaining"); + } + } catch (Exception ex) { throw new FileReaderException("Failed to read location file", ex); } diff --git a/src/main/java/fun/wilddev/geo/integrations/ipstack/IpStackHttp.java b/src/main/java/fun/wilddev/geo/integrations/ipstack/IpStackHttp.java index 92aea63..9997a26 100644 --- a/src/main/java/fun/wilddev/geo/integrations/ipstack/IpStackHttp.java +++ b/src/main/java/fun/wilddev/geo/integrations/ipstack/IpStackHttp.java @@ -50,6 +50,9 @@ public Location find(@NonNull String ip) throws HttpRequestFailedException { val source = response.bodyTo(IpStackLocationResponse.class); + if (source == null) + throw new HttpRequestFailedException("null response"); + if (source.getSuccess() == Boolean.FALSE) throw new HttpRequestFailedException("Request rejected: " + source);