diff --git a/dev/src/main/java/com/google/adk/web/controller/SessionController.java b/dev/src/main/java/com/google/adk/web/controller/SessionController.java index 2540e63a..1be7db35 100644 --- a/dev/src/main/java/com/google/adk/web/controller/SessionController.java +++ b/dev/src/main/java/com/google/adk/web/controller/SessionController.java @@ -21,6 +21,7 @@ import com.google.adk.sessions.BaseSessionService; import com.google.adk.sessions.ListSessionsResponse; import com.google.adk.sessions.Session; +import com.google.adk.web.dto.SessionRequest; import io.reactivex.rxjava3.core.Maybe; import io.reactivex.rxjava3.core.Single; import java.util.Collections; @@ -167,13 +168,15 @@ public Session createSessionWithId( @PathVariable String appName, @PathVariable String userId, @PathVariable String sessionId, - @RequestBody(required = false) Map state) { + @RequestBody(required = false) SessionRequest body) { log.info( - "Request received for POST /apps/{}/users/{}/sessions/{} with state: {}", + "Request received for POST /apps/{}/users/{}/sessions/{} with request body: {}", appName, userId, sessionId, - state); + body); + + Map initialState = (body != null) ? body.getState() : Collections.emptyMap(); try { findSessionOrThrow(appName, userId, sessionId); @@ -190,7 +193,6 @@ public Session createSessionWithId( log.info("Session {} not found, proceeding with creation.", sessionId); } - Map initialState = (state != null) ? state : Collections.emptyMap(); try { Session createdSession = sessionService @@ -228,18 +230,17 @@ public Session createSessionWithId( public Session createSession( @PathVariable String appName, @PathVariable String userId, - @RequestBody(required = false) Map state) { + @RequestBody(required = false) SessionRequest body) { log.info( - "Request received for POST /apps/{}/users/{}/sessions (service generates ID) with state:" - + " {}", + "Request received for POST /apps/{}/users/{}/sessions (service generates ID) with request" + + " body: {}", appName, userId, - state); + body); - Map initialState = (state != null) ? state : Collections.emptyMap(); try { - + Map initialState = (body != null) ? body.getState() : Collections.emptyMap(); Session createdSession = sessionService .createSession(appName, userId, new ConcurrentHashMap<>(initialState), null) diff --git a/dev/src/main/java/com/google/adk/web/dto/SessionRequest.java b/dev/src/main/java/com/google/adk/web/dto/SessionRequest.java new file mode 100644 index 00000000..521fd5b1 --- /dev/null +++ b/dev/src/main/java/com/google/adk/web/dto/SessionRequest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.adk.web.dto; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.collect.ImmutableMap; +import java.util.Map; + +/** + * Data Transfer Object (DTO) for POST /apps/{appName}/users/{userId}/sessions and POST + * /apps/{appName}/users/{userId}/sessions/{sessionId} equests. Contains information for a session. + */ +public final class SessionRequest { + private final ImmutableMap state; + + @JsonCreator + public SessionRequest(@JsonProperty("state") Map state) { + this.state = (state == null) ? ImmutableMap.of() : ImmutableMap.copyOf(state); + } + + public ImmutableMap getState() { + return state; + } +}