Skip to content

Commit be0f491

Browse files
committed
Rework GitLabForm to preserve the value type
Fixes #1213
1 parent 28f43e5 commit be0f491

File tree

4 files changed

+124
-136
lines changed

4 files changed

+124
-136
lines changed

gitlab4j-api/src/main/java/org/gitlab4j/api/GitLabApiForm.java

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.gitlab4j.api.models.AccessLevel;
1212
import org.gitlab4j.api.models.Variable;
1313
import org.gitlab4j.models.GitLabForm;
14+
import org.gitlab4j.models.GitLabFormValue;
1415
import org.gitlab4j.models.utils.ISO8601;
1516

1617
/**
@@ -35,13 +36,33 @@ public GitLabApiForm(MultivaluedHashMap<String, String> map) {
3536
public GitLabApiForm(int page, int perPage) {
3637
super();
3738
withParam(AbstractApi.PAGE_PARAM, page);
38-
withParam(AbstractApi.PER_PAGE_PARAM, (Integer) perPage);
39+
withParam(AbstractApi.PER_PAGE_PARAM, perPage);
3940
}
4041

4142
public GitLabApiForm(GitLabForm form) {
4243
super();
43-
for (Entry<String, String> e : form.getFormValues().entrySet()) {
44-
this.param(e.getKey(), e.getValue());
44+
for (Entry<String, GitLabFormValue> e : form.getFormValues().entrySet()) {
45+
GitLabFormValue value = e.getValue();
46+
switch (value.getType()) {
47+
case ACCESS_LEVEL:
48+
withParam(e.getKey(), (AccessLevel) value.getValue(), value.isRequired());
49+
break;
50+
case DATE:
51+
withParam(e.getKey(), (Date) value.getValue(), value.isRequired());
52+
break;
53+
case LIST:
54+
withParam(e.getKey(), (List<?>) value.getValue(), value.isRequired());
55+
break;
56+
case MAP:
57+
@SuppressWarnings("unchecked")
58+
Map<String, ?> mapValue = (Map<String, ?>) value.getValue();
59+
withParam(e.getKey(), mapValue, value.isRequired());
60+
break;
61+
case OBJECT:
62+
default:
63+
withParam(e.getKey(), value.getValue(), value.isRequired());
64+
break;
65+
}
4566
}
4667
}
4768

@@ -52,8 +73,8 @@ public GitLabApiForm(GitLabForm form) {
5273
* @param value the value of the field/attribute to add
5374
* @return this GitLabAPiForm instance
5475
*/
55-
public GitLabApiForm withParam(String name, Object value) throws IllegalArgumentException {
56-
return (withParam(name, value, false));
76+
public GitLabApiForm withParam(String name, Object value) {
77+
return withParam(name, value, false);
5778
}
5879

5980
/**
@@ -63,8 +84,8 @@ public GitLabApiForm withParam(String name, Object value) throws IllegalArgument
6384
* @param date the value of the field/attribute to add
6485
* @return this GitLabAPiForm instance
6586
*/
66-
public GitLabApiForm withParam(String name, Date date) throws IllegalArgumentException {
67-
return (withParam(name, date, false));
87+
public GitLabApiForm withParam(String name, Date date) {
88+
return withParam(name, date, false);
6889
}
6990

7091
/**
@@ -76,8 +97,8 @@ public GitLabApiForm withParam(String name, Date date) throws IllegalArgumentExc
7697
* @return this GitLabAPiForm instance
7798
* @throws IllegalArgumentException if a required parameter is null or empty
7899
*/
79-
public GitLabApiForm withParam(String name, Date date, boolean required) throws IllegalArgumentException {
80-
return (withParam(name, (date == null ? null : ISO8601.toString(date)), required));
100+
public GitLabApiForm withParam(String name, Date date, boolean required) {
101+
return withParam(name, date == null ? null : ISO8601.toString(date), required);
81102
}
82103

83104
/**
@@ -87,8 +108,8 @@ public GitLabApiForm withParam(String name, Date date, boolean required) throws
87108
* @param level the value of the field/attribute to add
88109
* @return this GitLabAPiForm instance
89110
*/
90-
public GitLabApiForm withParam(String name, AccessLevel level) throws IllegalArgumentException {
91-
return (withParam(name, level, false));
111+
public GitLabApiForm withParam(String name, AccessLevel level) {
112+
return withParam(name, level, false);
92113
}
93114

94115
/**
@@ -100,8 +121,8 @@ public GitLabApiForm withParam(String name, AccessLevel level) throws IllegalArg
100121
* @return this GitLabAPiForm instance
101122
* @throws IllegalArgumentException if a required parameter is null or empty
102123
*/
103-
public GitLabApiForm withParam(String name, AccessLevel level, boolean required) throws IllegalArgumentException {
104-
return (withParam(name, (level == null ? null : level.toValue()), required));
124+
public GitLabApiForm withParam(String name, AccessLevel level, boolean required) {
125+
return withParam(name, level == null ? null : level.toValue(), required);
105126
}
106127

107128
/**
@@ -112,8 +133,8 @@ public GitLabApiForm withParam(String name, AccessLevel level, boolean required)
112133
* @param values a List containing the values of the field/attribute to add
113134
* @return this GitLabAPiForm instance
114135
*/
115-
public <T> GitLabApiForm withParam(String name, List<T> values) {
116-
return (withParam(name, values, false));
136+
public GitLabApiForm withParam(String name, List<?> values) {
137+
return withParam(name, values, false);
117138
}
118139

119140
/**
@@ -126,23 +147,23 @@ public <T> GitLabApiForm withParam(String name, List<T> values) {
126147
* @return this GitLabAPiForm instance
127148
* @throws IllegalArgumentException if a required parameter is null or empty
128149
*/
129-
public <T> GitLabApiForm withParam(String name, List<T> values, boolean required) throws IllegalArgumentException {
150+
public GitLabApiForm withParam(String name, List<?> values, boolean required) {
130151

131152
if (values == null || values.isEmpty()) {
132153
if (required) {
133154
throw new IllegalArgumentException(name + " cannot be empty or null");
134155
}
135156

136-
return (this);
157+
return this;
137158
}
138159

139-
for (T value : values) {
160+
for (Object value : values) {
140161
if (value != null) {
141162
this.param(name + "[]", value.toString());
142163
}
143164
}
144165

145-
return (this);
166+
return this;
146167
}
147168

148169
/**
@@ -154,15 +175,14 @@ public <T> GitLabApiForm withParam(String name, List<T> values, boolean required
154175
* @return this GitLabAPiForm instance
155176
* @throws IllegalArgumentException if a required parameter is null or empty
156177
*/
157-
public GitLabApiForm withParam(String name, Map<String, ?> variables, boolean required)
158-
throws IllegalArgumentException {
178+
public GitLabApiForm withParam(String name, Map<String, ?> variables, boolean required) {
159179

160180
if (variables == null || variables.isEmpty()) {
161181
if (required) {
162182
throw new IllegalArgumentException(name + " cannot be empty or null");
163183
}
164184

165-
return (this);
185+
return this;
166186
}
167187

168188
for (Entry<String, ?> variable : variables.entrySet()) {
@@ -172,7 +192,7 @@ public GitLabApiForm withParam(String name, Map<String, ?> variables, boolean re
172192
}
173193
}
174194

175-
return (this);
195+
return this;
176196
}
177197

178198
/**
@@ -185,14 +205,14 @@ public GitLabApiForm withParam(String name, Map<String, ?> variables, boolean re
185205
* @return this GitLabAPiForm instance
186206
* @throws IllegalArgumentException if a required parameter is null or empty
187207
*/
188-
public GitLabApiForm withParam(String name, Object value, boolean required) throws IllegalArgumentException {
208+
public GitLabApiForm withParam(String name, Object value, boolean required) {
189209

190210
if (value == null) {
191211
if (required) {
192212
throw new IllegalArgumentException(name + " cannot be empty or null");
193213
}
194214

195-
return (this);
215+
return this;
196216
}
197217

198218
String stringValue = value.toString();
@@ -201,7 +221,7 @@ public GitLabApiForm withParam(String name, Object value, boolean required) thro
201221
}
202222

203223
this.param(name.trim(), stringValue);
204-
return (this);
224+
return this;
205225
}
206226

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

215235
if (variables == null || variables.isEmpty()) {
216-
return (this);
236+
return this;
217237
}
218238

219239
variables.forEach(v -> {
@@ -223,6 +243,6 @@ public GitLabApiForm withParam(List<Variable> variables) {
223243
}
224244
});
225245

226-
return (this);
246+
return this;
227247
}
228248
}

0 commit comments

Comments
 (0)