Azure / azure-webjobs-sdk-extensions

Azure WebJobs SDK Extensions
MIT License
344 stars 206 forks source link

Invalid template expression when using CosmosDb input #601

Open TechWatching opened 5 years ago

TechWatching commented 5 years ago

I have an Azure Function triggered by ServiceBus, with a CosmosDB input binding whose sqlQuery use a property of the ServiceBus message to retrieve a document. Unfortunately it does not work, I receive an exception when launching my Azure Function : Microsoft.Azure.WebJobs.Host: Error indexing method 'SaveFunction'. Microsoft.Azure.WebJobs.Host: Invalid template 'SELECT top 1 * FROM c where c.MyProperty = {UserProperties["MyProperty"]}'. Invalid template expression 'UserProperties["MyProperty"].

Repro steps

  1. Create an Azure Function triggered by ServiceBus with an CosmosDb input binding that uses in its SqlQuery a UserProperty of the ServiceBus Message (see sample code at the end).

  2. Run the Azure Function

Expected behavior

The Azure Function should be indexed correctly, without error

Actual behavior

I receive this exception : Microsoft.Azure.WebJobs.Host: Error indexing method 'SaveFunction'. Microsoft.Azure.WebJobs.Host: Invalid template 'SELECT top 1 * FROM c where c.MyProperty = {UserProperties["MyProperty"]}'. Invalid template expression 'UserProperties["MyProperty"].

Related information

The source code look like that

 public async Task Save([ServiceBusTrigger("myServiceBus", Connection = "MyServiceBusConnectionString")] Message message,
            [CosmosDB(databaseName: "MyCosmosDb",
                collectionName: "MyCollection",
                ConnectionStringSetting = "MyCosmosDbConnectionString",
                SqlQuery = "SELECT top 1 * FROM c where c.MyProperty = {UserProperties[\"MyProperty\"]}"
            )] IEnumerable<MyDocument>documents,
            ILogger log)
        {
           ...
        }
renatoeufe commented 3 years ago

Same issue here ...

v-anvari commented 3 years ago

Able to reproduce the error. Triaging the issue for further investigation

@ealsur , @brettsam , Is the below code the right syntax to use with cosmosDb. Can you kindly share your inputs here

public async Task Save([ServiceBusTrigger("myServiceBus", Connection = "MyServiceBusConnectionString")] Message message, [CosmosDB(databaseName: "MyCosmosDb", collectionName: "MyCollection", ConnectionStringSetting = "MyCosmosDbConnectionString", SqlQuery = "SELECT top 1 * FROM c where c.MyProperty = {UserProperties[\"MyProperty\"]}" )] IEnumerabledocuments, ILogger log) { ... }

ealsur commented 3 years ago

I am not familiar with the binding expressions, from the Cosmos DB side, the binding should result in a string, but this error seems to be coming from the binding expression parsing, not particular to Cosmos DB.

v-anvari commented 3 years ago

Hi @ealsur , Thank you for your inputs. Can you tag the right folks who could help us here. Is there any particular repo this issue can be transferred to

ealsur commented 3 years ago

@v-anvari the repo I believe is correct, but I wouldn't know who would be the owners of the binding mapping. From reading the code, I see that it happens here: https://github.com/Azure/azure-webjobs-sdk-extensions/blob/dev/src/WebJobs.Extensions.CosmosDB/CosmosDBSqlResolutionPolicy.cs

But this error does not happen there, this is a generic validation that says that the Binding Expression is incorrect, the error does not seem to come from the code in this repo, or at least I don't see anywhere where that particular error message is generated. @brettsam do you happen to know the source?