Skip to content

Commit 00c18bd

Browse files
committed
Fixes, disallow paths
1 parent 742387f commit 00c18bd

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

driver-core/src/main/com/mongodb/client/model/expressions/DocumentExpression.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.client.model.expressions;
1818

1919
import org.bson.conversions.Bson;
20+
import org.bson.types.Decimal128;
2021

2122
import java.time.Instant;
2223

@@ -46,7 +47,11 @@ default BooleanExpression getBoolean(final String fieldName, final boolean other
4647

4748
NumberExpression getNumber(String fieldName, NumberExpression other);
4849

49-
default NumberExpression getNumber(final String fieldName, final Number other) {
50+
default NumberExpression getNumber(final String fieldName, final double other) {
51+
return getNumber(fieldName, Expressions.numberToExpression(other));
52+
}
53+
54+
default NumberExpression getNumber(final String fieldName, final Decimal128 other) {
5055
return getNumber(fieldName, Expressions.numberToExpression(other));
5156
}
5257

driver-core/src/main/com/mongodb/client/model/expressions/MqlExpression.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,15 @@ public <R extends Expression> R cond(final R left, final R right) {
139139

140140
/** @see DocumentExpression */
141141

142-
private Function<CodecRegistry, AstPlaceholder> getFieldInternal(final String fieldBoolean) {
143-
return (cr) -> astDoc("$getField", new BsonDocument()
144-
.append("input", this.fn.apply(cr).bsonValue)
145-
.append("field", new BsonString(fieldBoolean)));
142+
private Function<CodecRegistry, AstPlaceholder> getFieldInternal(final String fieldName) {
143+
return (cr) -> {
144+
BsonValue value = fieldName.startsWith("$")
145+
? new BsonDocument("$literal", new BsonString(fieldName))
146+
: new BsonString(fieldName);
147+
return astDoc("$getField", new BsonDocument()
148+
.append("input", this.fn.apply(cr).bsonValue)
149+
.append("field", value));
150+
};
146151
}
147152

148153
@Override
@@ -216,7 +221,7 @@ public <R extends Expression> ArrayExpression<R> getArray(final String fieldName
216221
}
217222

218223
@Override
219-
public <T1 extends Expression> ArrayExpression<T1> getArray(final String fieldName, final ArrayExpression<? extends T1> other) {
224+
public <R extends Expression> ArrayExpression<R> getArray(final String fieldName, final ArrayExpression<? extends R> other) {
220225
return getArray(fieldName).isArrayOr(other);
221226
}
222227

driver-core/src/test/functional/com/mongodb/client/model/expressions/DocumentExpressionsFunctionalTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ public void getFieldTest() {
9696
.getDate("a").month(of("UTC")));
9797
assertExpression(3, ofDoc("{a: [3, 2]}").getArray("a").first());
9898
assertExpression(2, ofDoc("{a: {b: 2}}").getDocument("a").getInteger("b"));
99+
100+
// field names, not paths
101+
DocumentExpression doc = ofDoc("{a: {b: 2}, 'a.b': 3, 'a$b': 4, '$a.b': 5}");
102+
assertExpression(2, doc.getDocument("a").getInteger("b"));
103+
assertExpression(3, doc.getInteger("a.b"));
104+
assertExpression(4, doc.getInteger("a$b"));
105+
assertExpression(5,
106+
doc.getInteger("$a.b"),
107+
"{'$getField': {'input': {'$literal': {'a': {'b': 2}, 'a.b': 3, 'a$b': 4, '$a.b': 5}}, "
108+
+ "'field': {'$literal': '$a.b'}}}");
99109
}
100110

101111
@Test

0 commit comments

Comments
 (0)