Fixed UUID parsing from String for JSON requests

Due to the way Optional#orElse works, the part inside orElse was run
even when the UUID could be parsed from the given String, leading to
unnecessary work as well as errors in the player UUID query
(That uses names and doesn't look at uuids like the server query)

Fixed by splitting the two execution branches

Affects issues:
- Fixed #1200 (Manually tested)
This commit is contained in:
Rsl1122 2019-10-25 15:12:01 +03:00
parent 64f26f1df6
commit 83293804a5
2 changed files with 35 additions and 5 deletions

View File

@ -24,6 +24,7 @@ import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQuerie
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Optional;
import java.util.UUID;
/**
@ -45,8 +46,11 @@ public class Identifiers {
String serverIndentifier = target.getParameter("server")
.orElseThrow(() -> new BadRequestException("'server' parameter was not defined."));
return UUIDUtility.parseFromString(serverIndentifier)
.orElse(getServerUUIDFromName(serverIndentifier));
Optional<UUID> parsed = UUIDUtility.parseFromString(serverIndentifier);
if (parsed.isPresent()) {
return parsed.get();
}
return getServerUUIDFromName(serverIndentifier);
}
private UUID getServerUUIDFromName(String serverName) throws BadRequestException {
@ -57,10 +61,13 @@ public class Identifiers {
public UUID getPlayerUUID(RequestTarget target) throws BadRequestException {
String playerIdentifier = target.getParameter("player")
.orElseThrow(() -> new BadRequestException("'player' parameter was not defined."));
.orElseThrow(() -> new BadRequestException("'player' parameter was not defined.")).trim();
return UUIDUtility.parseFromString(playerIdentifier)
.orElse(getPlayerUUIDFromName(playerIdentifier));
Optional<UUID> parsed = UUIDUtility.parseFromString(playerIdentifier);
if (parsed.isPresent()) {
return parsed.get();
}
return getPlayerUUIDFromName(playerIdentifier);
}
private UUID getPlayerUUIDFromName(String playerName) throws BadRequestException {

View File

@ -0,0 +1,23 @@
package com.djrapitops.plan.identification;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import utilities.OptionalAssert;
import java.util.UUID;
/**
* Tests for {@link UUIDUtility}.
*
* @author Rsl1122
*/
@RunWith(JUnitPlatform.class)
class UUIDUtilityTest {
@Test
void stringUUIDIsParsed() {
String test = "f3cc3e96-1bc9-35ad-994f-d894e9764b93";
OptionalAssert.equals(UUID.fromString(test), UUIDUtility.parseFromString(test));
}
}