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:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Fix has been merged, will be available in this month’s release.
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!