Skip to content

Commit 5b2beda

Browse files
committed
Fix enum concatenation especially in the media search endpoint
1 parent 49f923d commit 5b2beda

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

library/src/main/java/me/proxer/library/api/list/MediaSearchEndpoint.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import me.proxer.library.util.ProxerUtils;
1010

1111
import javax.annotation.Nullable;
12+
import java.util.EnumSet;
1213
import java.util.List;
1314
import java.util.Set;
1415

@@ -88,21 +89,21 @@ public final class MediaSearchEndpoint implements PagingLimitEndpoint<List<Media
8889
*/
8990
@Nullable
9091
@Setter
91-
private Set<Genre> genres;
92+
private EnumSet<Genre> genres;
9293

9394
/**
9495
* Sets the excluded genres.
9596
*/
9697
@Nullable
9798
@Setter
98-
private Set<Genre> excludedGenres;
99+
private EnumSet<Genre> excludedGenres;
99100

100101
/**
101102
* Sets the required fsk ratings.
102103
*/
103104
@Nullable
104105
@Setter
105-
private Set<FskConstraint> fskConstraints;
106+
private EnumSet<FskConstraint> fskConstraints;
106107

107108
/**
108109
* {@inheritDoc}
@@ -156,9 +157,9 @@ public MediaSearchEndpoint excludedTags(@Nullable final Set<String> excludedIds)
156157

157158
@Override
158159
public ProxerCall<List<MediaListEntry>> build() {
159-
String joinedGenres = genres == null ? null : ProxerUtils.join(DELIMITER, genres);
160-
String joinedExcludedGenres = excludedGenres == null ? null : ProxerUtils.join(DELIMITER, excludedGenres);
161-
String joinedFskConstraints = fskConstraints == null ? null : ProxerUtils.join(DELIMITER, fskConstraints);
160+
String joinedGenres = genres == null ? null : ProxerUtils.joinEnums(DELIMITER, genres);
161+
String joinedExcludedGenres = excludedGenres == null ? null : ProxerUtils.joinEnums(DELIMITER, excludedGenres);
162+
String joinedFskConstraints = fskConstraints == null ? null : ProxerUtils.joinEnums(DELIMITER, fskConstraints);
162163

163164
return internalApi.mediaSearch(name, language, type, joinedGenres, joinedExcludedGenres, joinedFskConstraints,
164165
sort, length, lengthBound, tags, excludedTags, tagRateFilter, tagSpoilerFilter, page, limit);

library/src/main/java/me/proxer/library/util/ProxerUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import javax.annotation.Nullable;
77
import java.lang.reflect.Field;
8+
import java.util.ArrayList;
9+
import java.util.EnumSet;
810
import java.util.Iterator;
911

1012
/**
@@ -67,4 +69,21 @@ public String join(final String delimiter, final Iterable<?> iterable) {
6769

6870
return builder.toString();
6971
}
72+
73+
/**
74+
* Joins an Iterator of enums with a given delimiter to a {@link String} and returns the result.
75+
* <p>
76+
* For String conversion, the {@link #getApiEnumName(Enum)} method is used, so this method will only work with enums
77+
* from this library.
78+
*/
79+
@SuppressWarnings("checkstyle:designforextension") // Lombok makes it static.
80+
public String joinEnums(final String delimiter, final EnumSet<?> enums) {
81+
final ArrayList<String> convertedEnums = new ArrayList<>();
82+
83+
for (final Enum<?> value : enums) {
84+
convertedEnums.add(ProxerUtils.getApiEnumName(value));
85+
}
86+
87+
return join(delimiter, convertedEnums);
88+
}
7089
}

library/src/test/java/me/proxer/library/api/list/MediaSearchEndpointTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void testPath() throws ProxerException, IOException, InterruptedException
5656
.execute();
5757

5858
assertThat(server.takeRequest().getPath()).isEqualTo("/api/v1/list/entrysearch?name=test&language=en&"
59-
+ "type=all-manga&genre=ACTION%2BADULT&nogenre=DRAMA%2BFANTASY&fsk=FEAR&sort=clicks&length=300&"
59+
+ "type=all-manga&genre=Action%2BAdult&nogenre=Drama%2BFantasy&fsk=fear&sort=clicks&length=300&"
6060
+ "length-limit=down&tags=3%2B7&notags=5%2B20&tagratefilter=rate_1&"
6161
+ "tagspoilerfilter=spoiler_1&p=3&limit=10");
6262
}

library/src/test/java/me/proxer/library/util/ProxerUtilsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Arrays;
99
import java.util.Collections;
10+
import java.util.EnumSet;
1011

1112
import static org.assertj.core.api.Assertions.assertThat;
1213

@@ -45,6 +46,17 @@ public void testJoinEmpty() {
4546
assertThat(ProxerUtils.join(";", Collections.emptyList())).isEmpty();
4647
}
4748

49+
@Test
50+
public void testJoinEnums() {
51+
assertThat(ProxerUtils.joinEnums(";", EnumSet.of(Genre.ADVENTURE, Genre.ACTION, Genre.DRAMA)))
52+
.isEqualTo("Abenteuer;Action;Drama");
53+
}
54+
55+
@Test
56+
public void testJoinEnumsEmpty() {
57+
assertThat(ProxerUtils.joinEnums(";", EnumSet.noneOf(Genre.class))).isEmpty();
58+
}
59+
4860
@Test
4961
public void testIsUtilityClass() {
5062
PrivateConstructorChecker

0 commit comments

Comments
 (0)