From 5225ab878eb2e7d47b57a9930332e57dcf0df1ad Mon Sep 17 00:00:00 2001 From: dyma solovei Date: Tue, 1 Dec 2026 14:41:19 +0100 Subject: [PATCH 1/2] fix: serialize time as RFC3339 string --- .../collections/data/InsertManyRequest.java | 7 ++--- .../v1/api/collections/query/Filter.java | 5 ++-- .../client6/v1/internal/DateUtil.java | 22 +++++++++++++++- .../client6/v1/internal/json/JSONTest.java | 26 +++++++++++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/weaviate/client6/v1/api/collections/data/InsertManyRequest.java b/src/main/java/io/weaviate/client6/v1/api/collections/data/InsertManyRequest.java index 1d2deda00..9e7052d8e 100644 --- a/src/main/java/io/weaviate/client6/v1/api/collections/data/InsertManyRequest.java +++ b/src/main/java/io/weaviate/client6/v1/api/collections/data/InsertManyRequest.java @@ -14,6 +14,7 @@ import io.weaviate.client6.v1.api.collections.GeoCoordinates; import io.weaviate.client6.v1.api.collections.PhoneNumber; import io.weaviate.client6.v1.api.collections.WeaviateObject; +import io.weaviate.client6.v1.internal.DateUtil; import io.weaviate.client6.v1.internal.MapUtil; import io.weaviate.client6.v1.internal.grpc.ByteStringUtil; import io.weaviate.client6.v1.internal.grpc.Rpc; @@ -175,7 +176,7 @@ private static com.google.protobuf.Value marshalValue(Object value) { } else if (value instanceof UUID v) { protoValue.setStringValue(v.toString()); } else if (value instanceof OffsetDateTime v) { - protoValue.setStringValue(v.toString()); + protoValue.setStringValue(DateUtil.toRFC3339(v)); } else if (value instanceof Boolean v) { protoValue.setBoolValue(v.booleanValue()); } else if (value instanceof Number v) { @@ -208,7 +209,7 @@ private static com.google.protobuf.Value marshalValue(Object value) { } else if (listValue instanceof UUID lv) { protoListValue.setStringValue(lv.toString()); } else if (listValue instanceof OffsetDateTime lv) { - protoListValue.setStringValue(lv.toString()); + protoListValue.setStringValue(DateUtil.toRFC3339(lv)); } else if (listValue instanceof Boolean lv) { protoListValue.setBoolValue(lv); } else if (listValue instanceof Number lv) { @@ -238,7 +239,7 @@ private static com.google.protobuf.Value marshalValue(Object value) { .map(lv -> com.google.protobuf.Value.newBuilder().setStringValue(lv.toString()).build()).toList(); } else if (value instanceof OffsetDateTime[] v) { values = Arrays.stream(v) - .map(lv -> com.google.protobuf.Value.newBuilder().setStringValue(lv.toString()).build()).toList(); + .map(lv -> com.google.protobuf.Value.newBuilder().setStringValue(DateUtil.toRFC3339(lv)).build()).toList(); } else if (value instanceof Boolean[] v) { values = Arrays.stream(v) .map(lv -> com.google.protobuf.Value.newBuilder().setBoolValue(lv).build()).toList(); diff --git a/src/main/java/io/weaviate/client6/v1/api/collections/query/Filter.java b/src/main/java/io/weaviate/client6/v1/api/collections/query/Filter.java index 6a2dc38a0..cb3818f0b 100644 --- a/src/main/java/io/weaviate/client6/v1/api/collections/query/Filter.java +++ b/src/main/java/io/weaviate/client6/v1/api/collections/query/Filter.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.List; +import io.weaviate.client6.v1.internal.DateUtil; import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBase; import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBase.Filters; @@ -908,7 +909,7 @@ private DateOperand(OffsetDateTime value) { @Override public void appendTo(WeaviateProtoBase.Filters.Builder filter) { - filter.setValueText(value.toString()); + filter.setValueText(DateUtil.toRFC3339(value)); } @Override @@ -930,7 +931,7 @@ private DateArrayOperand(OffsetDateTime... values) { } private List formatted() { - return values.stream().map(OffsetDateTime::toString).toList(); + return values.stream().map(DateUtil::toRFC3339).toList(); } @Override diff --git a/src/main/java/io/weaviate/client6/v1/internal/DateUtil.java b/src/main/java/io/weaviate/client6/v1/internal/DateUtil.java index b103e05e7..5a4882402 100644 --- a/src/main/java/io/weaviate/client6/v1/internal/DateUtil.java +++ b/src/main/java/io/weaviate/client6/v1/internal/DateUtil.java @@ -2,6 +2,9 @@ import java.io.IOException; import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.temporal.ChronoField; import com.google.gson.Gson; import com.google.gson.TypeAdapter; @@ -15,11 +18,28 @@ public final class DateUtil { private DateUtil() { } + private static final DateTimeFormatter RFC3339 = new DateTimeFormatterBuilder() + .append(DateTimeFormatter.ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(ChronoField.HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(ChronoField.MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(ChronoField.SECOND_OF_MINUTE, 2) + .appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true) + .appendOffsetId() + .toFormatter(); + /** Convert ISO8601-formatted time string to {@link OffsetDateTime}. */ public static OffsetDateTime fromISO8601(String iso8601) { return OffsetDateTime.parse(iso8601); } + /** Format {@link OffsetDateTime} as RFC3339 timestamp. */ + public static String toRFC3339(OffsetDateTime odt) { + return odt.format(RFC3339); + } + public static enum CustomTypeAdapterFactory implements TypeAdapterFactory { INSTANCE; @@ -34,7 +54,7 @@ public TypeAdapter create(Gson gson, TypeToken type) { @Override public void write(JsonWriter out, OffsetDateTime value) throws IOException { - out.value(value.toString()); + out.value(toRFC3339(value)); } @Override diff --git a/src/test/java/io/weaviate/client6/v1/internal/json/JSONTest.java b/src/test/java/io/weaviate/client6/v1/internal/json/JSONTest.java index b0eba8cdf..fb63831d2 100644 --- a/src/test/java/io/weaviate/client6/v1/internal/json/JSONTest.java +++ b/src/test/java/io/weaviate/client6/v1/internal/json/JSONTest.java @@ -1,5 +1,6 @@ package io.weaviate.client6.v1.internal.json; +import java.time.OffsetDateTime; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -2274,6 +2275,31 @@ public static Object[][] testCases() { io.weaviate.client6.v1.api.rbac.groups.UserType.DB_ENV_USER, "\"db_env_user\"" }, + { + OffsetDateTime.class, + OffsetDateTime.parse("2024-03-01T00:00:00Z"), + "\"2024-03-01T00:00:00Z\"", + }, + { + OffsetDateTime.class, + OffsetDateTime.parse("2024-03-01T00:00:01Z"), + "\"2024-03-01T00:00:01Z\"", + }, + { + OffsetDateTime.class, + OffsetDateTime.parse("2024-03-01T12:34:56.789Z"), + "\"2024-03-01T12:34:56.789Z\"", + }, + { + OffsetDateTime.class, + OffsetDateTime.parse("2024-03-01T00:00:00.000000001Z"), + "\"2024-03-01T00:00:00.000000001Z\"", + }, + { + OffsetDateTime.class, + OffsetDateTime.parse("2024-03-01T00:00:00+02:00"), + "\"2024-03-01T00:00:00+02:00\"", + }, }; } From 243eb0969fd08950e8bad70551eb759f25c64a99 Mon Sep 17 00:00:00 2001 From: dyma solovei Date: Tue, 1 Dec 2026 18:13:55 +0100 Subject: [PATCH 2/2] feat(rbac): support expiration for db users --- .../api/rbac/users/CreateDbUserRequest.java | 42 ++++++++++++++++- .../client6/v1/api/rbac/users/DbUser.java | 1 + .../v1/api/rbac/users/DbUsersClient.java | 46 +++++++++++++++++-- .../v1/api/rbac/users/DbUsersClientAsync.java | 28 ++++++++++- .../users/UpdateDbUserExpirationRequest.java | 27 +++++++++++ 5 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 src/main/java/io/weaviate/client6/v1/api/rbac/users/UpdateDbUserExpirationRequest.java diff --git a/src/main/java/io/weaviate/client6/v1/api/rbac/users/CreateDbUserRequest.java b/src/main/java/io/weaviate/client6/v1/api/rbac/users/CreateDbUserRequest.java index 7b425cff5..776f3698a 100644 --- a/src/main/java/io/weaviate/client6/v1/api/rbac/users/CreateDbUserRequest.java +++ b/src/main/java/io/weaviate/client6/v1/api/rbac/users/CreateDbUserRequest.java @@ -1,17 +1,55 @@ package io.weaviate.client6.v1.api.rbac.users; +import java.time.OffsetDateTime; import java.util.Collections; +import java.util.Map; +import java.util.function.Function; +import io.weaviate.client6.v1.internal.ObjectBuilder; import io.weaviate.client6.v1.internal.json.JSON; import io.weaviate.client6.v1.internal.rest.Endpoint; import io.weaviate.client6.v1.internal.rest.SimpleEndpoint; import io.weaviate.client6.v1.internal.rest.UrlEncoder; -public record CreateDbUserRequest(String userId) { +public record CreateDbUserRequest(String userId, OffsetDateTime expiration) { - public static final Endpoint _ENDPOINT = SimpleEndpoint.noBody( + public static final Endpoint _ENDPOINT = new SimpleEndpoint<>( __ -> "POST", request -> "/users/db/" + UrlEncoder.encodeValue(request.userId), request -> Collections.emptyMap(), + request -> { + if (request.expiration() == null) { + return null; + } + return JSON.serialize(Map.of("expiresAt", request.expiration())); + }, (statusCode, response) -> JSON.deserialize(response, CreateDbUserResponse.class).apiKey()); + + public static CreateDbUserRequest of(String userId, Function> fn) { + return fn.apply(new Builder(userId)).build(); + } + + public CreateDbUserRequest(Builder builder) { + this(builder.userId, builder.expiration); + } + + public static final class Builder implements ObjectBuilder { + private final String userId; + private OffsetDateTime expiration; + + public Builder(String userId) { + this.userId = userId; + } + + /* Set user expiration time. */ + public Builder expiration(OffsetDateTime expiration) { + this.expiration = expiration; + return this; + } + + @Override + public CreateDbUserRequest build() { + return new CreateDbUserRequest(this); + } + } } diff --git a/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUser.java b/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUser.java index f73313b5c..0d95008ae 100644 --- a/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUser.java +++ b/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUser.java @@ -12,5 +12,6 @@ public record DbUser( @SerializedName("roles") List roleNames, @SerializedName("createdAt") OffsetDateTime createdAt, @SerializedName("lastUsedAt") OffsetDateTime lastUsedAt, + @SerializedName("expiresAt") OffsetDateTime userExpiresAt, @SerializedName("apiKeyFirstLetters") String apiKeyFirstLetters) { } diff --git a/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUsersClient.java b/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUsersClient.java index 18d77d310..5d0056271 100644 --- a/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUsersClient.java +++ b/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUsersClient.java @@ -1,6 +1,7 @@ package io.weaviate.client6.v1.api.rbac.users; import java.io.IOException; +import java.time.OffsetDateTime; import java.util.List; import java.util.Optional; import java.util.function.Function; @@ -27,14 +28,33 @@ public DbUsersClient(RestTransport restTransport) { * or the server being unavailable. */ public String create(String userId) throws IOException { - return this.restTransport.performRequest(new CreateDbUserRequest(userId), CreateDbUserRequest._ENDPOINT); + return this.restTransport.performRequest(CreateDbUserRequest.of(userId, ObjectBuilder.identity()), + CreateDbUserRequest._ENDPOINT); + } + + /** + * Create a new "db" user. + * + * @param userId User ID. + * @param fn Lambda expression for optional parameters. + * @return API key for the created user. + * @throws WeaviateApiException in case the server returned with an + * error status code. + * @throws IOException in case the request was not sent successfully + * due to a malformed request, a networking error + * or the server being unavailable. + */ + public String create(String userId, Function> fn) + throws IOException { + return this.restTransport.performRequest(CreateDbUserRequest.of(userId, fn), CreateDbUserRequest._ENDPOINT); } /** * Delete a "db" user. * * @param userId User ID. - * @return {@code true} if the user was deleted, {@code false} if there was no user to delete. + * @return {@code true} if the user was deleted, {@code false} if there was no + * user to delete. * @throws WeaviateApiException in case the server returned with an * error status code. * @throws IOException in case the request was not sent successfully @@ -49,7 +69,8 @@ public boolean delete(String userId) throws IOException { * Activate a "db" user. * * @param userId User ID. - * @return {@code true} if the user was activated, {@code false} if the user was already active. + * @return {@code true} if the user was activated, {@code false} if the user was + * already active. * @throws WeaviateApiException in case the server returned with an * error status code. * @throws IOException in case the request was not sent successfully @@ -64,7 +85,8 @@ public boolean activate(String userId) throws IOException { * Deactivate a "db" user. * * @param userId User ID. - * @return {@code true} if the user was deactivated, {@code false} if the user was already deactivated. + * @return {@code true} if the user was deactivated, {@code false} if the user + * was already deactivated. * @throws WeaviateApiException in case the server returned with an * error status code. * @throws IOException in case the request was not sent successfully @@ -89,6 +111,22 @@ public String rotateKey(String userId) throws IOException { return this.restTransport.performRequest(new RotateDbUserKeyRequest(userId), RotateDbUserKeyRequest._ENDPOINT); } + /** + * Update user expiration time. + * + * @param userId User ID. + * @param expiration New expiration. Set {@code null} to disable expiration. + * @throws WeaviateApiException in case the server returned with an + * error status code. + * @throws IOException in case the request was not sent successfully + * due to a malformed request, a networking error + * or the server being unavailable. + */ + public void updateUserExpiration(String userId, OffsetDateTime expiration) throws IOException { + this.restTransport.performRequest(new UpdateDbUserExpirationRequest(userId, expiration), + UpdateDbUserExpirationRequest._ENDPOINT); + } + /** * Fetch "db" user info. * diff --git a/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUsersClientAsync.java b/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUsersClientAsync.java index 0d291ca43..25a8f11cd 100644 --- a/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUsersClientAsync.java +++ b/src/main/java/io/weaviate/client6/v1/api/rbac/users/DbUsersClientAsync.java @@ -1,6 +1,7 @@ package io.weaviate.client6.v1.api.rbac.users; import java.io.IOException; +import java.time.OffsetDateTime; import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; @@ -22,7 +23,21 @@ public DbUsersClientAsync(RestTransport restTransport) { * @return API key for the created user. */ public CompletableFuture create(String userId) throws IOException { - return this.restTransport.performRequestAsync(new CreateDbUserRequest(userId), CreateDbUserRequest._ENDPOINT); + return this.restTransport.performRequestAsync(CreateDbUserRequest.of(userId, ObjectBuilder.identity()), + CreateDbUserRequest._ENDPOINT); + } + + /** + * Create a new "db" user. + * + * @param userId User ID. + * @param fn Lambda expression for optional parameters. + * @return API key for the created user. + */ + public CompletableFuture create(String userId, + Function> fn) + throws IOException { + return this.restTransport.performRequestAsync(CreateDbUserRequest.of(userId, fn), CreateDbUserRequest._ENDPOINT); } /** @@ -62,6 +77,17 @@ public CompletableFuture rotateKey(String userId) throws IOException { return this.restTransport.performRequestAsync(new RotateDbUserKeyRequest(userId), RotateDbUserKeyRequest._ENDPOINT); } + /** + * Update user expiration time. + * + * @param userId User ID. + * @param expiration New expiration. Set {@code null} to disable expiration. + */ + public CompletableFuture updateUserExpiration(String userId, OffsetDateTime expiration) throws IOException { + return this.restTransport.performRequestAsync(new UpdateDbUserExpirationRequest(userId, expiration), + UpdateDbUserExpirationRequest._ENDPOINT); + } + /** * Fetch "db" user info. * diff --git a/src/main/java/io/weaviate/client6/v1/api/rbac/users/UpdateDbUserExpirationRequest.java b/src/main/java/io/weaviate/client6/v1/api/rbac/users/UpdateDbUserExpirationRequest.java new file mode 100644 index 000000000..bd1fcff4c --- /dev/null +++ b/src/main/java/io/weaviate/client6/v1/api/rbac/users/UpdateDbUserExpirationRequest.java @@ -0,0 +1,27 @@ +package io.weaviate.client6.v1.api.rbac.users; + +import java.time.OffsetDateTime; +import java.util.Collections; +import java.util.Map; + +import io.weaviate.client6.v1.internal.json.JSON; +import io.weaviate.client6.v1.internal.rest.Endpoint; +import io.weaviate.client6.v1.internal.rest.SimpleEndpoint; +import io.weaviate.client6.v1.internal.rest.UrlEncoder; + +public record UpdateDbUserExpirationRequest(String userId, OffsetDateTime expiration) { + + public static final Endpoint _ENDPOINT = SimpleEndpoint.sideEffect( + __ -> "PUT", + request -> "/users/db/" + UrlEncoder.encodeValue(request.userId) + "/expiration", + request -> Collections.emptyMap(), + request -> { + Map body = null; + if (request.expiration == null) { + body = Map.of("expiresAt", request.expiration()); + } else { + body = Map.of("neverExpires", true); + } + return JSON.serialize(body); + }); +}