fix: preserve typed array items in nullable anyOf schemas#274
Open
4444J99 wants to merge 1 commit intotadata-org:mainfrom
Open
fix: preserve typed array items in nullable anyOf schemas#2744444J99 wants to merge 1 commit intotadata-org:mainfrom
items in nullable anyOf schemas#2744444J99 wants to merge 1 commit intotadata-org:mainfrom
Conversation
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
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When OpenAPI represents optional typed parameters (e.g.
Optional[List[int]]), it produces ananyOfschema:{ "anyOf": [ {"type": "array", "items": {"type": "integer"}}, {"type": "null"} ] }get_single_param_type_from_schemacorrectly resolves the type to"array"and injects it at the top level, but theitemsdefinition remains buried inside theanyOfvariant. 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
inputSchemaagainst JSON Schema (such as gpt-4o) reject this becausetype: "array"at the top level lacks a correspondingitemsdefinition.Solution
Add
flatten_nullable_anyof()toopenapi/utils.py. When a schema contains a simple nullableanyOf(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 theanyOfkey.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
items), objects (properties,required,additionalProperties), and any other fields on the non-null variant (e.g.format,enum,minItems,maxItems).Tests
6 new tests covering:
flatten_nullable_anyofutility: array items hoisting, no-op on non-nullable, skip on multi-type unions, object properties hoistingAll 123 tests pass (117 existing + 6 new). Coverage 83.29%.
Fixes #57