From d2e60eb5acd87dbf4263ab5f9da7c1a9f7863e5a Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 5 Apr 2020 16:42:09 +0100 Subject: [PATCH] Attempt to fix issue converting MongoDB document _id to UUID (#2110) --- .../implementation/mongodb/MongoStorage.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java index a6bb1bfb8..fe73c3af4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java @@ -250,7 +250,7 @@ public class MongoStorage implements StorageImplementation { while (cursor.hasNext()) { Document d = cursor.next(); - UUID uuid = d.get("_id", UUID.class); + UUID uuid = getDocumentId(d); Set nodes = new HashSet<>(nodesFromDoc(d)); Set results = nodes.stream() .map(bulkUpdate::apply) @@ -354,8 +354,7 @@ public class MongoStorage implements StorageImplementation { MongoCollection c = this.database.getCollection(this.prefix + "users"); try (MongoCursor cursor = c.find().iterator()) { while (cursor.hasNext()) { - Document d = cursor.next(); - uuids.add(d.get("_id", UUID.class)); + uuids.add(getDocumentId(cursor.next())); } } return uuids; @@ -368,7 +367,7 @@ public class MongoStorage implements StorageImplementation { try (MongoCursor cursor = c.find().iterator()) { while (cursor.hasNext()) { Document d = cursor.next(); - UUID holder = d.get("_id", UUID.class); + UUID holder = getDocumentId(d); Set nodes = new HashSet<>(nodesFromDoc(d)); for (Node e : nodes) { @@ -601,8 +600,8 @@ public class MongoStorage implements StorageImplementation { Set conflicting = new HashSet<>(); try (MongoCursor cursor = c.find(new Document("name", username)).iterator()) { - if (cursor.hasNext()) { - conflicting.add(cursor.next().get("_id", UUID.class)); + while (cursor.hasNext()) { + conflicting.add(getDocumentId(cursor.next())); } } conflicting.remove(uniqueId); @@ -621,7 +620,7 @@ public class MongoStorage implements StorageImplementation { MongoCollection c = this.database.getCollection(this.prefix + "uuid"); Document doc = c.find(new Document("name", username.toLowerCase())).first(); if (doc != null) { - return doc.get("_id", UUID.class); + return getDocumentId(doc); } return null; } @@ -636,6 +635,17 @@ public class MongoStorage implements StorageImplementation { return null; } + private static UUID getDocumentId(Document doc) { + Object id = doc.get("_id"); + if (id instanceof UUID) { + return (UUID) id; + } else if (id instanceof String) { + return UUID.fromString((String) id); + } else { + throw new IllegalArgumentException("Unknown id type: " + id.getClass().getName()); + } + } + private static Document userToDoc(User user) { List nodes = user.normalData().immutable().values().stream() .map(MongoStorage::nodeToDoc)