microsoftgraph / microsoftgraph/msgraph-sdk-python-core

BatchRequestItem loses query parameters from RequestInformation

Open
#1,035 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status:waiting-for-triage type:bug
Dominant language
Python
Stars
288
Forks
52
Avg merge
8h 10m
Merged PRs (30d)
1

Description

Describe the bug

I'm trying to peform a batched GET request with additional query parameters to select specific fields on a response.
The resulting BatchRequestItem is not aware of query parameters and will result in the request being sent without them.
After some debugging I figured out that the underlying problem is the way RequestInformation works.
It's unclear whether this is by design, at least it's very easy to use it incorrectly.

RequestInformation has a public property query_parameters. It also has a url property with getter and setter methods, which deal with __uri internally.
After setting the url property, which results in __uri being truthy, query_parameters will not be evaluated anymore in the url getter, which is used e.g. by BatchRequestItem.

It's unclear whether this is an issue with BatchRequestItem copying the parameters from RequestInformation or whether RequestInformation should include them in the url getter, in which case this issue should be moved to the repo for the microsoft-kiota-abstractions package.

Expected behavior

Query parameters included in the batch body.

How to reproduce
import asyncio
from azure.identity import AzureCliCredential
from msgraph import GraphServiceClient
from kiota_abstractions.request_information import RequestInformation
from kiota_abstractions.method import Method
from msgraph_core.requests.batch_request_item import BatchRequestItem
from msgraph_core.requests.batch_request_content import BatchRequestContent


async def main():
    credential = AzureCliCredential()
    client = GraphServiceClient(credentials=credential, scopes=["https://graph.microsoft.com/.default"])

    request_info = RequestInformation()
    request_info.http_method = Method.GET
    request_info.url = "/me"

    request_info.query_parameters = {
        "$select": ",".join([
            "id",
            "userPrincipalName",
            "displayName",
            "accountEnabled",
        ])
    }

    batch_item = BatchRequestItem(request_information=request_info)
    batch_content = BatchRequestContent({batch_item.id: batch_item})
    batch_response = await client.batch.post(
        batch_request_content=batch_content,
    )
    print(repr(batch_response.responses.get(batch_item.id).body))


asyncio.run(main())
SDK Version

1.3.8

Latest version known to work for scenario above?

No response

Known Workarounds

Append query parameters to the URL directly, e.g.

    query_parameters = {
        "$select": ",".join([
            "id",
            "userPrincipalName",
            "displayName",
            "accountEnabled",
        ])
    }
    request_info.url = "/me?" + urllib.parse.urlencode(query_parameters)
Debug output

N/A

Configuration

No response

Other information

The constructor for BatchRequestItem only takes the URL from the provided RequestInformation, but it doesn't take into account that there may be query parameters, so they're just ignored.

https://github.com/microsoftgraph/msgraph-sdk-python-core/blob/v1.3.8/src/msgraph_core/requests/batch_request_item.py#L41-L53

The getter for url on RequestInformation skips any further processing if __url is already truthy: https://github.com/microsoft/kiota-python/blob/microsoft-kiota-abstractions-v1.9.7/packages/abstractions/kiota_abstractions/request_information.py#L97-L102

The setter for url copies the provided URL to __url: https://github.com/microsoft/kiota-python/blob/microsoft-kiota-abstractions-v1.9.7/packages/abstractions/kiota_abstractions/request_information.py#L123-L130

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/msgraph_core/requests/batch_request_item.py at the constructor lines linked in the issue, then compare how RequestInformation.url and query_parameters behave in the referenced kiota-python file. Run the supplied reproduction to confirm the batch body omits the parameters; done means the query parameters appear in the batch request body.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.