Skip to content

Commit

Permalink
Merge pull request #25 from penguineer/fix-displayname-npe
Browse files Browse the repository at this point in the history
Fix NPE when a display name was not set
  • Loading branch information
penguineer authored Aug 2, 2024
2 parents 98b7982 + 27f6284 commit 096bc59
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private UserDTO createUser(OAuth2User origin) {
final String origin_id = Optional.ofNullable(origin.getAttribute("id"))
.map(Object::toString)
.orElseThrow(() -> new IllegalArgumentException("id attribute is missing"));
final String origin_name = origin.getAttribute("name");
final String origin_name = determineDisplayName(origin);
final String origin_email = origin.getAttribute("email");
final String origin_avatar_url = origin.getAttribute("avatar_url");

Expand All @@ -92,7 +92,7 @@ private UserDTO createUser(OAuth2User origin) {
}

private UserEntity updateUserFromOIDC(OAuth2User origin, UserEntity user) {
final String origin_name = origin.getAttribute("name");
final String origin_name = determineDisplayName(origin);
final String origin_email = origin.getAttribute("email");
final String origin_avatar_url = origin.getAttribute("avatar_url");

Expand All @@ -105,4 +105,11 @@ private UserEntity updateUserFromOIDC(OAuth2User origin, UserEntity user) {

return userEntityService.save(user);
}

private String determineDisplayName(OAuth2User origin) {
return Optional.ofNullable(origin.getAttribute("name"))
.or(() -> Optional.ofNullable(origin.getAttribute("login")))
.map(String::valueOf)
.orElseThrow(() -> new IllegalArgumentException("Name and login attribute are missing"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public record UserDTO(
@JsonProperty("email") String email,
@JsonProperty("avatar_url") URI avatarUrl) {

public UserDTO {
Objects.requireNonNull(displayName, "displayName must not be null");
}

@Override
public String toString() {
return displayName + "(" + id + ")";
Expand Down

0 comments on commit 096bc59

Please sign in to comment.