Skip to content

Commit 89b150f

Browse files
committed
fix(workflow): reject schemas that violate the draft 2020-12 meta-schema at publish time (issue agentic-ai-java#274)
1 parent ab05cfe commit 89b150f

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎argi-orchestration/argi-workflow-core/src/main/java/io/github/agentic/ai/workflow/WorkflowDefinitionValidator.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package io.github.agentic.ai.workflow;
1717

18+
import com.networknt.schema.Error;
19+
import com.networknt.schema.InputFormat;
20+
import com.networknt.schema.Schema;
21+
import com.networknt.schema.SchemaLocation;
1822
import com.networknt.schema.SchemaRegistry;
1923
import com.networknt.schema.SpecificationVersion;
2024

@@ -39,6 +43,9 @@ public final class WorkflowDefinitionValidator {
3943

4044
private static final SchemaRegistry SCHEMAS = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
4145

46+
private static final Schema META_SCHEMA = SCHEMAS
47+
.getSchema(SchemaLocation.of(SpecificationVersion.DRAFT_2020_12.getDialectId()));
48+
4249
private WorkflowDefinitionValidator() {
4350
}
4451

@@ -124,6 +131,19 @@ private static void validateSchema(String field, String schema) {
124131
if (schema.isBlank()) {
125132
throw new IllegalArgumentException(field + " must not be blank");
126133
}
134+
// Compiling the document is not enough: a non-object document or an invalid
135+
// keyword value would be accepted here and silently ignored at run time.
136+
List<Error> errors;
137+
try {
138+
errors = META_SCHEMA.validate(schema, InputFormat.JSON);
139+
}
140+
catch (RuntimeException exception) {
141+
throw new IllegalArgumentException(field + " is not valid JSON: " + exception.getMessage(), exception);
142+
}
143+
if (!errors.isEmpty()) {
144+
throw new IllegalArgumentException(
145+
field + " is not a valid JSON Schema (draft 2020-12): " + errors.get(0).getMessage());
146+
}
127147
try {
128148
SCHEMAS.getSchema(schema);
129149
}

‎argi-orchestration/argi-workflow-core/src/test/java/io/github/agentic/ai/workflow/WorkflowSchemaContractTest.java‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import java.util.concurrent.atomic.AtomicInteger;
4444
import java.util.concurrent.atomic.AtomicReference;
4545
import org.junit.jupiter.api.Test;
46+
import org.junit.jupiter.params.ParameterizedTest;
47+
import org.junit.jupiter.params.provider.ValueSource;
4648

4749
import static org.junit.jupiter.api.Assertions.assertEquals;
4850
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@@ -58,7 +60,7 @@
5860
*/
5961
class WorkflowSchemaContractTest {
6062

61-
private static final String MALFORMED_SCHEMA = "{\"type\":\"objec";
63+
private static final String MALFORMED_SCHEMA = "{\"type\":\"object\"";
6264

6365
private static final String INPUT_SCHEMA =
6466
"{\"type\":\"object\",\"properties\":{\"question\":{\"type\":\"string\"}},\"required\":[\"question\"]}";
@@ -81,6 +83,24 @@ void publishRejectsMalformedSchemas() {
8183
assertTrue(output.getMessage().contains("outputSchema"));
8284
}
8385

86+
@ParameterizedTest
87+
@ValueSource(strings = { "{\"required\":\"question\"}", "[]", "123", "\"schema\"", "null" })
88+
void publishRejectsDocumentsThatAreNotJsonSchemas(String schema) {
89+
WorkflowRegistry registry = new WorkflowRegistry();
90+
IllegalArgumentException input = assertThrows(IllegalArgumentException.class,
91+
() -> registry.publish(definition(schema, null)));
92+
assertTrue(input.getMessage().contains("inputSchema"));
93+
IllegalArgumentException output = assertThrows(IllegalArgumentException.class,
94+
() -> registry.publish(definition(null, schema)));
95+
assertTrue(output.getMessage().contains("outputSchema"));
96+
}
97+
98+
@Test
99+
void publishAcceptsObjectAndBooleanSchemas() {
100+
new WorkflowRegistry().publish(definition("{\"type\":\"object\"}", "true"));
101+
new WorkflowRegistry().publish(definition("false", "{}"));
102+
}
103+
84104
@Test
85105
void blankSchemasStayRejected() {
86106
WorkflowRegistry registry = new WorkflowRegistry();

0 commit comments

Comments
 (0)