Skip to content

Commit

Permalink
Rework GitLabForm to preserve the value type
Browse files Browse the repository at this point in the history
Fixes #1213
  • Loading branch information
jmini committed Dec 24, 2024
1 parent 28f43e5 commit be0f491
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 136 deletions.
76 changes: 48 additions & 28 deletions gitlab4j-api/src/main/java/org/gitlab4j/api/GitLabApiForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -35,13 +36,33 @@ public GitLabApiForm(MultivaluedHashMap<String, String> 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<String, String> e : form.getFormValues().entrySet()) {
this.param(e.getKey(), e.getValue());
for (Entry<String, GitLabFormValue> 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<String, ?> mapValue = (Map<String, ?>) value.getValue();
withParam(e.getKey(), mapValue, value.isRequired());
break;
case OBJECT:
default:
withParam(e.getKey(), value.getValue(), value.isRequired());
break;
}
}
}

Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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 <T> GitLabApiForm withParam(String name, List<T> values) {
return (withParam(name, values, false));
public GitLabApiForm withParam(String name, List<?> values) {
return withParam(name, values, false);
}

/**
Expand All @@ -126,23 +147,23 @@ public <T> GitLabApiForm withParam(String name, List<T> values) {
* @return this GitLabAPiForm instance
* @throws IllegalArgumentException if a required parameter is null or empty
*/
public <T> GitLabApiForm withParam(String name, List<T> 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;
}

/**
Expand All @@ -154,15 +175,14 @@ public <T> GitLabApiForm withParam(String name, List<T> values, boolean required
* @return this GitLabAPiForm instance
* @throws IllegalArgumentException if a required parameter is null or empty
*/
public GitLabApiForm withParam(String name, Map<String, ?> variables, boolean required)
throws IllegalArgumentException {
public GitLabApiForm withParam(String name, Map<String, ?> 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<String, ?> variable : variables.entrySet()) {
Expand All @@ -172,7 +192,7 @@ public GitLabApiForm withParam(String name, Map<String, ?> variables, boolean re
}
}

return (this);
return this;
}

/**
Expand All @@ -185,14 +205,14 @@ public GitLabApiForm withParam(String name, Map<String, ?> 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();
Expand All @@ -201,7 +221,7 @@ public GitLabApiForm withParam(String name, Object value, boolean required) thro
}

this.param(name.trim(), stringValue);
return (this);
return this;
}

/**
Expand All @@ -213,7 +233,7 @@ public GitLabApiForm withParam(String name, Object value, boolean required) thro
public GitLabApiForm withParam(List<Variable> variables) {

if (variables == null || variables.isEmpty()) {
return (this);
return this;
}

variables.forEach(v -> {
Expand All @@ -223,6 +243,6 @@ public GitLabApiForm withParam(List<Variable> variables) {
}
});

return (this);
return this;
}
}
Loading

0 comments on commit be0f491

Please sign in to comment.