Skip to content

Commit 422db6b

Browse files
committed
Fixed data binding for NoteableType (#149).
1 parent 46b72ec commit 422db6b

File tree

4 files changed

+83
-23
lines changed

4 files changed

+83
-23
lines changed

src/main/java/org/gitlab4j/api/models/Note.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,24 @@ public String toString() {
3737
}
3838
}
3939

40-
public static enum NotableType {
41-
ISSUE("Issue"), MERGE_REQUEST("MergeRequest"), SNIPPET("Snippet");
40+
public static enum NoteableType {
4241

43-
private String name;
42+
ISSUE, MERGE_REQUEST, SNIPPET;
43+
private static JacksonJsonEnumHelper<NoteableType> enumHelper = new JacksonJsonEnumHelper<>(NoteableType.class, true, true);
4444

45-
NotableType(String name) {
46-
this.name = name;
45+
@JsonCreator
46+
public static NoteableType forValue(String value) {
47+
return enumHelper.forValue(value);
48+
}
49+
50+
@JsonValue
51+
public String toValue() {
52+
return (enumHelper.toString(this));
4753
}
4854

4955
@Override
5056
public String toString() {
51-
return (name);
57+
return (enumHelper.toString(this));
5258
}
5359
}
5460

@@ -61,7 +67,8 @@ public String toString() {
6167
private String fileName;
6268
private Integer id;
6369
private Integer noteableId;
64-
private NotableType noteableType;
70+
private NoteableType noteableType;
71+
private Integer noteableIid;
6572
private Boolean system;
6673
private String title;
6774
private String updatedAt;
@@ -139,14 +146,22 @@ public void setNoteableId(Integer noteableId) {
139146
this.noteableId = noteableId;
140147
}
141148

142-
public NotableType getNoteableType() {
149+
public NoteableType getNoteableType() {
143150
return noteableType;
144151
}
145152

146-
public void setNoteableType(NotableType noteableType) {
153+
public void setNoteableType(NoteableType noteableType) {
147154
this.noteableType = noteableType;
148155
}
149156

157+
public Integer getNoteableIid() {
158+
return noteableIid;
159+
}
160+
161+
public void setNoteableIid(Integer noteableIid) {
162+
this.noteableIid = noteableIid;
163+
}
164+
150165
public Boolean getSystem() {
151166
return system;
152167
}

src/main/java/org/gitlab4j/api/utils/JacksonJsonEnumHelper.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ public JacksonJsonEnumHelper(Class<E> enumType, boolean firstLetterCapitalized)
3131
}
3232
}
3333

34+
public JacksonJsonEnumHelper(Class<E> enumType, boolean firstLetterCapitalized, boolean camelCased) {
35+
36+
valuesMap = new HashMap<>();
37+
namesMap = new HashMap<>();
38+
39+
for (E e : enumType.getEnumConstants()) {
40+
41+
char[] chars = e.name().toLowerCase().toCharArray();
42+
StringBuilder nameBuf = new StringBuilder(chars.length);
43+
boolean nextCharIsCapitalized = firstLetterCapitalized;
44+
for (char ch : chars) {
45+
if (ch == '_' && camelCased) {
46+
nextCharIsCapitalized = true;
47+
} else if (nextCharIsCapitalized) {
48+
nextCharIsCapitalized = false;
49+
nameBuf.append(Character.toUpperCase(ch));
50+
} else {
51+
nameBuf.append(ch);
52+
}
53+
}
54+
55+
String name = nameBuf.toString();
56+
valuesMap.put(name, e);
57+
namesMap.put(e, name);
58+
}
59+
}
60+
3461
@JsonCreator
3562
public E forValue(String value) {
3663
return valuesMap.get(value);

src/main/java/org/gitlab4j/api/webhook/NoteEvent.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
import org.gitlab4j.api.models.Diff;
99
import org.gitlab4j.api.models.User;
10+
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
11+
12+
import com.fasterxml.jackson.annotation.JsonCreator;
13+
import com.fasterxml.jackson.annotation.JsonValue;
1014

1115
@XmlAccessorType(XmlAccessType.FIELD)
1216
public class NoteEvent implements Event {
@@ -96,7 +100,7 @@ public EventMergeRequest getMergeRequest() {
96100
public void setMergeRequest(EventMergeRequest mergeRequest) {
97101
this.mergeRequest = mergeRequest;
98102
}
99-
103+
100104
public EventSnippet getSnippet() {
101105
return snippet;
102106
}
@@ -105,21 +109,24 @@ public void setSnippet(EventSnippet snippet) {
105109
this.snippet = snippet;
106110
}
107111

108-
public enum NoteableType {
109-
COMMIT("Commit"),
110-
ISSUE("Issue"),
111-
MERGE_REQUEST("MergeRequest"),
112-
SNIPPET("Snippet");
112+
public static enum NoteableType {
113113

114-
private String name;
114+
ISSUE, MERGE_REQUEST, SNIPPET, COMMIT;
115+
private static JacksonJsonEnumHelper<NoteableType> enumHelper = new JacksonJsonEnumHelper<>(NoteableType.class, true, true);
116+
117+
@JsonCreator
118+
public static NoteableType forValue(String value) {
119+
return enumHelper.forValue(value);
120+
}
115121

116-
NoteableType(String name) {
117-
this.name = name;
122+
@JsonValue
123+
public String toValue() {
124+
return (enumHelper.toString(this));
118125
}
119126

120127
@Override
121128
public String toString() {
122-
return (name);
129+
return (enumHelper.toString(this));
123130
}
124131
}
125132

@@ -128,7 +135,7 @@ public static class ObjectAttributes {
128135

129136
private Integer id;
130137
private String note;
131-
private NoteableType notableType;
138+
private NoteableType noteableType;
132139
private Integer authorId;
133140
private Date createdAt;
134141
private Date updatedAt;
@@ -158,11 +165,11 @@ public void setNote(String note) {
158165
}
159166

160167
public NoteableType getNoteableType() {
161-
return notableType;
168+
return noteableType;
162169
}
163170

164-
public void setNoteableType(NoteableType notableType) {
165-
this.notableType = notableType;
171+
public void NoteableType(NoteableType notableType) {
172+
this.noteableType = notableType;
166173
}
167174

168175
public Integer getAuthorId() {

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,17 @@ public void testNote() {
332332
}
333333
}
334334

335+
@Test
336+
public void testMergeRequestNote() {
337+
338+
try {
339+
Note note = makeFakeApiCall(Note.class, "merge-request-note");
340+
assertTrue(compareJson(note, "merge-request-note"));
341+
} catch (Exception e) {
342+
e.printStackTrace();
343+
}
344+
}
345+
335346
@Test
336347
public void testNotificationSettings() {
337348

0 commit comments

Comments
 (0)