Skip to content

fix: preserve typed array items in nullable anyOf schemas#274

Open
4444J99 wants to merge 1 commit intotadata-org:mainfrom
4444J99:fix/typed-array-items-preservation
Open

fix: preserve typed array items in nullable anyOf schemas#274
4444J99 wants to merge 1 commit intotadata-org:mainfrom
4444J99:fix/typed-array-items-preservation

Conversation

@4444J99
Copy link
Copy Markdown

@4444J99 4444J99 commented Mar 27, 2026

Problem

When OpenAPI represents optional typed parameters (e.g. Optional[List[int]]), it produces an anyOf schema:

{
  "anyOf": [
    {"type": "array", "items": {"type": "integer"}},
    {"type": "null"}
  ]
}

get_single_param_type_from_schema correctly resolves the type to "array" and injects it at the top level, but the items definition remains buried inside the anyOf variant. The resulting MCP tool schema looks like:

{
  "anyOf": [
    {"type": "array", "items": {"type": "integer"}},
    {"type": "null"}
  ],
  "type": "array",
  "title": "time_values"
}

LLM clients that validate tool inputSchema against JSON Schema (such as gpt-4o) reject this because type: "array" at the top level lacks a corresponding items definition.

Solution

Add flatten_nullable_anyof() to openapi/utils.py. When a schema contains a simple nullable anyOf (one non-null type + null), it hoists all type-specific fields (items, properties, required, etc.) from the non-null variant to the top level and removes the anyOf key.

Called after type injection for both query parameters and body parameters in convert.py.

Before

{"anyOf": [{"type": "array", "items": {"type": "integer"}}, {"type": "null"}], "type": "array", "title": "time_values"}

After

{"type": "array", "items": {"type": "integer"}, "title": "time_values"}

Scope

  • Only flattens simple nullable patterns (exactly one non-null type + null). Complex multi-type unions are left unchanged.
  • Handles arrays (items), objects (properties, required, additionalProperties), and any other fields on the non-null variant (e.g. format, enum, minItems, maxItems).
  • No changes to non-nullable schemas.

Tests

6 new tests covering:

  • flatten_nullable_anyof utility: array items hoisting, no-op on non-nullable, skip on multi-type unions, object properties hoisting
  • End-to-end conversion: optional typed arrays in body params and query params

All 123 tests pass (117 existing + 6 new). Coverage 83.29%.

Fixes #57

When OpenAPI represents optional typed parameters (e.g. Optional[List[int]]),
it uses anyOf with the typed schema and a null variant. The conversion to MCP
tool schemas correctly resolved the type to "array" but left the "items"
definition buried inside the anyOf variant. LLM clients that validate against
JSON Schema (such as gpt-4o) rejected these schemas because the top-level
"type: array" lacked an "items" definition.

Add flatten_nullable_anyof() to hoist type-specific fields (items, properties,
required, etc.) from simple nullable anyOf patterns to the top level, producing
flat, valid schemas.

Fixes tadata-org#57
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typed arrays

1 participant