From 45a8c494f8f53509334861ba33829e49e42587b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Thu, 3 Sep 2026 13:40:49 +0200 Subject: [PATCH] :goal_net: better checks for request parameters --- .../clientoptions/BaseProductParameters.java | 33 +++++++++++++++---- .../clientoptions/BaseSearchParameters.java | 16 +++++---- .../com/mindee/v2/http/MindeeHttpApiV2.java | 5 ++- .../search/models/ModelSearchParameters.java | 26 +++++++++------ .../RagDocumentSearchParameters.java | 20 ++++++----- 5 files changed, 68 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/mindee/v2/clientoptions/BaseProductParameters.java b/src/main/java/com/mindee/v2/clientoptions/BaseProductParameters.java index 4adc0e0ce..f411cda7d 100644 --- a/src/main/java/com/mindee/v2/clientoptions/BaseProductParameters.java +++ b/src/main/java/com/mindee/v2/clientoptions/BaseProductParameters.java @@ -1,10 +1,13 @@ package com.mindee.v2.clientoptions; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import lombok.Data; +/** + * Base parameters for sending a file to a Mindee V2 product. + */ @Data public abstract class BaseProductParameters { /** @@ -23,15 +26,35 @@ public abstract class BaseProductParameters { */ protected final String[] webhookIds; + protected BaseProductParameters(String modelId, String alias, String[] webhookIds) { + if (modelId == null || modelId.trim().isBlank()) { + throw new IllegalArgumentException("modelId cannot be null or whitespace."); + } + if ("".equals(alias)) { + throw new IllegalArgumentException("alias cannot be an empty string."); + } + if ( + webhookIds != null && Arrays.stream(webhookIds).anyMatch(id -> id == null || id.isBlank()) + ) { + throw new IllegalArgumentException( + "WebhookIds cannot contain null, empty, or whitespace values." + ); + } + + this.modelId = modelId.trim(); + this.alias = alias; + this.webhookIds = webhookIds != null ? webhookIds : new String[0]; + } + public Map getRequestParameters() { var parameters = new HashMap(); parameters.put("model_id", this.getModelId()); - if (this.getAlias() != null && !this.getAlias().isBlank()) { + if (this.getAlias() != null) { parameters.put("alias", getAlias()); } - if (this.getWebhookIds().length > 0) { + if (this.getWebhookIds() != null && this.getWebhookIds().length > 0) { parameters.put("webhook_ids", String.join(",", this.getWebhookIds())); } @@ -49,8 +72,7 @@ protected T self() { } protected BaseBuilder(String modelId) { - this.modelId = Objects - .requireNonNull(modelId, "The model ID is required in product parameters"); + this.modelId = modelId; } /** Set an alias for the uploaded document. */ @@ -65,5 +87,4 @@ public T webhookIds(String[] webhookIds) { return self(); } } - } diff --git a/src/main/java/com/mindee/v2/clientoptions/BaseSearchParameters.java b/src/main/java/com/mindee/v2/clientoptions/BaseSearchParameters.java index 8d6d4d39f..75020137c 100644 --- a/src/main/java/com/mindee/v2/clientoptions/BaseSearchParameters.java +++ b/src/main/java/com/mindee/v2/clientoptions/BaseSearchParameters.java @@ -22,12 +22,22 @@ public abstract class BaseSearchParameters responseClass, Integer page, Integer perPage ) { this.responseClass = Objects.requireNonNull(responseClass, "responseClass cannot be null"); + if (page != null && page <= 0) { + throw new IllegalArgumentException("page must be greater than 0"); + } + if (perPage != null && perPage <= 0) { + throw new IllegalArgumentException("perPage must be greater than 0"); + } + this.page = page; this.perPage = perPage; } @@ -39,15 +49,9 @@ public Map getRequestParameters() { var parameters = new HashMap(); if (this.getPage() != null) { - if (this.getPage() <= 0) { - throw new IllegalArgumentException("page must be greater than 0"); - } parameters.put("page", String.valueOf(getPage())); } if (this.getPerPage() != null) { - if (this.getPerPage() <= 0) { - throw new IllegalArgumentException("perPage must be greater than 0"); - } parameters.put("per_page", String.valueOf(getPerPage())); } diff --git a/src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java b/src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java index c165a0385..632e2a600 100644 --- a/src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java +++ b/src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java @@ -133,6 +133,9 @@ public TResponse reqGetResultById( Class responseClass, String inferenceId ) { + if (inferenceId == null || inferenceId.trim().isEmpty()) { + throw new IllegalArgumentException("inferenceId cannot be null or empty."); + } var productInfo = getResponseProductAttributes(responseClass); var url = String .format( @@ -150,7 +153,7 @@ public TResponse reqGetResultByUrl( String inferenceUrl ) { if (inferenceUrl == null || inferenceUrl.trim().isEmpty()) { - throw new IllegalArgumentException("inferenceUrl must not be null or blank."); + throw new IllegalArgumentException("inferenceUrl cannot be null or empty."); } validateInferenceUrl(inferenceUrl); var get = new HttpGet(inferenceUrl); diff --git a/src/main/java/com/mindee/v2/search/models/ModelSearchParameters.java b/src/main/java/com/mindee/v2/search/models/ModelSearchParameters.java index 0e4fc0595..05c188bad 100644 --- a/src/main/java/com/mindee/v2/search/models/ModelSearchParameters.java +++ b/src/main/java/com/mindee/v2/search/models/ModelSearchParameters.java @@ -22,8 +22,18 @@ public class ModelSearchParameters extends BaseSearchParameters getRequestParameters() { var parameters = new HashMap<>(super.getRequestParameters()); - if (this.getName() != null && !this.getName().isEmpty()) { - parameters.put("name", this.getName()); + if (getName() != null) { + parameters.put("name", getName()); } - if (this.getModelType() != null && !this.getModelType().isEmpty()) { - parameters.put("model_type", this.getModelType()); + if (getModelType() != null) { + parameters.put("model_type", getModelType()); } return parameters; @@ -65,9 +75,7 @@ public static final class Builder extends BaseSearchParameters.BaseBuilder getRequestParameters() { var parameters = new HashMap<>(super.getRequestParameters()); - parameters.put("model_id", this.getModelId()); + parameters.put("model_id", getModelId()); - if (this.getFilename() != null && !this.getFilename().isEmpty()) { - parameters.put("filename", this.getFilename()); + if (getFilename() != null && !getFilename().isEmpty()) { + parameters.put("filename", getFilename()); } return parameters; @@ -74,9 +78,7 @@ public static final class Builder extends BaseSearchParameters.BaseBuilder