Skip to content

fix(tools): build RestApiTool bodies from the parser's normalized schema - #7839

Open
adityasingh2400 wants to merge 1 commit into
google:mainfrom
adityasingh2400:fix/rest-api-tool-schemaless-body
Open

adityasingh2400 wants to merge 1 commit into
google:mainfrom
adityasingh2400:fix/rest-api-tool-schemaless-body

Conversation

@adityasingh2400

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
OperationParser normalizes each request-body media type's schema with _schema_from_openapi, so a media type declared without a schema (e.g. application/octet-stream: {}) becomes Schema() and gets a single body parameter. RestApiTool._prepare_request_params read media_type_object.schema_ raw and called .type on it. A schema-less body therefore produced a tool that crashed with AttributeError: 'NoneType' object has no attribute 'type' on every call.

Solution:
Normalize the schema in _prepare_request_params with the same _schema_from_openapi call the parser makes, so the request is built from the schema the parameters were derived from. Object bodies and other schemas behave as before. The two mypy errors on that line (Item "None"/"Reference" of "Schema | Reference | Any | None" has no attribute "type") go away, and no new ones appear.

Testing Plan

Unit Tests:

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

Added test_prepare_request_params_media_type_without_schema to test_rest_api_tool.py. It builds the parameters with OperationParser, as OpenAPIToolset does, and asserts that the bytes are sent as data with Content-Type: application/octet-stream.

# On main (before the fix)
AttributeError: 'NoneType' object has no attribute 'type'

# With the fix
$ pytest tests/unittests/tools/openapi_tool
433 passed

pre-commit run passes on the changed files. mypy on rest_api_tool.py goes from 25 errors on main to 23; the two removed are the union-attr errors on this line.

Manual End-to-End (E2E) Tests:

Reproduced through the parser-built parameters, the same path OpenAPIToolset uses:

params = OperationParser(op).get_parameters()   -> [('body', 'body', None)]
tool._prepare_request_params(params, {"body": b"%PDF-1.7"})
main:    AttributeError: 'NoneType' object has no attribute 'type'
this PR: data=b'%PDF-1.7', Content-Type: application/octet-stream

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.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

None.

OperationParser normalizes a request-body media type that has no schema
(e.g. application/octet-stream: {}) to an empty Schema and exposes one
body parameter, but _prepare_request_params read the raw schema_ and
raised AttributeError on None. Normalize it the same way so the generated
tool can actually be called. This also clears the two mypy union-attr
errors on that line.

Fixes google#7838
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.

RestApiTool crashes on a request body media type that has no schema

2 participants