Skip to content

Commit b242b29

Browse files
fix(safety): 안전시설 JSON 페이징 방어 로직 추가 (#72)
1 parent 48bda7e commit b242b29

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

backend/src/main/java/com/ssafy/salmanhae/service/safety/ingest/AbstractJsonSafetyFacilityOpenApiClient.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
abstract class AbstractJsonSafetyFacilityOpenApiClient implements SafetyFacilitySourceClient {
1717

18+
private static final int MAX_PAGES = 1000;
19+
1820
private final Logger log = LoggerFactory.getLogger(getClass());
1921

2022
private final String sourceName;
@@ -49,7 +51,7 @@ List<NormalizedSafetyFacility> fetchPagedJson(String baseUrl, String serviceKey)
4951
List<NormalizedSafetyFacility> facilities = new ArrayList<>();
5052
int pageNo = 1;
5153
int totalCount = -1;
52-
while (totalCount < 0 || (pageNo - 1) * properties.pageSize() < totalCount) {
54+
while (pageNo <= MAX_PAGES && (totalCount < 0 || (pageNo - 1) * properties.pageSize() < totalCount)) {
5355
URI uri = UriComponentsBuilder.fromUriString(baseUrl)
5456
.queryParam("serviceKey", serviceKey)
5557
.queryParam("pageNo", pageNo)
@@ -69,15 +71,22 @@ List<NormalizedSafetyFacility> fetchPagedJson(String baseUrl, String serviceKey)
6971
if (body == null || body.isBlank()) {
7072
break;
7173
}
74+
JsonNode root;
75+
try {
76+
root = objectMapper.readTree(body);
77+
} catch (Exception exception) {
78+
log.warn("Failed to parse {} safety facilities from {}", payloadName, baseUrl, exception);
79+
break;
80+
}
7281
ParsedSafetyFacilityPage page;
7382
try {
74-
page = parsePage(body);
83+
page = parsePage(root);
7584
} catch (IllegalArgumentException exception) {
7685
log.warn("Failed to parse {} safety facilities from {}", payloadName, baseUrl, exception);
7786
break;
7887
}
7988
if (totalCount < 0) {
80-
totalCount = totalCount(body);
89+
totalCount = SafetyFacilityParserSupport.totalCount(root);
8190
}
8291
if (page.rawItemCount() == 0) {
8392
break;
@@ -88,6 +97,9 @@ List<NormalizedSafetyFacility> fetchPagedJson(String baseUrl, String serviceKey)
8897
}
8998
pageNo++;
9099
}
100+
if (pageNo > MAX_PAGES) {
101+
log.warn("Stopped fetching {} safety facilities after reaching max page limit {}", payloadName, MAX_PAGES);
102+
}
91103
return facilities;
92104
}
93105

@@ -112,13 +124,5 @@ private ParsedSafetyFacilityPage parsePage(JsonNode root) {
112124
return new ParsedSafetyFacilityPage(facilities, items.size());
113125
}
114126

115-
private int totalCount(String json) {
116-
try {
117-
return SafetyFacilityParserSupport.totalCount(objectMapper.readTree(json));
118-
} catch (Exception exception) {
119-
return -1;
120-
}
121-
}
122-
123127
abstract NormalizedSafetyFacility toFacility(JsonNode node);
124128
}

0 commit comments

Comments
 (0)