From 4f5b16516729bcfe6536d94544d57472f7435f44 Mon Sep 17 00:00:00 2001 From: Kirill Gontar Date: Thu, 30 Jul 2026 23:24:09 +0200 Subject: [PATCH] fixes --- .../sdk/fleetmdm/FleetMdmClient.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/sdk/fleetmdm/src/main/java/com/openframe/sdk/fleetmdm/FleetMdmClient.java b/sdk/fleetmdm/src/main/java/com/openframe/sdk/fleetmdm/FleetMdmClient.java index 30dc6a154..b12ac20b5 100644 --- a/sdk/fleetmdm/src/main/java/com/openframe/sdk/fleetmdm/FleetMdmClient.java +++ b/sdk/fleetmdm/src/main/java/com/openframe/sdk/fleetmdm/FleetMdmClient.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.CollectionType; import com.openframe.sdk.fleetmdm.exception.FleetMdmApiException; import com.openframe.sdk.fleetmdm.exception.FleetMdmException; import com.openframe.sdk.fleetmdm.model.Host; @@ -397,9 +398,8 @@ public List listPolicies() { try { HttpResponse response = sendRequest(POLICIES_URL, "GET", null); checkResponse(response, "list Fleet policies"); - return MAPPER.convertValue( - requireNode(response.body(), "policies"), - MAPPER.getTypeFactory().constructCollectionType(List.class, Policy.class)); + String body = response.body(); + return parseList(body, "policies", Policy.class); } catch (FleetMdmApiException e) { throw e; } catch (Exception e) { @@ -459,9 +459,8 @@ public List listScheduledQueries() { try { HttpResponse response = sendRequest(QUERIES_URL, "GET", null); checkResponse(response, "list Fleet scheduled queries"); - return MAPPER.convertValue( - requireNode(response.body(), "queries"), - MAPPER.getTypeFactory().constructCollectionType(List.class, Query.class)); + String body = response.body(); + return parseList(body, "queries", Query.class); } catch (FleetMdmApiException e) { throw e; } catch (Exception e) { @@ -524,9 +523,8 @@ public CompletableFuture> listPoliciesAsync() { .thenApply(response -> { checkResponse(response, "list Fleet policies"); try { - return MAPPER.convertValue( - requireNode(response.body(), "policies"), - MAPPER.getTypeFactory().constructCollectionType(List.class, Policy.class)); + String body = response.body(); + return parseList(body, "policies", Policy.class); } catch (Exception e) { throw new FleetMdmException("Failed to parse list policies response", e); } @@ -590,9 +588,8 @@ public CompletableFuture> listScheduledQueriesAsync() { .thenApply(response -> { checkResponse(response, "list Fleet scheduled queries"); try { - return MAPPER.convertValue( - requireNode(response.body(), "queries"), - MAPPER.getTypeFactory().constructCollectionType(List.class, Query.class)); + String body = response.body(); + return parseList(body, "queries", Query.class); } catch (Exception e) { throw new FleetMdmException("Failed to parse list scheduled queries response", e); } @@ -800,6 +797,16 @@ private static void checkResponse(HttpResponse response, String action) + (body.isEmpty() ? "" : ": " + body), response.statusCode(), body); } + private static List parseList(String responseBody, String fieldName, Class itemType) throws Exception { + JsonNode root = MAPPER.readTree(responseBody); + JsonNode node = root.get(fieldName); + if (node == null || node.isNull()) { + return List.of(); + } + CollectionType listType = MAPPER.getTypeFactory().constructCollectionType(List.class, itemType); + return MAPPER.convertValue(node, listType); + } + private static JsonNode requireNode(String responseBody, String fieldName) throws Exception { JsonNode root = MAPPER.readTree(responseBody); JsonNode node = root.get(fieldName);