Skip to content

Commit d54f2e4

Browse files
christophstroblmp911de
authored andcommitted
Follow changes in uuid processing.
Closes: #3750 Original pull request: #4334
1 parent cc3b33e commit d54f2e4

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/json/ParameterBindingJsonReader.java

+29-17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Locale;
2828
import java.util.Map;
2929
import java.util.TimeZone;
30+
import java.util.UUID;
3031
import java.util.function.Supplier;
3132
import java.util.regex.Matcher;
3233
import java.util.regex.Pattern;
@@ -289,10 +290,9 @@ public BsonType readBsonType() {
289290
} else if ("DBPointer".equals(value)) {
290291
setCurrentBsonType(BsonType.DB_POINTER);
291292
currentValue = visitDBPointerConstructor();
292-
} else if ("UUID".equals(value) || "GUID".equals(value) || "CSUUID".equals(value) || "CSGUID".equals(value)
293-
|| "JUUID".equals(value) || "JGUID".equals(value) || "PYUUID".equals(value) || "PYGUID".equals(value)) {
293+
} else if ("UUID".equals(value)) {
294294
setCurrentBsonType(BsonType.BINARY);
295-
currentValue = visitUUIDConstructor(value);
295+
currentValue = visitUUIDConstructor();
296296
} else if ("new".equals(value)) {
297297
visitNew();
298298
} else {
@@ -840,9 +840,8 @@ private void visitNew() {
840840
} else if ("DBPointer".equals(value)) {
841841
currentValue = visitDBPointerConstructor();
842842
setCurrentBsonType(BsonType.DB_POINTER);
843-
} else if ("UUID".equals(value) || "GUID".equals(value) || "CSUUID".equals(value) || "CSGUID".equals(value)
844-
|| "JUUID".equals(value) || "JGUID".equals(value) || "PYUUID".equals(value) || "PYGUID".equals(value)) {
845-
currentValue = visitUUIDConstructor(value);
843+
} else if ("UUID".equals(value)) {
844+
currentValue = visitUUIDConstructor();
846845
setCurrentBsonType(BsonType.BINARY);
847846
} else {
848847
throw new JsonParseException("JSON reader expected a type name but found '%s'.", value);
@@ -862,7 +861,13 @@ private void visitExtendedJSON() {
862861
setCurrentBsonType(BsonType.BINARY);
863862
return;
864863
}
865-
} else if ("$regex".equals(value) || "$options".equals(value)) {
864+
}
865+
if ("$uuid".equals(value)) {
866+
currentValue = visitUuidExtendedJson();
867+
setCurrentBsonType(BsonType.BINARY);
868+
return;
869+
}
870+
else if ("$regex".equals(value) || "$options".equals(value)) {
866871
currentValue = visitRegularExpressionExtendedJson(value);
867872
if (currentValue != null) {
868873
setCurrentBsonType(BsonType.REGULAR_EXPRESSION);
@@ -956,16 +961,12 @@ private BsonBinary visitBinDataConstructor() {
956961
return new BsonBinary(subTypeToken.getValue(Integer.class).byteValue(), bytes);
957962
}
958963

959-
private BsonBinary visitUUIDConstructor(final String uuidConstructorName) {
960-
verifyToken(JsonTokenType.LEFT_PAREN);
961-
String hexString = readStringFromExtendedJson().replaceAll("\\{", "").replaceAll("}", "").replaceAll("-", "");
962-
verifyToken(JsonTokenType.RIGHT_PAREN);
963-
byte[] bytes = decodeHex(hexString);
964-
BsonBinarySubType subType = BsonBinarySubType.UUID_STANDARD;
965-
if (!"UUID".equals(uuidConstructorName) || !"GUID".equals(uuidConstructorName)) {
966-
subType = BsonBinarySubType.UUID_LEGACY;
967-
}
968-
return new BsonBinary(subType, bytes);
964+
private BsonBinary visitUUIDConstructor() {
965+
this.verifyToken(JsonTokenType.LEFT_PAREN);
966+
String hexString = this.readStringFromExtendedJson().replace("-", "");
967+
968+
this.verifyToken(JsonTokenType.RIGHT_PAREN);
969+
return new BsonBinary(BsonBinarySubType.UUID_STANDARD, decodeHex(hexString));
969970
}
970971

971972
private BsonRegularExpression visitRegularExpressionConstructor() {
@@ -1484,6 +1485,17 @@ private int readIntFromExtendedJson() {
14841485
return value;
14851486
}
14861487

1488+
private BsonBinary visitUuidExtendedJson() {
1489+
verifyToken(JsonTokenType.COLON);
1490+
String hexString = this.readStringFromExtendedJson().replace("-", "");
1491+
verifyToken(JsonTokenType.END_OBJECT);
1492+
try {
1493+
return new BsonBinary(BsonBinarySubType.UUID_STANDARD, decodeHex(hexString));
1494+
} catch (IllegalArgumentException e) {
1495+
throw new JsonParseException(e);
1496+
}
1497+
}
1498+
14871499
private void visitJavaScriptExtendedJson() {
14881500
verifyToken(JsonTokenType.COLON);
14891501
String code = readStringFromExtendedJson();

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/util/json/ParameterBindingJsonReaderUnitTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.List;
2828
import java.util.UUID;
2929

30+
import org.bson.BsonBinary;
31+
import org.bson.BsonBinarySubType;
3032
import org.bson.Document;
3133
import org.bson.codecs.DecoderContext;
3234
import org.junit.jupiter.api.Test;
@@ -547,6 +549,23 @@ void retainsSpelArgumentTypeViaParameterPlaceholderWhenValueContainsDoubleQuotes
547549
assertThat(target.get("arg0")).isEqualTo(source);
548550
}
549551

552+
@Test // GH-3750
553+
void shouldParseUUIDasStandardRepresentation() {
554+
555+
String json = "{ 'value' : UUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\") }";
556+
557+
BsonBinary value = parse(json).get("value", BsonBinary.class);
558+
assertThat(value.getType()).isEqualTo(BsonBinarySubType.UUID_STANDARD.getValue());
559+
}
560+
561+
@Test // GH-3750
562+
public void shouldParse$uuidAsStandardRepresentation() {
563+
564+
String json = "{ 'value' : { '$uuid' : \"73ff-d26444b-34c6-990e8e-7d1dfc035d4\" } } }";
565+
BsonBinary value = parse(json).get("value", BsonBinary.class);
566+
assertThat(value.getType()).isEqualTo(BsonBinarySubType.UUID_STANDARD.getValue());
567+
}
568+
550569
private static Document parse(String json, Object... args) {
551570

552571
ParameterBindingJsonReader reader = new ParameterBindingJsonReader(json, args);

0 commit comments

Comments
 (0)