Skip to content

Commit 9a825f8

Browse files
committed
remove MissingClaim. Return null when claim or value is missing
1 parent 38af002 commit 9a825f8

9 files changed

Lines changed: 46 additions & 69 deletions

File tree

lib/src/main/java/com/auth0/jwtdecodejava/impl/MissingClaim.java renamed to lib/src/main/java/com/auth0/jwtdecodejava/impl/BaseClaim.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,44 @@
66
import java.util.Date;
77
import java.util.List;
88

9-
public final class MissingClaim implements Claim {
10-
11-
@Override
12-
public boolean isMissing() {
13-
return true;
14-
}
15-
9+
public class BaseClaim implements Claim {
1610
@Override
1711
public boolean isNull() {
18-
return false;
12+
return true;
1913
}
2014

2115
@Override
2216
public Boolean asBoolean() {
23-
throw new JWTException("Missing Claim");
17+
return null;
2418
}
2519

2620
@Override
2721
public Integer asInt() {
28-
throw new JWTException("Missing Claim");
22+
return null;
2923
}
3024

3125
@Override
3226
public Double asDouble() {
33-
throw new JWTException("Missing Claim");
27+
return null;
3428
}
3529

3630
@Override
3731
public String asString() {
38-
throw new JWTException("Missing Claim");
32+
return null;
3933
}
4034

4135
@Override
4236
public Date asDate() {
43-
throw new JWTException("Missing Claim");
37+
return null;
4438
}
4539

4640
@Override
4741
public <T> T[] asArray(Class<T> tClazz) throws JWTException {
48-
throw new JWTException("Missing Claim");
42+
return null;
4943
}
5044

5145
@Override
5246
public <T> List<T> asList(Class<T> tClazz) throws JWTException {
53-
throw new JWTException("Missing Claim");
47+
return null;
5448
}
5549
}

lib/src/main/java/com/auth0/jwtdecodejava/impl/ClaimImpl.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.fasterxml.jackson.core.JsonProcessingException;
66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.fasterxml.jackson.databind.ObjectMapper;
8-
import com.fasterxml.jackson.databind.node.NullNode;
98
import com.sun.istack.internal.NotNull;
109

1110
import java.lang.reflect.Array;
@@ -14,47 +13,37 @@
1413
import java.util.List;
1514
import java.util.Map;
1615

17-
public class ClaimImpl implements Claim {
16+
class ClaimImpl extends BaseClaim {
1817

1918
private final JsonNode data;
2019

21-
public ClaimImpl(@NotNull JsonNode node) {
22-
this.data = node == null ? NullNode.getInstance() : node;
23-
}
24-
25-
@Override
26-
public boolean isMissing() {
27-
return data.isMissingNode();
28-
}
29-
30-
@Override
31-
public boolean isNull() {
32-
return data.isNull() || data.isObject() && data.size() == 0;
20+
private ClaimImpl(@NotNull JsonNode node) {
21+
this.data = node;
3322
}
3423

3524
@Override
3625
public Boolean asBoolean() {
37-
return isNull() || !data.isBoolean() ? null : data.asBoolean();
26+
return !data.isBoolean() ? null : data.asBoolean();
3827
}
3928

4029
@Override
4130
public Integer asInt() {
42-
return isNull() || !data.isNumber() ? null : data.asInt();
31+
return !data.isNumber() ? null : data.asInt();
4332
}
4433

4534
@Override
4635
public Double asDouble() {
47-
return isNull() || !data.isNumber() ? null : data.asDouble();
36+
return !data.isNumber() ? null : data.asDouble();
4837
}
4938

5039
@Override
5140
public String asString() {
52-
return isNull() || !data.isTextual() ? null : data.asText();
41+
return !data.isTextual() ? null : data.asText();
5342
}
5443

5544
@Override
5645
public Date asDate() {
57-
if (isNull() || !data.canConvertToLong()) {
46+
if (!data.canConvertToLong()) {
5847
return null;
5948
}
6049
long seconds = data.asLong();
@@ -64,8 +53,8 @@ public Date asDate() {
6453
@Override
6554
@SuppressWarnings("unchecked")
6655
public <T> T[] asArray(Class<T> tClazz) throws JWTException {
67-
if (data.isNull() || !data.isArray()) {
68-
return (T[]) Array.newInstance(tClazz, 0);
56+
if (!data.isArray()) {
57+
return null;
6958
}
7059

7160
ObjectMapper mapper = new ObjectMapper();
@@ -82,8 +71,8 @@ public <T> T[] asArray(Class<T> tClazz) throws JWTException {
8271

8372
@Override
8473
public <T> List<T> asList(Class<T> tClazz) throws JWTException {
85-
if (data.isNull() || !data.isArray()) {
86-
return new ArrayList<>(0);
74+
if (!data.isArray()) {
75+
return null;
8776
}
8877

8978
ObjectMapper mapper = new ObjectMapper();
@@ -105,8 +94,8 @@ public static Claim extractClaim(@NotNull String claimName, @NotNull Map<String,
10594

10695
@NotNull
10796
public static Claim claimFromNode(JsonNode node) {
108-
if (node == null || node.isMissingNode()) {
109-
return new MissingClaim();
97+
if (node == null || node.isNull() || node.isMissingNode()) {
98+
return new BaseClaim();
11099
}
111100
return new ClaimImpl(node);
112101
}

lib/src/main/java/com/auth0/jwtdecodejava/impl/HeaderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import static com.auth0.jwtdecodejava.impl.ClaimImpl.extractClaim;
99
import static com.auth0.jwtdecodejava.impl.Claims.*;
1010

11-
public class HeaderImpl implements Header {
11+
class HeaderImpl implements Header {
1212
private final Map<String, JsonNode> tree;
1313

14-
public HeaderImpl(Map<String, JsonNode> tree) {
14+
HeaderImpl(Map<String, JsonNode> tree) {
1515
this.tree = tree;
1616
}
1717

lib/src/main/java/com/auth0/jwtdecodejava/impl/JWTParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public JWTParser() {
1818
this(getDefaultObjectMapper());
1919
}
2020

21-
public JWTParser(ObjectMapper mapper) {
21+
JWTParser(ObjectMapper mapper) {
2222
addDeserializers(mapper);
2323
this.mapper = mapper;
2424
}

lib/src/main/java/com/auth0/jwtdecodejava/impl/PayloadDeserializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
public class PayloadDeserializer extends StdDeserializer<Payload> {
1515

16-
public PayloadDeserializer() {
16+
PayloadDeserializer() {
1717
this(null);
1818
}
1919

20-
protected PayloadDeserializer(Class<?> vc) {
20+
private PayloadDeserializer(Class<?> vc) {
2121
super(vc);
2222
}
2323

lib/src/main/java/com/auth0/jwtdecodejava/impl/PayloadImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import static com.auth0.jwtdecodejava.impl.ClaimImpl.extractClaim;
1414
import static com.auth0.jwtdecodejava.impl.Claims.*;
1515

16-
public class PayloadImpl implements Payload {
16+
class PayloadImpl implements Payload {
1717
private Map<String, JsonNode> tree;
1818

19-
public PayloadImpl(Map<String, JsonNode> tree) {
19+
PayloadImpl(Map<String, JsonNode> tree) {
2020
this.tree = tree;
2121
}
2222

lib/src/main/java/com/auth0/jwtdecodejava/interfaces/Claim.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
public interface Claim {
1010

11-
boolean isMissing();
12-
1311
boolean isNull();
1412

1513
/**

lib/src/test/java/com/auth0/jwtdecodejava/JWTDecoderTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.auth0.jwtdecodejava;
22

33
import com.auth0.jwtdecodejava.exceptions.JWTException;
4-
import com.auth0.jwtdecodejava.impl.MissingClaim;
4+
import com.auth0.jwtdecodejava.impl.BaseClaim;
55
import com.auth0.jwtdecodejava.interfaces.Claim;
66
import com.auth0.jwtdecodejava.interfaces.JWT;
77
import com.sun.istack.internal.Nullable;
@@ -120,7 +120,7 @@ public void shouldGetStringAudience() throws Exception {
120120

121121
@Test
122122
public void shouldGetExpirationTime() throws Exception {
123-
JWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJleHAiOiIxNDc2NzI3MDg2In0.XwZztHlQwnAgmnQvrcWXJloLOUaLZGiY0HOXJCKRaks");
123+
JWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NzY3MjcwODZ9.L9dcPHEDQew2u9MkDCORFkfDGcSOsgoPqNY-LUMLEHg");
124124
assertThat(jwt, is(notNullValue()));
125125
assertThat(jwt.getExpiresAt(), is(instanceOf(Date.class)));
126126
long ms = 1476727086L * 1000;
@@ -131,7 +131,7 @@ public void shouldGetExpirationTime() throws Exception {
131131

132132
@Test
133133
public void shouldGetNotBefore() throws Exception {
134-
JWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJuYmYiOiIxNDc2NzI3MDg2In0.pi3Fi3oFiXk5A5AetDdL0hjVx_rt6F5r_YiG6HoCYDw");
134+
JWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJuYmYiOjE0NzY3MjcwODZ9.tkpD3iCPQPVqjnjpDVp2bJMBAgpVCG9ZjlBuMitass0");
135135
assertThat(jwt, is(notNullValue()));
136136
assertThat(jwt.getNotBefore(), is(instanceOf(Date.class)));
137137
long ms = 1476727086L * 1000;
@@ -142,7 +142,7 @@ public void shouldGetNotBefore() throws Exception {
142142

143143
@Test
144144
public void shouldGetIssuedAt() throws Exception {
145-
JWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOiIxNDc2NzI3MDg2In0.u6BxwrO7S0sqDY8-1cUOLzU2uejAJBzQQF8g_o5BAgo");
145+
JWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE0NzY3MjcwODZ9.KPjGoW665E8V5_27Jugab8qSTxLk2cgquhPCBfAP0_w");
146146
assertThat(jwt, is(notNullValue()));
147147
assertThat(jwt.getIssuedAt(), is(instanceOf(Date.class)));
148148
long ms = 1476727086L * 1000;
@@ -202,7 +202,7 @@ public void shouldGetMissingClaimIfClaimDoesNotExist() throws Exception {
202202
JWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.e30.K17vlwhE8FCMShdl1_65jEYqsQqBOVMPUU9IgG-QlTM");
203203
assertThat(jwt, is(notNullValue()));
204204
assertThat(jwt.getClaim("notExisting"), is(notNullValue()));
205-
assertThat(jwt.getClaim("notExisting"), is(instanceOf(MissingClaim.class)));
205+
assertThat(jwt.getClaim("notExisting"), is(instanceOf(BaseClaim.class)));
206206
}
207207

208208
@Test

lib/src/test/java/com/auth0/jwtdecodejava/impl/ClaimImplTest.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,19 @@ public void shouldGetArrayValue() throws Exception {
137137
}
138138

139139
@Test
140-
public void shouldGetEmptyArrayIfNullValue() throws Exception {
140+
public void shouldGetNullArrayIfNullValue() throws Exception {
141141
JsonNode value = mapper.valueToTree(null);
142142
Claim claim = claimFromNode(value);
143143

144-
assertThat(claim.asArray(String.class), is(notNullValue()));
145-
assertThat(claim.asArray(String.class), is(IsArrayWithSize.<String>emptyArray()));
144+
assertThat(claim.asArray(String.class), is(nullValue()));
146145
}
147146

148147
@Test
149-
public void shouldGetEmptyArrayIfNonArrayValue() throws Exception {
148+
public void shouldGetNullArrayIfNonArrayValue() throws Exception {
150149
JsonNode value = mapper.valueToTree(1);
151150
Claim claim = claimFromNode(value);
152151

153-
assertThat(claim.asArray(String.class), is(notNullValue()));
154-
assertThat(claim.asArray(String.class), is(IsArrayWithSize.<String>emptyArray()));
152+
assertThat(claim.asArray(String.class), is(nullValue()));
155153
}
156154

157155
@Test
@@ -182,21 +180,19 @@ public void shouldGetListValue() throws Exception {
182180
}
183181

184182
@Test
185-
public void shouldGetEmptyListIfNullValue() throws Exception {
183+
public void shouldGetNullListIfNullValue() throws Exception {
186184
JsonNode value = mapper.valueToTree(null);
187185
Claim claim = claimFromNode(value);
188186

189-
assertThat(claim.asList(String.class), is(notNullValue()));
190-
assertThat(claim.asList(String.class), is(IsEmptyCollection.emptyCollectionOf(String.class)));
187+
assertThat(claim.asList(String.class), is(nullValue()));
191188
}
192189

193190
@Test
194-
public void shouldGetEmptyListIfNonArrayValue() throws Exception {
191+
public void shouldGetNullListIfNonArrayValue() throws Exception {
195192
JsonNode value = mapper.valueToTree(1);
196193
Claim claim = claimFromNode(value);
197194

198-
assertThat(claim.asList(String.class), is(notNullValue()));
199-
assertThat(claim.asList(String.class), is(IsEmptyCollection.emptyCollectionOf(String.class)));
195+
assertThat(claim.asList(String.class), is(nullValue()));
200196
}
201197

202198
@Test
@@ -209,13 +205,13 @@ public void shouldThrowIfListClassMismatch() throws Exception {
209205
}
210206

211207
@Test
212-
public void shouldReturnMissingClaimWhenParsingNullValue() throws Exception {
208+
public void shouldReturnBaseClaimWhenParsingNullValue() throws Exception {
213209
JsonNode value = mapper.valueToTree(null);
214210
Claim claim = claimFromNode(value);
215211

216212
assertThat(claim, is(notNullValue()));
217-
assertThat(claim, is(instanceOf(MissingClaim.class)));
218-
assertThat(claim.isMissing(), is(true));
213+
assertThat(claim, is(instanceOf(BaseClaim.class)));
214+
assertThat(claim.isNull(), is(true));
219215
}
220216

221217
@Test

0 commit comments

Comments
 (0)