[java-firestore] UserDataConverter encodes a pre-epoch Date with negative nanos, so the write is refused
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 java-firestore/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UserDataConverter.java 开始,检查 issue 中链接的 encodeValue 转换,并将其与 com.google.cloud.Timestamp.of(Date) 进行比较。为 Unix 纪元之前的日期(例如 new Date(-1500))添加或找到一个回归测试,并验证编码后的 timestamp 的 nanos 非负。完成标准是该日期可以写入,且不会出现 emulator 的无效 timestamp 错误;issue 未指定测试文件。
由索引模型根据 Issue 内容生成。
描述
Issue Details
UserDataConverter.encodeValue converts a java.util.Date into a Timestamp proto by truncating the milliseconds toward zero for seconds and putting the remainder in nanos:
For a date before the epoch that is not a whole second, the remainder is negative, so the proto carries a negative nanos, which google.protobuf.Timestamp does not allow (nanos must be in [0, 999999999]). new Date(-1500) is sent as seconds: -1, nanos: -500000000, and new Date(-1) as nanos: -1000000. The emulator refuses the write with INVALID_ARGUMENT: Timestamp nanos exceeds limit for field., so a valid Date cannot be written. The same conversion serves every value the library encodes, so a Date field of a POJO and a query filter or cursor on a Date should be affected the same way (not tried).
com.google.cloud.Timestamp.of(Date) already does this conversion correctly (it borrows a second when the remainder is negative), so encodeValue could use it, or Math.floorDiv and Math.floorMod.
Found while building an Apache Flink sink on google-cloud-firestore (flink-gcp/flink-connector-gcp).
Environment
- OS Type and Version: macOS 26 (arm64)
- Java Version and JDK Vendor: Eclipse Temurin 17.0.20
- Firestore emulator:
gcr.io/google.com/cloudsdktool/google-cloud-cli:588.0.0-emulators(Native mode, Standard edition); 587.0.0 answers the same
The refusal was measured on the emulator only, not against a production database.
Dependencies
- Libraries-Bom: 26.90.0
- google-cloud-firestore 3.49.0
- Gax: 2.87.0
- Auth: 1.54.0
The affected code is identical in 3.49.0 and on main.
Reproducer
Run with FIRESTORE_EMULATOR_HOST set.
package repro;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import java.util.Date;
import java.util.Map;
public class PreEpochDate {
public static void main(String[] args) throws Exception {
Firestore db = FirestoreOptions.newBuilder().setProjectId("demo").build().getService();
for (long millis : new long[] {-1000, -1500, -1}) {
DocumentReference doc = db.collection("dates").document("d" + millis);
try {
doc.set(Map.of("t", new Date(millis))).get();
System.out.println(millis + " ms: written, read back as " + doc.get().get().getTimestamp("t"));
} catch (Exception e) {
System.out.println(millis + " ms: " + e.getCause());
}
}
System.exit(0);
}
}
Logs and Stack Trace
-1000 ms: written, read back as 1969-12-31T23:59:59Z
-1500 ms: com.google.api.gax.rpc.InvalidArgumentException: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Timestamp nanos exceeds limit for field.
-1 ms: com.google.api.gax.rpc.InvalidArgumentException: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Timestamp nanos exceeds limit for field.
Behavior
Deterministic: any Date before the epoch whose millisecond value is not a multiple of 1000.
- 主要语言
- Java
- 星标
- 2.1k
- 派生
- 1.2k
- 平均合并
- 2 天 21 小时
- 30 天内合并 PR
- 139
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
googleapis/google-cloud-java 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
googleapis/google-cloud-java#14636 ·
维护者通常 1 天内回复
-
[java-firestore] UserDataConverter throws IndexOutOfBoundsException on an empty __binary__ value未关闭
难度 2/5 1-3 小时 新手友好度 74/100
googleapis/google-cloud-java#14634 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
googleapis/google-cloud-java#14579 ·
维护者通常 1 天内回复
-
[java-firestore] CustomClassMapper.convertBigDecimal calls .abs() on Double, unintentionally flipping negative values to positive可能已有人在做 @wu-hui 于 14 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
googleapis/google-cloud-java#14533 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Github CI failing when trying to install xmllint可能已有人在做 @blakeli0 于 50 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
googleapis/google-cloud-java#14132 · 1 条评论 ·
维护者通常 1 天内回复
查看 googleapis/google-cloud-java 的全部 Issue
相似的 Issue
-
status: waiting-for-triage
难度 2/5 1-3 小时 新手友好度 72/100
-
bug
难度 2/5 1-3 小时 新手友好度 62/100
-
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
AOT compilation fails for derived delete queries returning Set可能已有人在做 @ryujungkyun 今天认领。 未关闭status: waiting-for-triage
难度 2/5 1-3 小时 新手友好度 72/100
spring-projects/spring-data-jpa#4387 ·
维护者通常 3 天内回复
-
:ml/Transform >bug Team:ML
难度 2/5 半天 新手友好度 64/100
elastic/elasticsearch#161960 · 2 条评论 · 1 个 reaction ·
维护者通常 1 天内回复