Skip to content

Commit

Permalink
getClaimAsBoolean() should not be falsy
Browse files Browse the repository at this point in the history
Closes spring-projectsgh-10148

Remove comment as per PR review
  • Loading branch information
ahmedmq committed Sep 5, 2021
1 parent 989c141 commit af8c72f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -96,8 +96,14 @@ default String getClaimAsString(String claim) {
* @return the claim value or {@code null} if it does not exist
*/
default Boolean getClaimAsBoolean(String claim) {
return !hasClaim(claim) ? null
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), Boolean.class);
if (!hasClaim(claim)) {
return null;
}
Object claimValue = getClaims().get(claim);
Boolean convertedValue = ClaimConversionService.getSharedInstance().convert(claimValue, Boolean.class);
Assert.isTrue(convertedValue != null,
() -> "Unable to convert claim '" + claim + "' of type '" + claimValue.getClass() + "' to Boolean.");
return convertedValue;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,7 +41,11 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
if (source instanceof Boolean) {
return source;
}
return Boolean.valueOf(source.toString());
if (source instanceof String) {
return Boolean.valueOf((String) source);
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatObject;

/**
Expand Down Expand Up @@ -135,4 +136,13 @@ public void getClaimWhenValueIsNotConvertedThenThrowClassCastException() {
assertThatObject(this.claimAccessor.getClaim(claimName)).isNotInstanceOf(Boolean.class);
}

// gh-10148
@Test
public void getClaimAsBooleanThrowsIllegalArgumentForNonBooleanType() {
String claimName = "boolean";
this.claims.put(claimName, new HashMap<>());

assertThatIllegalArgumentException().isThrownBy(() -> this.claimAccessor.getClaimAsBoolean(claimName));
}

}

0 comments on commit af8c72f

Please sign in to comment.