Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/src/main/java/com/google/adk/workflow/FunctionNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ public Builder<I, O> toBuilder() {
* JSON shape infers none. An edge into an inferred record or POJO fails at build if the
* producer's output schema lists a key the type does not, while a builder schema checks only the
* keys it lists and requires. On a {@code Content} port a builder schema describes the message
* text, and an edge is checked against the {@code Content} schema instead.
* text, and an edge is checked against the {@code Content} schema instead. On an {@code Event} or
* genai port, or a list, map or array of them, a builder schema does not let the port take keys
* its type lacks.
*/
@Override
public Optional<Schema> inputSchema() {
Expand Down Expand Up @@ -189,7 +191,7 @@ Optional<Schema> edgeInputSchema() {

/** Whether a builder schema would let this port take keys its type does not list. */
boolean acceptsExtraKeysWithASchema() {
return inputSchemaInferred() && !inputCodec.takesContent();
return inputSchemaInferred() && !inputCodec.rejectsUnknownKeys();
}

/** Whether {@link #edgeInputSchema} was inferred, so its objects with properties are closed. */
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/com/google/adk/workflow/FunctionNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
* "3"}). An integer also accepts a whole number such as {@code 3.0}, and a char only one
* character of text.
* <li>An enum or boolean reads a number only where Jackson writes it as one.
* <li>A port of {@code Event} or of a {@code com.google.genai.types} type such as {@code
* Content}, or of a list, map or array of them, rejects unknown keys anywhere in its value.
* Other ports drop them.
* </ul>
*
* <p>A node's body asks the user for input with {@link #requestInput}.
Expand Down
52 changes: 42 additions & 10 deletions core/src/main/java/com/google/adk/workflow/PortCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@
final class PortCodec<T> {

/**
* One mapper for every port: unknown properties are ignored, null fields are written, {@code
* java.time} values travel as ISO-8601 strings, and genai types as genai writes them. Numbers,
* booleans and chars, also inside genai types, read as {@link PortScalars} describes, and any
* other whole-number type reads a float only when it has no fraction. It rejects a null
* primitive, an empty string as a number, boolean, enum or date, a number as an enum not written
* as one, a number or boolean as text, and a missing {@link RequiredProperties} property.
* The port mapper: unknown properties are ignored, null fields are written, {@code java.time}
* values travel as ISO-8601 strings, and genai types as genai writes them. Numbers, booleans and
* chars, also inside genai types, read as {@link PortScalars} describes, and any other
* whole-number type reads a float only when it has no fraction. It rejects a null primitive, an
* empty string as a number, boolean, enum or date, a number as an enum not written as one, a
* number or boolean as text, and a missing {@link RequiredProperties} property.
*/
static final ObjectMapper MAPPER =
JsonMapper.builder()
Expand All @@ -131,6 +131,14 @@ final class PortCodec<T> {
/** Genai's own reader of the form genai writes, with the port rules for scalars. */
private static final ObjectReader GENAI_READER = genaiReader();

/** {@link #GENAI_READER} rejecting unknown properties. */
private static final ObjectReader STRICT_GENAI_READER =
GENAI_READER.with(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

/** {@link #MAPPER} rejecting unknown properties, for a port that {@link #rejectsUnknownKeys}. */
private static final ObjectMapper STRICT_MAPPER =
MAPPER.copy().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

/** The port's type, with a primitive boxed. */
private final JavaType type;

Expand All @@ -139,6 +147,9 @@ final class PortCodec<T> {
/** Whether the declared type is primitive, so the port takes no null. */
private final boolean primitive;

/** {@link #STRICT_MAPPER} if this port {@link #rejectsUnknownKeys}, else {@link #MAPPER}. */
private final ObjectMapper mapper;

private PortCodec(JavaType declared, boolean output) {
this.primitive = declared.isPrimitive();
this.type =
Expand All @@ -151,6 +162,7 @@ private PortCodec(JavaType declared, boolean output) {
output && type.getRawClass() == Event.class
? () -> null
: Suppliers.memoize(() -> PortSchemas.infer(type, output));
this.mapper = rejectsUnknownKeys() ? STRICT_MAPPER : MAPPER;
}

static JavaType javaType(Class<?> type) {
Expand Down Expand Up @@ -494,6 +506,19 @@ boolean takesContent() {
return type.getRawClass() == Content.class;
}

/**
* Whether this port rejects unknown keys at any depth: it reads an {@code Event} or genai value,
* or a list, map or array of them.
*/
boolean rejectsUnknownKeys() {
JavaType value = type;
while (value.isContainerType()) {
value = value.getContentType();
}
return Event.class.isAssignableFrom(value.getRawClass())
|| JsonSerializable.class.isAssignableFrom(value.getRawClass());
}

/**
* Returns {@code value} read as this port's type; {@code what} names the port in errors.
*
Expand Down Expand Up @@ -523,7 +548,7 @@ boolean takesContent() {
}
T converted;
try {
converted = MAPPER.convertValue(value, type);
converted = mapper.convertValue(value, type);
} catch (RuntimeException e) {
// Jackson's and genai's messages can quote the value, so no cause.
throw doesNotMatch(what);
Expand Down Expand Up @@ -566,7 +591,7 @@ boolean takesContent() {
// not be readable, so a generic type's own fields are trusted.
if (!hasType(value, type, /* trustGenericFields= */ true)) {
try {
value = MAPPER.convertValue(value, type);
value = mapper.convertValue(value, type);
} catch (RuntimeException e) {
// Jackson's message can quote the value, so no cause.
throw new IllegalArgumentException(what + " does not match " + type.toCanonical() + ".");
Expand Down Expand Up @@ -778,15 +803,22 @@ private static void rejectScalarsAsText(MutableCoercionConfig config) {
.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail);
}

/** Reads a genai value with {@link #GENAI_READER}. */
/**
* Reads a genai value with {@link #GENAI_READER}. Genai's reader drops unknown keys, so this
* rejects them when the calling mapper does.
*/
private static final class GenaiDeserializer extends StdDeserializer<Object> {
GenaiDeserializer(Class<?> type) {
super(type);
}

@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return GENAI_READER.treeToValue(ctxt.readTree(p), handledType());
ObjectReader reader =
ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
? STRICT_GENAI_READER
: GENAI_READER;
return reader.treeToValue(ctxt.readTree(p), handledType());
}
}

Expand Down
171 changes: 151 additions & 20 deletions core/src/test/java/com/google/adk/workflow/FunctionNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ record Flat(String label, @JsonUnwrapped Order order) {}
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
record Pair(int x, int y) {}

/** A user record that holds an event. */
record Envelope(String id, Event event) {}

/** Written as its ordinal wherever it appears, as its numeric shape asks. */
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
enum Rank {
Expand Down Expand Up @@ -978,6 +981,44 @@ public void builder_bodyDelegatingAnOutputThatIsNotAnO_fails() {
.contains("output of node 'router' does not match");
}

@Test
public void builder_contentBodyDelegatingAValueWithAnUnknownKey_fails() {
// Arrange
ImmutableMap<String, Object> message =
ImmutableMap.of("parts", ImmutableList.of(ImmutableMap.of("text", "hi", "extra", 1)));

// Act
RunResult result = run(chain(delegating(Content.class, message)));

// Assert
assertThat(result.errorMessage("IllegalArgumentException"))
.isEqualTo(
"validation error: output of node 'router' does not match "
+ Content.class.getName()
+ ".");
}

@Test
public void of_partListBodyReturningAnUnknownKey_fails() {
// Arrange: an unchecked cast lets a map with an unknown key into the list.
Map<String, Object> part = new HashMap<>();
part.put("text", "hi");
part.put("extra", 1);
@SuppressWarnings("unchecked")
List<Part> parts = (List<Part>) (List<?>) ImmutableList.of(part);
FunctionNode<Content, List<Part>> node =
FunctionNodes.of(
"parts", Content.class, new TypeReference<List<Part>>() {}, (ctx, m) -> parts);

// Act
RunResult result = run(chain(node));

// Assert
assertThat(result.errorMessage("IllegalArgumentException"))
.isEqualTo(
"output of node 'parts' does not match java.util.List<" + Part.class.getName() + ">.");
}

@Test
public void builder_voidBodyDelegatingItsOutput_fails() {
RunResult result = run(chain(delegating(Void.class, "x")));
Expand Down Expand Up @@ -2926,22 +2967,6 @@ public void build_integerIntoANullableIntegerSchema_buildsAndRuns() {
assertThat(result.outputAt("wf@1/increment@1")).isEqualTo(4);
}

@Test
public void build_recordIntoAContentPort_failsWithoutSuggestingABuilderSchema() {
// Arrange: a builder schema on a Content port describes the text, so it cannot help here.
FunctionNode<Content, FullOrder> full = fullOrder();
FunctionNode<Content, String> read =
FunctionNodes.of("read", Content.class, String.class, (ctx, content) -> content.text());

// Act
WorkflowConfigurationException error =
assertThrows(WorkflowConfigurationException.class, () -> chain(full, read));

// Assert
assertThat(error).hasMessageThat().startsWith("Schema mismatch on edge full -> read.");
assertThat(error).hasMessageThat().doesNotContain("inputSchema on the builder");
}

@Test
public void build_contentIntoAStringPort_fails() {
// Arrange: a returned Content is the output in its JSON form, an object.
Expand All @@ -2959,18 +2984,31 @@ public void build_contentIntoAStringPort_fails() {
}

@Test
public void build_recordIntoAnEventPort_fails() {
// Arrange: an Event port takes only an event's keys, and a FullOrder has others.
public void build_recordIntoAnEventOrGenaiPort_failsWithoutSuggestingABuilderSchema() {
// Arrange: these ports reject a FullOrder's other keys at run time, whatever their schema.
FunctionNode<Content, FullOrder> full = fullOrder();
FunctionNode<Content, String> read =
FunctionNodes.of("read", Content.class, String.class, (ctx, content) -> content.text());
FunctionNode<Event, String> author =
FunctionNodes.of("author", Event.class, String.class, (ctx, event) -> event.author());
FunctionNode<Part, String> part =
FunctionNodes.of("part", Part.class, String.class, (ctx, p) -> p.text().orElse(""));

// Act
WorkflowConfigurationException error =
WorkflowConfigurationException intoContent =
assertThrows(WorkflowConfigurationException.class, () -> chain(full, read));
WorkflowConfigurationException intoEvent =
assertThrows(WorkflowConfigurationException.class, () -> chain(full, author));
WorkflowConfigurationException intoPart =
assertThrows(WorkflowConfigurationException.class, () -> chain(full, part));

// Assert
assertThat(error).hasMessageThat().startsWith("Schema mismatch on edge full -> author.");
assertThat(intoContent).hasMessageThat().startsWith("Schema mismatch on edge full -> read.");
assertThat(intoContent).hasMessageThat().doesNotContain("inputSchema on the builder");
assertThat(intoEvent).hasMessageThat().startsWith("Schema mismatch on edge full -> author.");
assertThat(intoEvent).hasMessageThat().doesNotContain("inputSchema on the builder");
assertThat(intoPart).hasMessageThat().startsWith("Schema mismatch on edge full -> part.");
assertThat(intoPart).hasMessageThat().doesNotContain("inputSchema on the builder");
}

@Test
Expand Down Expand Up @@ -5809,6 +5847,99 @@ public void of_primitiveArrayPortsFedScalars_readEachElementAsItsTypeDoes() {
assertThat(singleValue).isEqualTo("[5]");
}

@Test
public void of_eventOrGenaiPortsFedAnUnknownKey_rejectIt() {
// Arrange: each value has an unknown key, at its top level or deeper.
ImmutableMap<String, Object> part = ImmutableMap.of("text", "hi", "extra", 1);
ImmutableList<Case> cases =
ImmutableList.of(
new Case(ImmutableMap.of("author", "a", "extra", 1), Event.class, "fails"),
new Case(
ImmutableMap.of("actions", ImmutableMap.of("stateDetla", 1)), Event.class, "fails"),
new Case(
ImmutableMap.of("nodeInfo", ImmutableMap.of("pathh", "wf@1")),
Event.class,
"fails"),
new Case(part, Part.class, "fails"),
new Case(ImmutableList.of(part), Part[].class, "fails"),
new Case(ImmutableMap.of("parts", ImmutableList.of(part)), Content.class, "fails"));

// Act
ImmutableMap<String, Object> actual = readAll(cases);

// Assert
assertThat(actual).containsExactlyEntriesIn(expected(cases)).inOrder();
}

@Test
public void of_userRecordPortsFedAnUnknownKey_dropIt() {
// Arrange: each value has unknown keys, also inside an event or genai value it holds.
ImmutableMap<String, Object> event =
ImmutableMap.of("author", "a", "extra", 1, "actions", ImmutableMap.of("stateDetla", 1));
ImmutableMap<String, Object> part = ImmutableMap.of("text", "hi", "extra", 1);
ImmutableMap<String, Object> content =
ImmutableMap.of("parts", ImmutableList.of(part), "extra", 1);
FunctionNode<Order, String> order =
FunctionNodes.of("n", Order.class, String.class, (ctx, o) -> o.id() + " x" + o.quantity());
FunctionNode<Envelope, String> envelope =
FunctionNodes.of(
"n", Envelope.class, String.class, (ctx, e) -> e.id() + " " + e.event().author());
FunctionNode<Reply, String> reply =
FunctionNodes.of(
"n", Reply.class, String.class, (ctx, r) -> r.id() + " " + r.content().text());

// Act
Object orderOutput = readWith(ImmutableMap.of("id", "a1", "quantity", 3, "extra", 1), order);
Object envelopeOutput =
readWith(ImmutableMap.of("id", "e1", "event", event, "extra", 1), envelope);
Object replyOutput = readWith(ImmutableMap.of("id", "r1", "content", content), reply);

// Assert
assertThat(orderOutput).isEqualTo("a1 x3");
assertThat(envelopeOutput).isEqualTo("e1 a");
assertThat(replyOutput).isEqualTo("r1 hi");
}

@Test
public void of_eventListsAndGenaiMapPortsFedAnUnknownKey_rejectIt() {
// Arrange
FunctionNode<List<Event>, String> events =
FunctionNodes.of(
"n", new TypeReference<List<Event>>() {}, String.class, (ctx, l) -> l.get(0).author());
FunctionNode<List<List<Event>>, String> nestedEvents =
FunctionNodes.of(
"n",
new TypeReference<List<List<Event>>>() {},
String.class,
(ctx, l) -> l.get(0).get(0).author());
FunctionNode<Map<String, Part>, String> parts =
FunctionNodes.of(
"n",
new TypeReference<Map<String, Part>>() {},
String.class,
(ctx, m) -> m.get("a").text().orElse(""));

// Act
Object known = readWith(ImmutableList.of(ImmutableMap.of("author", "a")), events);
Object eventsOutput =
readWith(ImmutableList.of(ImmutableMap.of("author", "a", "extra", 1)), events);
Object nestedKnown =
readWith(ImmutableList.of(ImmutableList.of(ImmutableMap.of("author", "a"))), nestedEvents);
Object nestedOutput =
readWith(
ImmutableList.of(ImmutableList.of(ImmutableMap.of("author", "a", "extra", 1))),
nestedEvents);
Object partsOutput =
readWith(ImmutableMap.of("a", ImmutableMap.of("text", "hi", "extra", 1)), parts);

// Assert
assertThat(known).isEqualTo("a");
assertThat(eventsOutput).isEqualTo("fails");
assertThat(nestedKnown).isEqualTo("a");
assertThat(nestedOutput).isEqualTo("fails");
assertThat(partsOutput).isEqualTo("fails");
}

@Test
public void of_recordWithUntypedFields_keepsTheNumberTypesItIsFed() {
// Arrange
Expand Down
Loading