From be0f49159aaaa2fb06d8ba3ef7a753e84b731ccd Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Tue, 24 Dec 2024 13:00:55 +0100 Subject: [PATCH] Rework GitLabForm to preserve the value type Fixes #1213 --- .../java/org/gitlab4j/api/GitLabApiForm.java | 76 ++++++---- .../java/org/gitlab4j/models/GitLabForm.java | 143 +++++------------- .../org/gitlab4j/models/GitLabFormValue.java | 32 ++++ .../gitlab4j/models/GitLabFormValueType.java | 9 ++ 4 files changed, 124 insertions(+), 136 deletions(-) create mode 100644 gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabFormValue.java create mode 100644 gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabFormValueType.java diff --git a/gitlab4j-api/src/main/java/org/gitlab4j/api/GitLabApiForm.java b/gitlab4j-api/src/main/java/org/gitlab4j/api/GitLabApiForm.java index ecc1d2e1e..3060b977a 100644 --- a/gitlab4j-api/src/main/java/org/gitlab4j/api/GitLabApiForm.java +++ b/gitlab4j-api/src/main/java/org/gitlab4j/api/GitLabApiForm.java @@ -11,6 +11,7 @@ import org.gitlab4j.api.models.AccessLevel; import org.gitlab4j.api.models.Variable; import org.gitlab4j.models.GitLabForm; +import org.gitlab4j.models.GitLabFormValue; import org.gitlab4j.models.utils.ISO8601; /** @@ -35,13 +36,33 @@ public GitLabApiForm(MultivaluedHashMap map) { public GitLabApiForm(int page, int perPage) { super(); withParam(AbstractApi.PAGE_PARAM, page); - withParam(AbstractApi.PER_PAGE_PARAM, (Integer) perPage); + withParam(AbstractApi.PER_PAGE_PARAM, perPage); } public GitLabApiForm(GitLabForm form) { super(); - for (Entry e : form.getFormValues().entrySet()) { - this.param(e.getKey(), e.getValue()); + for (Entry e : form.getFormValues().entrySet()) { + GitLabFormValue value = e.getValue(); + switch (value.getType()) { + case ACCESS_LEVEL: + withParam(e.getKey(), (AccessLevel) value.getValue(), value.isRequired()); + break; + case DATE: + withParam(e.getKey(), (Date) value.getValue(), value.isRequired()); + break; + case LIST: + withParam(e.getKey(), (List) value.getValue(), value.isRequired()); + break; + case MAP: + @SuppressWarnings("unchecked") + Map mapValue = (Map) value.getValue(); + withParam(e.getKey(), mapValue, value.isRequired()); + break; + case OBJECT: + default: + withParam(e.getKey(), value.getValue(), value.isRequired()); + break; + } } } @@ -52,8 +73,8 @@ public GitLabApiForm(GitLabForm form) { * @param value the value of the field/attribute to add * @return this GitLabAPiForm instance */ - public GitLabApiForm withParam(String name, Object value) throws IllegalArgumentException { - return (withParam(name, value, false)); + public GitLabApiForm withParam(String name, Object value) { + return withParam(name, value, false); } /** @@ -63,8 +84,8 @@ public GitLabApiForm withParam(String name, Object value) throws IllegalArgument * @param date the value of the field/attribute to add * @return this GitLabAPiForm instance */ - public GitLabApiForm withParam(String name, Date date) throws IllegalArgumentException { - return (withParam(name, date, false)); + public GitLabApiForm withParam(String name, Date date) { + return withParam(name, date, false); } /** @@ -76,8 +97,8 @@ public GitLabApiForm withParam(String name, Date date) throws IllegalArgumentExc * @return this GitLabAPiForm instance * @throws IllegalArgumentException if a required parameter is null or empty */ - public GitLabApiForm withParam(String name, Date date, boolean required) throws IllegalArgumentException { - return (withParam(name, (date == null ? null : ISO8601.toString(date)), required)); + public GitLabApiForm withParam(String name, Date date, boolean required) { + return withParam(name, date == null ? null : ISO8601.toString(date), required); } /** @@ -87,8 +108,8 @@ public GitLabApiForm withParam(String name, Date date, boolean required) throws * @param level the value of the field/attribute to add * @return this GitLabAPiForm instance */ - public GitLabApiForm withParam(String name, AccessLevel level) throws IllegalArgumentException { - return (withParam(name, level, false)); + public GitLabApiForm withParam(String name, AccessLevel level) { + return withParam(name, level, false); } /** @@ -100,8 +121,8 @@ public GitLabApiForm withParam(String name, AccessLevel level) throws IllegalArg * @return this GitLabAPiForm instance * @throws IllegalArgumentException if a required parameter is null or empty */ - public GitLabApiForm withParam(String name, AccessLevel level, boolean required) throws IllegalArgumentException { - return (withParam(name, (level == null ? null : level.toValue()), required)); + public GitLabApiForm withParam(String name, AccessLevel level, boolean required) { + return withParam(name, level == null ? null : level.toValue(), required); } /** @@ -112,8 +133,8 @@ public GitLabApiForm withParam(String name, AccessLevel level, boolean required) * @param values a List containing the values of the field/attribute to add * @return this GitLabAPiForm instance */ - public GitLabApiForm withParam(String name, List values) { - return (withParam(name, values, false)); + public GitLabApiForm withParam(String name, List values) { + return withParam(name, values, false); } /** @@ -126,23 +147,23 @@ public GitLabApiForm withParam(String name, List values) { * @return this GitLabAPiForm instance * @throws IllegalArgumentException if a required parameter is null or empty */ - public GitLabApiForm withParam(String name, List values, boolean required) throws IllegalArgumentException { + public GitLabApiForm withParam(String name, List values, boolean required) { if (values == null || values.isEmpty()) { if (required) { throw new IllegalArgumentException(name + " cannot be empty or null"); } - return (this); + return this; } - for (T value : values) { + for (Object value : values) { if (value != null) { this.param(name + "[]", value.toString()); } } - return (this); + return this; } /** @@ -154,15 +175,14 @@ public GitLabApiForm withParam(String name, List values, boolean required * @return this GitLabAPiForm instance * @throws IllegalArgumentException if a required parameter is null or empty */ - public GitLabApiForm withParam(String name, Map variables, boolean required) - throws IllegalArgumentException { + public GitLabApiForm withParam(String name, Map variables, boolean required) { if (variables == null || variables.isEmpty()) { if (required) { throw new IllegalArgumentException(name + " cannot be empty or null"); } - return (this); + return this; } for (Entry variable : variables.entrySet()) { @@ -172,7 +192,7 @@ public GitLabApiForm withParam(String name, Map variables, boolean re } } - return (this); + return this; } /** @@ -185,14 +205,14 @@ public GitLabApiForm withParam(String name, Map variables, boolean re * @return this GitLabAPiForm instance * @throws IllegalArgumentException if a required parameter is null or empty */ - public GitLabApiForm withParam(String name, Object value, boolean required) throws IllegalArgumentException { + public GitLabApiForm withParam(String name, Object value, boolean required) { if (value == null) { if (required) { throw new IllegalArgumentException(name + " cannot be empty or null"); } - return (this); + return this; } String stringValue = value.toString(); @@ -201,7 +221,7 @@ public GitLabApiForm withParam(String name, Object value, boolean required) thro } this.param(name.trim(), stringValue); - return (this); + return this; } /** @@ -213,7 +233,7 @@ public GitLabApiForm withParam(String name, Object value, boolean required) thro public GitLabApiForm withParam(List variables) { if (variables == null || variables.isEmpty()) { - return (this); + return this; } variables.forEach(v -> { @@ -223,6 +243,6 @@ public GitLabApiForm withParam(List variables) { } }); - return (this); + return this; } } diff --git a/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabForm.java b/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabForm.java index 4b4b6cfa9..98dc6852a 100644 --- a/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabForm.java +++ b/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabForm.java @@ -4,15 +4,12 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import org.gitlab4j.api.models.AccessLevel; -import org.gitlab4j.api.models.Variable; -import org.gitlab4j.models.utils.ISO8601; public class GitLabForm { - private Map formValues = new LinkedHashMap<>(); + private Map formValues = new LinkedHashMap<>(); public GitLabForm() { super(); @@ -27,7 +24,7 @@ public GitLabForm() { public GitLabForm(int page, int perPage) { super(); withParam(Constants.PAGE_PARAM, page); - withParam(Constants.PER_PAGE_PARAM, (Integer) perPage); + withParam(Constants.PER_PAGE_PARAM, perPage); } /** @@ -35,10 +32,10 @@ public GitLabForm(int page, int perPage) { * * @param name the name of the field/attribute to add * @param value the value of the field/attribute to add - * @return this GitLabAPiForm instance + * @return this {@link GitLabForm} instance */ - public GitLabForm withParam(String name, Object value) throws IllegalArgumentException { - return (withParam(name, value, false)); + public GitLabForm withParam(String name, Object value) { + return withParam(name, value, false); } /** @@ -46,10 +43,10 @@ public GitLabForm withParam(String name, Object value) throws IllegalArgumentExc * * @param name the name of the field/attribute to add * @param date the value of the field/attribute to add - * @return this GitLabAPiForm instance + * @return this {@link GitLabForm} instance */ - public GitLabForm withParam(String name, Date date) throws IllegalArgumentException { - return (withParam(name, date, false)); + public GitLabForm withParam(String name, Date date) { + return withParam(name, date, false); } /** @@ -58,11 +55,11 @@ public GitLabForm withParam(String name, Date date) throws IllegalArgumentExcept * @param name the name of the field/attribute to add * @param date the value of the field/attribute to add * @param required the field is required flag - * @return this GitLabAPiForm instance - * @throws IllegalArgumentException if a required parameter is null or empty + * @return this {@link GitLabForm} instance */ - public GitLabForm withParam(String name, Date date, boolean required) throws IllegalArgumentException { - return (withParam(name, (date == null ? null : ISO8601.toString(date)), required)); + public GitLabForm withParam(String name, Date date, boolean required) { + formValues.put(name, new GitLabFormValue(date, GitLabFormValueType.DATE, required)); + return this; } /** @@ -70,10 +67,10 @@ public GitLabForm withParam(String name, Date date, boolean required) throws Ill * * @param name the name of the field/attribute to add * @param level the value of the field/attribute to add - * @return this GitLabAPiForm instance + * @return this {@link GitLabForm} instance */ - public GitLabForm withParam(String name, AccessLevel level) throws IllegalArgumentException { - return (withParam(name, level, false)); + public GitLabForm withParam(String name, AccessLevel level) { + return withParam(name, level, false); } /** @@ -82,11 +79,11 @@ public GitLabForm withParam(String name, AccessLevel level) throws IllegalArgume * @param name the name of the field/attribute to add * @param level the value of the field/attribute to add * @param required the field is required flag - * @return this GitLabAPiForm instance - * @throws IllegalArgumentException if a required parameter is null or empty + * @return this {@link GitLabForm} instance */ - public GitLabForm withParam(String name, AccessLevel level, boolean required) throws IllegalArgumentException { - return (withParam(name, (level == null ? null : level.toValue()), required)); + public GitLabForm withParam(String name, AccessLevel level, boolean required) { + formValues.put(name, new GitLabFormValue(level, GitLabFormValueType.ACCESS_LEVEL, required)); + return this; } /** @@ -95,10 +92,10 @@ public GitLabForm withParam(String name, AccessLevel level, boolean required) th * @param the type contained by the List * @param name the name of the field/attribute to add * @param values a List containing the values of the field/attribute to add - * @return this GitLabAPiForm instance + * @return this {@link GitLabForm} instance */ - public GitLabForm withParam(String name, List values) { - return (withParam(name, values, false)); + public GitLabForm withParam(String name, List values) { + return withParam(name, values, false); } /** @@ -108,26 +105,11 @@ public GitLabForm withParam(String name, List values) { * @param name the name of the field/attribute to add * @param values a List containing the values of the field/attribute to add * @param required the field is required flag - * @return this GitLabAPiForm instance - * @throws IllegalArgumentException if a required parameter is null or empty + * @return this {@link GitLabForm} instance */ - public GitLabForm withParam(String name, List values, boolean required) throws IllegalArgumentException { - - if (values == null || values.isEmpty()) { - if (required) { - throw new IllegalArgumentException(name + " cannot be empty or null"); - } - - return (this); - } - - for (T value : values) { - if (value != null) { - formValues.put(name + "[]", value.toString()); - } - } - - return (this); + public GitLabForm withParam(String name, List values, boolean required) { + formValues.put(name, new GitLabFormValue(values, GitLabFormValueType.LIST, required)); + return this; } /** @@ -136,82 +118,27 @@ public GitLabForm withParam(String name, List values, boolean required) t * @param name the name of the field/attribute to add * @param variables a Map containing array of hashes * @param required the field is required flag - * @return this GitLabAPiForm instance - * @throws IllegalArgumentException if a required parameter is null or empty + * @return this {@link GitLabForm} instance */ - public GitLabForm withParam(String name, Map variables, boolean required) - throws IllegalArgumentException { - - if (variables == null || variables.isEmpty()) { - if (required) { - throw new IllegalArgumentException(name + " cannot be empty or null"); - } - - return (this); - } - - for (Entry variable : variables.entrySet()) { - Object value = variable.getValue(); - if (value != null) { - formValues.put(name + "[" + variable.getKey() + "]", value.toString()); - } - } - - return (this); + public GitLabForm withParam(String name, Map variables, boolean required) { + formValues.put(name, new GitLabFormValue(variables, GitLabFormValueType.MAP, required)); + return this; } /** * Fluent method for adding query and form parameters to a get() or post() call. - * If required is true and value is null, will throw an IllegalArgumentException. * * @param name the name of the field/attribute to add * @param value the value of the field/attribute to add * @param required the field is required flag - * @return this GitLabAPiForm instance - * @throws IllegalArgumentException if a required parameter is null or empty - */ - public GitLabForm withParam(String name, Object value, boolean required) throws IllegalArgumentException { - - if (value == null) { - if (required) { - throw new IllegalArgumentException(name + " cannot be empty or null"); - } - - return (this); - } - - String stringValue = value.toString(); - if (required && stringValue.trim().length() == 0) { - throw new IllegalArgumentException(name + " cannot be empty or null"); - } - - formValues.put(name.trim(), stringValue); - return (this); - } - - /** - * Fluent method for adding a List<Variable> type query and form parameters to a get(), post(), or put() call. - * - * @param variables the List of Variable to add - * @return this GitLabAPiForm instance + * @return this {@link GitLabForm} instance */ - public GitLabForm withParam(List variables) { - - if (variables == null || variables.isEmpty()) { - return (this); - } - - variables.forEach(v -> { - String value = v.getValue(); - if (value != null) { - formValues.put("variables[" + v.getKey() + "]", value); - } - }); - - return (this); + public GitLabForm withParam(String name, Object value, boolean required) { + formValues.put(name, new GitLabFormValue(value, GitLabFormValueType.OBJECT, required)); + return this; } - public Map getFormValues() { + public Map getFormValues() { return formValues; } } diff --git a/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabFormValue.java b/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabFormValue.java new file mode 100644 index 000000000..12b536d9e --- /dev/null +++ b/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabFormValue.java @@ -0,0 +1,32 @@ +package org.gitlab4j.models; + +public class GitLabFormValue { + + private Object value; + private GitLabFormValueType type; + private boolean required; + + public GitLabFormValue(Object value, GitLabFormValueType type, boolean required) { + super(); + this.value = value; + this.type = type; + this.required = required; + } + + public Object getValue() { + return value; + } + + public GitLabFormValueType getType() { + return type; + } + + public boolean isRequired() { + return required; + } + + @Override + public String toString() { + return "GitLabFormValue [value=" + value + ", type=" + type + ", required=" + required + "]"; + } +} diff --git a/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabFormValueType.java b/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabFormValueType.java new file mode 100644 index 000000000..7c176ed13 --- /dev/null +++ b/gitlab4j-models/src/main/java/org/gitlab4j/models/GitLabFormValueType.java @@ -0,0 +1,9 @@ +package org.gitlab4j.models; + +public enum GitLabFormValueType { + DATE, + ACCESS_LEVEL, + OBJECT, + LIST, + MAP, +}