Skip to content

Commit

Permalink
🐛 Fix DocumentBuilder.put(String key, Instant value)
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Oct 18, 2023
1 parent d96ee5e commit 9bcde21
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
64 changes: 32 additions & 32 deletions commons/src/main/java/org/restheart/utils/BsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.google.common.collect.Sets;
import com.mongodb.MongoClientSettings;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -60,7 +61,6 @@
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.json.Converter;
import org.bson.json.JsonMode;
import org.bson.json.JsonParseException;
import org.bson.json.JsonReader;
Expand Down Expand Up @@ -355,7 +355,7 @@ private static List<Optional<BsonValue>> _getPropsFromPath(BsonValue json, Strin
List<Optional<BsonValue>> nested;

switch (pathToken) {
case DOLLAR:
case DOLLAR -> {
if (!(json.isDocument())) {
throw new IllegalArgumentException("wrong path " + Arrays.toString(pathTokens) + " at token " + pathToken + "; it should be an object but found " + json.toString());
}
Expand All @@ -365,7 +365,8 @@ private static List<Optional<BsonValue>> _getPropsFromPath(BsonValue json, Strin
}

return _getPropsFromPath(json, subpath(pathTokens), totalTokensLength);
case "*":
}
case "*" -> {
if (!(json.isDocument())) {
return null;
} else {
Expand All @@ -384,7 +385,8 @@ private static List<Optional<BsonValue>> _getPropsFromPath(BsonValue json, Strin

return ret;
}
case "[*]":
}
case "[*]" -> {
if (!(json.isArray())) {
if (json.isDocument()) {
// this might be the case of PATCHING an element array using the dot notation
Expand All @@ -393,14 +395,14 @@ private static List<Optional<BsonValue>> _getPropsFromPath(BsonValue json, Strin
// in any case, it might also be the object { "object": { "array": {"2": xxx }}}

var allNumbericKeys = json.asDocument().keySet()
.stream().allMatch(k -> {
try {
Integer.parseInt(k);
return true;
} catch (NumberFormatException nfe) {
return false;
}
});
.stream().allMatch(k -> {
try {
Integer.valueOf(k);
return true;
} catch (NumberFormatException nfe) {
return false;
}
});

if (allNumbericKeys) {
var ret = new ArrayList<Optional<BsonValue>>();
Expand Down Expand Up @@ -439,7 +441,8 @@ private static List<Optional<BsonValue>> _getPropsFromPath(BsonValue json, Strin

return ret;
}
default:
}
default -> {
if (json.isArray()) {
throw new IllegalArgumentException("wrong path " + pathFromTokens(pathTokens) + " at token " + pathToken + "; it should be '[*]'");
} else if (json.isDocument()) {
Expand All @@ -451,6 +454,7 @@ private static List<Optional<BsonValue>> _getPropsFromPath(BsonValue json, Strin
} else {
return null;
}
}
}
}

Expand Down Expand Up @@ -490,24 +494,23 @@ public static boolean isAncestorPath(final String left, final String right) {
var rt = rightPathTokens[cont];

switch (lt) {
case "*":
break;
case "[*]":
case "*" -> {
}
case "[*]" -> {
try {
Integer.parseInt(rt);
break;
} catch (NumberFormatException nfe) {
Integer.valueOf(rt);
}catch (NumberFormatException nfe) {
ret = false;
break outerloop;
}
default:
}
default -> {
ret = rt.equals(lt);

if (!ret) {
break outerloop;
} else {
break;
}
}
}
}
}
Expand Down Expand Up @@ -559,7 +562,7 @@ private static String[] subpath(String[] pathTokens) {
subpath.add(pathTokens[cont]);
}

return subpath.toArray(new String[subpath.size()]);
return subpath.toArray(String[]::new);
}

/**
Expand Down Expand Up @@ -686,12 +689,9 @@ public static String toJson(BsonValue bson, JsonMode mode) {
.build()
: JsonWriterSettings.builder()
.indent(false)
.dateTimeConverter(new Converter<Long>() {
@Override
public void convert(Long t, StrictJsonWriter writer) {
writer.writeRaw("{\"$date\": " + t + " }");
}
}).build();
.dateTimeConverter((Long t, StrictJsonWriter writer) -> {
writer.writeRaw("{\"$date\": " + t + " }");
}).build();

if (bson.isDocument()) {
return minify(bson.asDocument().toJson(settings));
Expand Down Expand Up @@ -987,7 +987,7 @@ public static ArrayBuilder array() {
}

/**
* @param doc the BsonArray to wrap
* @param array the BsonArray to wrap
*
* @return a ArrayBuilder to help building BsonArray
*/
Expand Down Expand Up @@ -1150,7 +1150,7 @@ public DocumentBuilder put(String key, Instant value) {
if (value == null) {
putNull(key);
} else {
doc.put(key, new BsonDateTime(value.getEpochSecond()));
doc.put(key, new BsonDateTime(value.getEpochSecond()*1000));
}

return this;
Expand Down Expand Up @@ -1297,7 +1297,7 @@ public ArrayBuilder add(Boolean... values) {
}

public ArrayBuilder add(Instant... values) {
Arrays.stream(values).map(v -> v == null ? BsonNull.VALUE : new BsonDateTime(v.getEpochSecond())).forEach(array::add);
Arrays.stream(values).map(v -> v == null ? BsonNull.VALUE : new BsonDateTime(v.getEpochSecond()*1000)).forEach(array::add);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

package org.restheart.graphql.datafetchers;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand Down

0 comments on commit 9bcde21

Please sign in to comment.