File tree 2 files changed +40
-2
lines changed
main/java/org/springframework/data/mongodb/core/convert
test/java/org/springframework/data/mongodb/core/convert
2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -815,6 +815,11 @@ public Object convertId(@Nullable Object id) {
815
815
*/
816
816
@ Nullable
817
817
public Object convertId (@ Nullable Object id , Class <?> targetType ) {
818
+
819
+ if (!SpecialTypeTreatment .INSTANCE .isConversionCandidate (id )) {
820
+ return id ;
821
+ }
822
+
818
823
return converter .convertId (id , targetType );
819
824
}
820
825
@@ -876,8 +881,8 @@ protected boolean isKeyword(String candidate) {
876
881
private Object applyFieldTargetTypeHintToValue (Field documentField , @ Nullable Object value ) {
877
882
878
883
if (value == null || documentField .getProperty () == null || !documentField .getProperty ().hasExplicitWriteTarget ()
879
- || value instanceof Document || value instanceof DBObject || value instanceof Pattern
880
- || value instanceof BsonRegularExpression ) {
884
+ || value instanceof Document || value instanceof DBObject
885
+ || ! SpecialTypeTreatment . INSTANCE . isConversionCandidate ( value ) ) {
881
886
return value ;
882
887
}
883
888
@@ -1604,4 +1609,22 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
1604
1609
throw new IllegalStateException ("No enclosing property source available" );
1605
1610
}
1606
1611
}
1612
+
1613
+ /*
1614
+ * Types that must not be converted
1615
+ */
1616
+ enum SpecialTypeTreatment {
1617
+
1618
+ INSTANCE ;
1619
+
1620
+ private final Set <Class <?>> types = Set .of (Pattern .class , BsonRegularExpression .class );
1621
+
1622
+ boolean isConversionCandidate (@ Nullable Object value ) {
1623
+ if (value == null ) {
1624
+ return false ;
1625
+ }
1626
+
1627
+ return !types .contains (value .getClass ());
1628
+ }
1629
+ }
1607
1630
}
Original file line number Diff line number Diff line change @@ -1101,6 +1101,21 @@ void shouldRetainRegexPattern() {
1101
1101
assertThat (document .get ("text" )).isInstanceOf (BsonRegularExpression .class );
1102
1102
}
1103
1103
1104
+ @ Test // GH-4674
1105
+ void shouldRetainRegexPatternForIdProperty () {
1106
+
1107
+ org .bson .Document javaRegex = mapper .getMappedObject (query (where ("id" ).regex ("^1234$" )).getQueryObject (),
1108
+ context .getPersistentEntity (WithStringId .class ));
1109
+
1110
+ assertThat (javaRegex .get ("_id" )).isInstanceOf (Pattern .class );
1111
+
1112
+ org .bson .Document bsonRegex = mapper .getMappedObject (
1113
+ query (where ("id" ).regex (new BsonRegularExpression ("^1234$" ))).getQueryObject (),
1114
+ context .getPersistentEntity (WithStringId .class ));
1115
+
1116
+ assertThat (bsonRegex .get ("_id" )).isInstanceOf (BsonRegularExpression .class );
1117
+ }
1118
+
1104
1119
@ Test // DATAMONGO-2339
1105
1120
void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly () {
1106
1121
You can’t perform that action at this time.
0 commit comments