Skip to content

Commit bbe68e4

Browse files
committed
Optional support for lists
1 parent 5978290 commit bbe68e4

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

src/main/java/org/oidc/msg/validator/ArrayClaimValidator.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,58 @@
1616

1717
package org.oidc.msg.validator;
1818

19+
import java.util.Arrays;
20+
import java.util.List;
21+
1922
import org.oidc.msg.InvalidClaimException;
2023

2124
/**
22-
* A {@link ClaimValidator} for an array of strings or space separated strings.
25+
* A {@link ClaimValidator} for an array of strings or space separated strings. Optionally also
26+
* supports a list of strings, which is disabled by default.
2327
*/
2428
public class ArrayClaimValidator implements ClaimValidator<String> {
29+
30+
/** The flag to accept also list of strings in addition to the array. Defaults to false. */
31+
private boolean acceptLists;
32+
33+
/**
34+
* Constructor.
35+
*/
36+
public ArrayClaimValidator() {
37+
this(false);
38+
}
39+
40+
/**
41+
* Constructor.
42+
* @param lists The flag to accept also list of strings in addition to the arrays.
43+
*/
44+
public ArrayClaimValidator(boolean lists) {
45+
acceptLists = lists;
46+
}
2547

48+
@SuppressWarnings("unchecked")
2649
@Override
2750
public String validate(Object value) throws InvalidClaimException {
2851
if (value instanceof String) {
2952
return (String) value;
3053
}
3154
if (!(value instanceof String[]) || ((String[]) value).length == 0) {
55+
if (acceptLists && (value instanceof List) && !((List<?>) value).isEmpty()) {
56+
List<?> list = (List<?>) value;
57+
if (list.get(0) instanceof String) {
58+
return buildFromList((List<String>) value);
59+
}
60+
}
3261
throw new InvalidClaimException(
3362
String.format("Parameter '%s' is not of expected type", value));
3463
}
64+
return buildFromList(Arrays.asList((String[]) value));
65+
}
66+
67+
protected String buildFromList(List<String> list) {
3568
String spaceSeparatedString = "";
36-
for (String item : (String[]) value) {
37-
spaceSeparatedString += spaceSeparatedString.length() > 0 ? " " + item : item;
69+
for (String item : list) {
70+
spaceSeparatedString += spaceSeparatedString.length() > 0 ? " " + item : item;
3871
}
3972
return spaceSeparatedString;
4073
}

src/test/java/org/oidc/msg/validator/ArrayClaimValidatorTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.oidc.msg.validator;
1818

19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
1922
import org.junit.Assert;
2023
import org.junit.Before;
2124
import org.junit.Test;
@@ -38,7 +41,13 @@ public void testPlainString() throws InvalidClaimException {
3841
}
3942

4043
@Test(expected = InvalidClaimException.class)
41-
public void testEmptyArray() throws InvalidClaimException {
44+
public void testEmptyArrayWithoutListsEnabled() throws InvalidClaimException {
45+
validator.validate(new String[0]);
46+
}
47+
48+
@Test(expected = InvalidClaimException.class)
49+
public void testEmptyArrayWithListsEnabled() throws InvalidClaimException {
50+
validator = new ArrayClaimValidator(true);
4251
validator.validate(new String[0]);
4352
}
4453

@@ -53,5 +62,27 @@ public void testNonEmptyArray() throws InvalidClaimException {
5362
public void testEmptyString() throws InvalidClaimException {
5463
Assert.assertEquals("", validator.validate(""));
5564
}
65+
66+
@Test(expected = InvalidClaimException.class)
67+
public void testListWithoutEnablingLists() throws InvalidClaimException {
68+
validator.validate(Arrays.asList("string1", "string2"));
69+
}
70+
71+
@Test(expected = InvalidClaimException.class)
72+
public void testEmptyListWithEnablingLists() throws InvalidClaimException {
73+
validator.validate(new ArrayList<>());
74+
}
75+
76+
@Test(expected = InvalidClaimException.class)
77+
public void testInvalidListWithEnablingLists() throws InvalidClaimException {
78+
validator.validate(Arrays.asList(1, 2));
79+
}
80+
81+
@Test
82+
public void testListWithEnablingLists() throws InvalidClaimException {
83+
validator = new ArrayClaimValidator(true);
84+
Assert.assertEquals("string", validator.validate(Arrays.asList("string")));
85+
Assert.assertEquals("string1 string2", validator.validate(Arrays.asList("string1 string2")));
86+
}
5687

5788
}

0 commit comments

Comments
 (0)