From 9bcde21bada094e4e49144c9f73b8ce3cfaf0f25 Mon Sep 17 00:00:00 2001 From: Andrea Di Cesare Date: Wed, 18 Oct 2023 17:54:23 +0200 Subject: [PATCH] :bug: Fix DocumentBuilder.put(String key, Instant value) --- .../java/org/restheart/utils/BsonUtils.java | 64 +++++++++---------- .../GQLAggregationDataFetcher.java | 1 - 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/commons/src/main/java/org/restheart/utils/BsonUtils.java b/commons/src/main/java/org/restheart/utils/BsonUtils.java index c676cb7c4..4e8f857f6 100644 --- a/commons/src/main/java/org/restheart/utils/BsonUtils.java +++ b/commons/src/main/java/org/restheart/utils/BsonUtils.java @@ -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; @@ -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; @@ -355,7 +355,7 @@ private static List> _getPropsFromPath(BsonValue json, Strin List> 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()); } @@ -365,7 +365,8 @@ private static List> _getPropsFromPath(BsonValue json, Strin } return _getPropsFromPath(json, subpath(pathTokens), totalTokensLength); - case "*": + } + case "*" -> { if (!(json.isDocument())) { return null; } else { @@ -384,7 +385,8 @@ private static List> _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 @@ -393,14 +395,14 @@ private static List> _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>(); @@ -439,7 +441,8 @@ private static List> _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()) { @@ -451,6 +454,7 @@ private static List> _getPropsFromPath(BsonValue json, Strin } else { return null; } + } } } @@ -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; } + } } } } @@ -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); } /** @@ -686,12 +689,9 @@ public static String toJson(BsonValue bson, JsonMode mode) { .build() : JsonWriterSettings.builder() .indent(false) - .dateTimeConverter(new Converter() { - @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)); @@ -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 */ @@ -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; @@ -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; } diff --git a/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLAggregationDataFetcher.java b/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLAggregationDataFetcher.java index b0235a886..bb5e59888 100644 --- a/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLAggregationDataFetcher.java +++ b/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLAggregationDataFetcher.java @@ -21,7 +21,6 @@ package org.restheart.graphql.datafetchers; -import java.util.ArrayList; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors;