Skip to content

fix(tools): resolve escaped and indexed JSON Pointer refs in OpenAPI specs - #8430

Open
omkarbandikatte wants to merge 2 commits into
google:mainfrom
omkarbandikatte:fix/openapi-json-pointer-refs
Open

omkarbandikatte wants to merge 2 commits into
google:mainfrom
omkarbandikatte:fix/openapi-json-pointer-refs

Conversation

@omkarbandikatte

Copy link
Copy Markdown

Link to Issue or Description of Change

No existing issue; describing the change below.

Problem:

OpenApiSpecParser._resolve_references resolves a local $ref by splitting it on / and looking up each segment as a dict key. It doesn't decode JSON Pointer escapes (~1 for /, ~0 for ~, RFC 6901) or percent-encoding, and a numeric segment never indexes a list. A ref into paths, which always needs ~1, therefore resolves to None, for example:

delete:
  parameters:
    - $ref: "#/paths/~1pets~1%7BpetId%7D/get/parameters/0"

The raw $ref is left in the spec, so a referenced parameter is silently dropped from the generated tool (the delete operation above gets no petId parameter) and a referenced request body fails as an unresolved reference.

Solution:

tools/_gemini_schema_util._resolve_json_pointer already implements RFC 6901 decoding, percent-decoding and array indexing (added for _dereference_schema in #6148). resolve_ref now delegates to it, keeping the existing ValueError for external references.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New test_parse_spec_with_escaped_and_indexed_json_pointer_reference in tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py uses the ref above and checks that the delete operation gets the petId path parameter. It fails on main (assert [] == [('petId', 'path')]) and passes with this change.

$ pytest tests/unittests/tools/openapi_tool/
436 passed

pyink, isort and ruff (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/0 now produces the parameter in the generated RestApiTool.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

An AI coding assistant helped find this and draft the change; I reviewed it and ran the tests myself.

@1AbdullahButt 1AbdullahButt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_pointer returns None, 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 ~0 case. The new test covers only ~1 and %7B.

@omkarbandikatte

Copy link
Copy Markdown
Author

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 My%20Schema or a~1b) still resolve, and added a parametrized test for the ~0, literal %20 and literal ~1 cases. The OpenAPI tool tests pass (439).

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.

2 participants