Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(json-schema): accept document-root references in strict mode

jkomyno9feca8f95d89
diff --git a/python/composio/utils/strict_schema.py b/python/composio/utils/strict_schema.py
index 59e3552a5..48a7a453f 100644
--- a/python/composio/utils/strict_schema.py
+++ b/python/composio/utils/strict_schema.py
@@ -292,7 +292,9 @@ class _Walker:
ref = node.get("$ref")
if isinstance(ref, str):
- resolved = ref.startswith("#/") and _try_resolve_pointer(self.root, ref).ok
+ resolved = (ref == "#" or ref.startswith("#/")) and _try_resolve_pointer(
+ self.root, ref
+ ).ok
if not resolved:
self.reject(path, "$ref", f'unresolved $ref "{ref}"')
# The referenced definition is normalized where it is declared.
diff --git a/python/tests/fixtures/json-schema-conversion/strict-cases.json b/python/tests/fixtures/json-schema-conversion/strict-cases.json
index 4cf465516..1147b2c77 100644
--- a/python/tests/fixtures/json-schema-conversion/strict-cases.json
+++ b/python/tests/fixtures/json-schema-conversion/strict-cases.json
@@ -1539,6 +1539,50 @@
}
}
},
+ {
+ "id": "root-ref-kept",
+ "description": "A local $ref to the document root resolves and is kept.",
+ "schema": {
+ "type": "object",
+ "properties": {
+ "child": {
+ "$ref": "#"
+ },
+ "label": {
+ "type": "string"
+ }
+ },
+ "required": ["label"]
+ },
+ "strict": {
+ "schema": {
+ "type": "object",
+ "properties": {
+ "child": {
+ "anyOf": [
+ {
+ "$ref": "#"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "label": {
+ "type": "string"
+ }
+ },
+ "required": ["child", "label"],
+ "additionalProperties": false
+ }
+ },
+ "changes": [
+ {
+ "path": "properties.child",
+ "reason": "optional-property-nullable"
+ }
+ ]
+ },
{
"id": "dangling-ref-unsupported",
"description": "A $ref without a target is reported at its own path.",
diff --git a/ts/packages/core/src/utils/jsonSchema.ts b/ts/packages/core/src/utils/jsonSchema.ts
index a006071ad..19770dcfe 100644
--- a/ts/packages/core/src/utils/jsonSchema.ts
+++ b/ts/packages/core/src/utils/jsonSchema.ts
@@ -792,9 +792,10 @@ export function toStrictJsonSchema(schema: unknown): StrictJsonSchemaResult {
const out = walkChildren(node, mode, depth, path);
if (typeof node.$ref === 'string') {
- const resolution = node.$ref.startsWith('#/')
- ? tryResolvePointer(root, node.$ref)
- : ({ kind: 'unresolved', reason: 'malformed-pointer' } as const);
+ const resolution =
+ node.$ref === '#' || node.$ref.startsWith('#/')
+ ? tryResolvePointer(root, node.$ref)
+ : ({ kind: 'unresolved', reason: 'malformed-pointer' } as const);
if (resolution.kind === 'unresolved') {
unsupported.push({
path,
diff --git a/ts/packages/core/test/fixtures/json-schema-conversion/generate-strict-cases.mjs b/ts/packages/core/test/fixtures/json-schema-conversion/generate-strict-cases.mjs
index 5483f77e7..6208648b4 100644
--- a/ts/packages/core/test/fixtures/json-schema-conversion/generate-strict-cases.mjs
+++ b/ts/packages/core/test/fixtures/json-schema-conversion/generate-strict-cases.mjs
@@ -231,6 +231,11 @@ const cases = [
'A local $ref outside $defs resolves and is kept.',
obj({ a: obj({ x: { type: 'string' } }, ['x']), b: { $ref: '#/properties/a' } }, ['a', 'b']),
],
+ [
+ 'root-ref-kept',
+ 'A local $ref to the document root resolves and is kept.',
+ obj({ child: { $ref: '#' }, label: { type: 'string' } }, ['label']),
+ ],
[
'dangling-ref-unsupported',
'A $ref without a target is reported at its own path.',
diff --git a/ts/packages/core/test/fixtures/json-schema-conversion/strict-cases.json b/ts/packages/core/test/fixtures/json-schema-conversion/strict-cases.json
index 4cf465516..1147b2c77 100644
--- a/ts/packages/core/test/fixtures/json-schema-conversion/strict-cases.json
+++ b/ts/packages/core/test/fixtures/json-schema-conversion/strict-cases.json
@@ -1539,6 +1539,50 @@
}
}
},
+ {
+ "id": "root-ref-kept",
+ "description": "A local $ref to the document root resolves and is kept.",
+ "schema": {
+ "type": "object",
+ "properties": {
+ "child": {
+ "$ref": "#"
+ },
+ "label": {
+ "type": "string"
+ }
+ },
+ "required": ["label"]
+ },
+ "strict": {
+ "schema": {
+ "type": "object",
+ "properties": {
+ "child": {
+ "anyOf": [
+ {
+ "$ref": "#"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "label": {
+ "type": "string"
+ }
+ },
+ "required": ["child", "label"],
+ "additionalProperties": false
+ }
+ },
+ "changes": [
+ {
+ "path": "properties.child",
+ "reason": "optional-property-nullable"
+ }
+ ]
+ },
{
"id": "dangling-ref-unsupported",
"description": "A $ref without a target is reported at its own path.",