question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cosmos replace-functions (e.g. store procedures) do not work with "serverScript" key in the SPROC dict

See original GitHub issue
  • Package Name: azure-cosmos
  • Package Version: At least 4.2.0, probably all other versions
  • Operating System: Windows, but not OS specific
  • Python Version: 3.9.5 but not version specific

Describe the bug When replacing a stored procedure with the dictionary {"id": SPROC_NAME, "serverScript": SPROC_CONTENT}, there is a CosmosHttpResponseError. When using {"id": SPROC_NAME, "body": SPROC_CONTENT}, it works fine.

To Reproduce Steps to reproduce the behavior:

client = cosmos.CosmosClient("URI", credential="KEY")
database = client.get_database_client("DB_NAME")
container = database.get_container_client("CONTAINER_NAME")

sproc = {"id": SPROC_NAME, "serverScript": SPROC_CONTENT}
# this works
container.scripts.create_stored_procedure(body=sproc)

# this fails due to CosmosHttpResponseError
container.scripts.replace_stored_procedure(sproc=SPROC_NAME, body=sproc)

CosmosHttpResponseError: (BadRequest) Message: {"Errors":["The input content is invalid - 'serverScript' is not a valid property in the current payload.","The request payload is invalid. Ensure to provide a valid request payload."]}

The problem is that replacing and creating stuff (UDF, Trigger, SPROC) have different behaviours with regard to the “serverScript” key in azure\cosmos\_cosmos_client_connection.py:

  • When creating, the key “serverScipt” is popped and mapped:
        if sproc.get("serverScript"):
            sproc["body"] = str(sproc.pop("serverScript", ""))
        elif sproc.get("body"):
            sproc["body"] = str(sproc["body"])
  • When replacing, the key “serverScipt” is mapped but stays in the dictionary:
        if sproc.get("serverScript"):
            sproc["body"] = str(sproc["serverScript"])
        elif sproc.get("body"):
            sproc["body"] = str(sproc["body"])

This leads to the described error.

Expected behavior The stored procedures should also work with the serverScript key in the dict. It should also use the pop statement.

Screenshots The problem is described and a solution provided.

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
simorenohcommented, Feb 16, 2022

Fix has been merged, will be available in this month’s release.

1reaction
simorenohcommented, Feb 15, 2022

Hi @mxkus, thank you for bringing this up! It seems you are correct, by passing the serverScript still in the kwargs without popping it we are providing the backend with an unexpected body content. I’ll make a PR to address this issue, it should be getting publicly released in this month’s release. Will keep you posted on this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Write stored procedures, triggers, and UDFs in Azure Cosmos ...
Learn how to define stored procedures, triggers, and user-defined functions in Azure Cosmos DB.
Read more >
azure.cosmos package — Azure SDK for Python 2.0.0 ... - NET
A database contains one or more containers, each of which can contain items, stored procedures, triggers, and user-defined functions.
Read more >
Partition key is not working while inserting in Azure Cosmos DB
I am trying to insert documents in Azure CosmosDB collection using stored procedure. Earlier I was not using partition key and this code...
Read more >
pydocumentdb 2.3.3 - manpages.ubuntu!
Return type dict pydocumentdb.base module Base functions in the Azure Cosmos DB ... Parameters · sproc_link (str) -- The link to the stored...
Read more >
Using Stored Procedure in Azure Data Factory
If I hit that Preview Data button, it actually won't work because I don't have a table in my database named "Passing". The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found