Skip to content

Rework GitLabForm to preserve the value type #1217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 48 additions & 30 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,49 +121,47 @@ 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);
}

/**
* Fluent method for adding a List type query and form parameters to a get() or post() call.
*
* @param <T> 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
*/
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);
}

/**
* Fluent method for adding a List type query and form parameters to a get() or post() call.
*
* @param <T> 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
* @param required the field is required flag
* @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 +173,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 +190,7 @@ public GitLabApiForm withParam(String name, Map<String, ?> variables, boolean re
}
}

return (this);
return this;
}

/**
Expand All @@ -185,14 +203,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 +219,7 @@ public GitLabApiForm withParam(String name, Object value, boolean required) thro
}

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

/**
Expand All @@ -213,7 +231,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 +241,6 @@ public GitLabApiForm withParam(List<Variable> variables) {
}
});

return (this);
return this;
}
}
Loading
Loading