Repository navigation
fix(tools): resolve escaped and indexed JSON Pointer refs in OpenAPI specs - #8430
omkarbandikatte wants to merge 2 commits into
Conversation
1AbdullahButt
left a comment
There was a problem hiding this comment.
Good fix, with one compatibility gap worth closing before merge. I ran pytest tests/unittests/tools/openapi_tool/ in python:3.12 with constraints-3.12.txt: 436 pass on the head. On main, only the new test_parse_spec_with_escaped_and_indexed_json_pointer_reference fails.
A scratch script called OpenApiSpecParser()._resolve_references() on both branches. #/paths/~1x/get/responses/200 and .../parameters/0/name now resolve where main left the $ref, and #/components/schemas/My%20Schema now finds a key named My Schema.
The gap: refs that main resolved now silently stay as unresolved $refs. When a schema key literally contains %20 (or ~1), main looked the key up verbatim and found it. The head percent-decodes and unescapes first, so {"My%20Schema": ...} and {"a~1b": ...} are no longer found. RFC 6901 is on your side, but specs generated by tools that don't encode keys do exist. Today they would just lose their parameter schemas without any warning.
Suggestions:
src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py:285: if_resolve_json_pointerreturnsNone, fall back to main's verbatim lookup of the segments, so existing specs keep resolving.- Add the two literal-key cases next to
tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py:183, plus a~0case. The new test covers only~1and%7B.
|
Thanks @1AbdullahButt for testing this and for catching the compatibility gap. I've added a fallback to the previous verbatim lookup when the RFC 6901 lookup finds nothing, so refs that use a key unescaped (like |
Link to Issue or Description of Change
No existing issue; describing the change below.
Problem:
OpenApiSpecParser._resolve_referencesresolves a local$refby splitting it on/and looking up each segment as a dict key. It doesn't decode JSON Pointer escapes (~1for/,~0for~, RFC 6901) or percent-encoding, and a numeric segment never indexes a list. A ref intopaths, which always needs~1, therefore resolves toNone, for example:The raw
$refis left in the spec, so a referenced parameter is silently dropped from the generated tool (thedeleteoperation above gets nopetIdparameter) and a referenced request body fails as an unresolved reference.Solution:
tools/_gemini_schema_util._resolve_json_pointeralready implements RFC 6901 decoding, percent-decoding and array indexing (added for_dereference_schemain #6148).resolve_refnow delegates to it, keeping the existingValueErrorfor external references.Testing Plan
Unit Tests:
New
test_parse_spec_with_escaped_and_indexed_json_pointer_referenceintests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.pyuses the ref above and checks that thedeleteoperation gets thepetIdpath parameter. It fails onmain(assert [] == [('petId', 'path')]) and passes with this change.pyink,isortandruff(pre-commit versions) are clean.Manual End-to-End (E2E) Tests:
Parsing an OpenAPI spec whose operation references another operation's parameter with
#/paths/~1pets~1%7BpetId%7D/get/parameters/0now produces the parameter in the generatedRestApiTool.Checklist
An AI coding assistant helped find this and draft the change; I reviewed it and ran the tests myself.