Skip to content

Commit db33970

Browse files
authored
Custom empty claims
Closes #858. Custom claims can now be empty again (which was the behavior for <= 0.11.5).
1 parent 59c9df1 commit db33970

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

api/src/main/java/io/jsonwebtoken/lang/Objects.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static boolean isArray(Object obj) {
9494
* Returns {@code true} if the specified argument:
9595
* <ol>
9696
* <li>is {@code null}, or</li>
97-
* <li>is a String and {@link Strings#hasText(String)} is {@code false}, or</li>
97+
* <li>is a CharSequence and {@link Strings#hasText(CharSequence)} is {@code false}, or</li>
9898
* <li>is a Collection or Map with zero size, or</li>
9999
* <li>is an empty array</li>
100100
* </ol>
@@ -106,7 +106,7 @@ public static boolean isArray(Object obj) {
106106
*/
107107
public static boolean isEmpty(Object v) {
108108
return v == null ||
109-
(v instanceof String && !Strings.hasText((String) v)) ||
109+
(v instanceof CharSequence && !Strings.hasText((CharSequence) v)) ||
110110
(v instanceof Collection && Collections.isEmpty((Collection<?>) v)) ||
111111
(v instanceof Map && Collections.isEmpty((Map<?, ?>) v)) ||
112112
(v.getClass().isArray() && Array.getLength(v) == 0);

impl/src/main/java/io/jsonwebtoken/impl/ParameterMap.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,6 @@ public Object get(Object o) {
124124
return values.get(o);
125125
}
126126

127-
private static Object clean(Object o) {
128-
if (o instanceof String) {
129-
o = Strings.clean((String) o);
130-
}
131-
return o;
132-
}
133-
134127
/**
135128
* Convenience method to put a value for an idiomatic param.
136129
*
@@ -143,7 +136,7 @@ protected final <T> Object put(Parameter<T> param, Object value) {
143136
assertMutable();
144137
Assert.notNull(param, "Parameter cannot be null.");
145138
Assert.hasText(param.getId(), "Parameter id cannot be null or empty.");
146-
return apply(param, clean(value));
139+
return apply(param, value);
147140
}
148141

149142
@Override
@@ -156,12 +149,12 @@ public final Object put(String name, Object value) {
156149
return put(param, value);
157150
} else {
158151
// non-standard or custom property, just apply directly:
159-
return nullSafePut(name, clean(value));
152+
return nullSafePut(name, value);
160153
}
161154
}
162155

163156
private Object nullSafePut(String name, Object value) {
164-
if (Objects.isEmpty(value)) {
157+
if (value == null) {
165158
return remove(name);
166159
} else {
167160
this.idiomaticValues.put(name, value);
@@ -199,9 +192,8 @@ private <T> Object apply(Parameter<T> param, Object rawValue) {
199192
String msg = sb.toString();
200193
throw new IllegalArgumentException(msg, e);
201194
}
202-
Object retval = nullSafePut(id, canonicalValue);
203195
this.idiomaticValues.put(id, idiomaticValue);
204-
return retval;
196+
return this.values.put(id, canonicalValue);
205197
}
206198

207199
@Override

impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtBuilderTest.groovy

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,6 @@ class DefaultJwtBuilderTest {
219219
assertEquals b.claimsBuilder.foo, 'bar'
220220
}
221221

222-
@Test
223-
void testClaimEmptyString() {
224-
String value = ' '
225-
builder.claim('foo', value)
226-
assertTrue builder.claimsBuilder.isEmpty() // shouldn't populate claims instance
227-
}
228-
229222
@Test
230223
void testExistingClaimsAndSetClaim() {
231224
Claims c = Jwts.claims().add('foo', 'bar').build()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright © 2023 jsonwebtoken.io
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.jsonwebtoken.issues
17+
18+
import io.jsonwebtoken.Jwts
19+
import org.junit.Test
20+
21+
import static org.junit.Assert.assertEquals
22+
23+
class Issue858Test {
24+
25+
@Test
26+
void testEmptyAndNullEntries() {
27+
def jwt = Jwts.builder()
28+
.subject('Joe')
29+
.claim('foo', '') // empty allowed
30+
.claim('list', []) // empty allowed
31+
.claim('map', [:]) // empty map allowed
32+
.claim('another', null) // null not allowed (same behavior since <= 0.11.5), won't be added
33+
.compact()
34+
35+
def claims = Jwts.parser().unsecured().build().parseUnsecuredClaims(jwt).getPayload()
36+
assertEquals 4, claims.size()
37+
assertEquals 'Joe', claims.getSubject()
38+
assertEquals '', claims.get('foo')
39+
assertEquals([], claims.get('list'))
40+
assertEquals([:], claims.get('map'))
41+
}
42+
}

0 commit comments

Comments
 (0)