Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Query with nested property that contains spaces causes invalid parameter name #23084

Closed
gogopavl opened this issue Jul 20, 2021 · 4 comments · Fixed by #23341
Closed

[BUG] Query with nested property that contains spaces causes invalid parameter name #23084

gogopavl opened this issue Jul 20, 2021 · 4 comments · Fixed by #23341
Assignees
Labels
azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization.

Comments

@gogopavl
Copy link
Contributor

Describe the bug

Issuing a query using custom Criteria through a CosmosTemplate in cases where the underlying document has a nested key with spaces results in an error because of the way the query parameter name is generated.

The library, as is, supports the execution of queries with nested properties, but only with the use of the dot notation (e.g. c.someCollection.someKey.value). When the nested property has spaces, though, this will break, which is expected (for instance, c.someCollection.key with spaces.value).

The docs here mention that in such cases the ["..."] notation should be used (or ['']), which I tried, but results in an error because of how the query parameter name is rendered.

Exception or Stack Trace

Getting java.lang.reflect.UndeclaredThrowableException when catching the exception , but after a bit of digging found that the underlying error is the following:

{
  "Errors": [
    "Invalid query. Specified parameter name '@someCollection['key with spaces']['value']1ac16075_dd7a_4eab_bb2d_287dabbd9b7a' is invalid. Parameter names should be in the format of symbol '@' followed by a valid identifier. E.g. @param1"
  ]
}

Which is part of

{
  "ClassName": "CosmosException",
  "userAgent": "azsdk-java-cosmos/4.13.0 MacOSX/10.15.7 JRE/11.0.5",
  "statusCode": 400,
  "resourceAddress": "...",
  "error": "{\"code\":\"BadRequest\",\"message\":\"Gateway Failed to Retrieve Query Plan: Message: {\\\"Errors\\\":[\\\"Invalid query. Specified parameter name '@someCollection['key with spaces']['value']1ac16075_dd7a_4eab_bb2d_287dabbd9b7a' is invalid. Parameter names should be in the format of symbol '@' followed by a valid identifier. E.g. @param1\\\"]}\\r\\nActivityId: 50810215-87ed-4b30-b684-d5764efdaf66, Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Documents.Common/2.14.0, StatusCode: BadRequest\",\"additionalErrorInfo\":null}",
  "innerErrorMessage": "Gateway Failed to Retrieve Query Plan: Message: {\"Errors\":[\"Invalid query. Specified parameter name '@someCollection['key with spaces']['value']1ac16075_dd7a_4eab_bb2d_287dabbd9b7a' is invalid. Parameter names should be in the format of symbol '@' followed by a valid identifier. E.g. @param1\"]}\r\nActivityId: 50810215-87ed-4b30-b684-d5764efdaf66, Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Documents.Common/2.14.0, StatusCode: BadRequest",
  "causeInfo": null,
  "responseHeaders": "{Transfer-Encoding=chunked, Strict-Transport-Security=max-age=31536000, Server=Microsoft-HTTPAPI/2.0, x-ms-gatewayversion=version=2.14.0, Date=Fri, 16 Jul 2021 16:04:10 GMT, x-ms-activity-id=50810215-87ed-4b30-b684-d5764efdaf66, Content-Type=application/json}",
  "requestHeaders": "[Accept=application/json, x-ms-cosmos-is-query-plan-request=True, x-ms-date=Fri, 16 Jul 2021 16:04:09 GMT, x-ms-cosmos-supported-query-features=Aggregate, CompositeAggregate, MultipleOrderBy, MultipleAggregates, OrderBy, OffsetAndLimit, Distinct, GroupBy, Top, NonValueAggregate, x-ms-cosmos-query-version=1.0, Content-Type=application/query+json]",
  "cosmosDiagnostics": {}
}

PS Redacted some info from the Cosmos Exception payload to not expose sensitive information. If the whole response is required along with the cosmos diagnostics let me know & I'll provide in a private channel.

To Reproduce

  1. Set up a cosmos-managed database and a container which holds JSON documents that have nested keys with spaces/special characters - example shown in the Code Snippet section right below.
  2. From a Java-based application that uses the azure-spring-data-cosmos-3.8.0 library, use Criteria to formulate and issue a query with an equality CriteriaType via a CosmosTemplate.
  3. When the subject of the enforced Criteria contains square brackets to specify nested keys with spaces (["..."] or ['...']) then the query results in an error due to the illegal generated parameter name (library code).

Code Snippet

  • Data
    • Java class (constructors, accessors, mutators etc. excluded for brevity reasons)
      ...
      @Container(containerName = "...", ru = RECEIPT_RU_LIMIT, autoScale = true, timeToLive = RECEIPT_TIME_TO_LIVE)
      public class CosmosReceiptDocument {
          
          @Id
          private String someId;
      
          @PartitionKey
          private String somePartitionKey;
          
          private Map<String, SomeValue> someCollection;
      
          public static class SomeValue {
      
              private String value;
          }
      }
      
    • JSON
      {
          "someId": "document-id",
          "somePartitionKey": "document-partition-key",
          "someCollection": {
              "key with spaces": {
                  "value": "some value"
              },
              "keyWithoutSpaces": {
                  "value": "some other value"
              }
          }
      }
      
  • Code
    public Slice<CosmosReceiptDocument> getReceiptsByCriteria() {
    
        String subject = String.format("%s['%s']['%s']",
                "someCollection", "key with spaces", "value");
    
        /*  Same behaviour as above
            String alternativeSubject = String.format("%s[\"%s\"][\"%s\"]",
                    "someCollection", "key with spaces", "value"); */
    
        /*  This will work but only for nested keys that don't have spaces, otherwise breaks due to syntax error
            String subjectThatWorks = String.format("%s.%s.%s",
                    "someCollection", "keyWithoutSpaces", "value"); */
    
        Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, subject,
                Collections.singletonList("some value"), Part.IgnoreCaseType.NEVER);
    
        Pageable pageRequest = CosmosPageRequest.of(0, 20, null, Sort.unsorted());
        CosmosQuery query = new CosmosQuery(criteria).with(pageRequest);
    
        return cosmosTemplate.sliceQuery(query, CosmosReceiptDocument.class, Constants.CONSENT_RECEIPTS);
    }
    

Expected behavior

When querying the container for documents that match the specified Criteria for the nested properties that may contain spaces I would expect that the query isn't flagged because of the generated parameter name. As this is based on the SQL API, my expectation is that this should work using the Java SDK just as it does from data explorer.

Screenshots

N/A

Setup (please complete the following information):

  • OS: macOS Catalina V 10.15.7 (19H114)
  • IDE: IntelliJ IDEA 2021.1.2 (Community Edition)
  • Library/Libraries: azure-spring-data-cosmos-3.8.0.jar

Additional context

I came across an older bug (has been resolved) that is similar here: issue 221

Information Checklist

  • [✅] Bug Description Added
  • [✅] Repro Steps Added
  • [✅] Setup information Added
@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jul 20, 2021
@joshfree joshfree added azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library. and removed question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jul 20, 2021
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Jul 20, 2021
@joshfree
Copy link
Member

@backwind1233 could you please follow up with @gogopavl?

@kushagraThapar
Copy link
Member

kushagraThapar commented Jul 20, 2021

I will be working with @gogopavl on this issue, assigning myself to it.

@kushagraThapar kushagraThapar self-assigned this Jul 20, 2021
gogopavl pushed a commit to gogopavl/azure-sdk-for-java that referenced this issue Aug 4, 2021
Replace any non-alphanumeric characters in the generated query parameter name with an underscore
so that criteria with references to nested properties such as user['first name'] do not result
in a bad request due to invalid query parameter name.

Resolves Azure#23084
gogopavl added a commit to gogopavl/azure-sdk-for-java that referenced this issue Aug 4, 2021
Replace any non-alphanumeric characters in the generated query parameter name with an underscore
so that criteria with references to nested properties such as user['first name'] do not result
in a bad request due to invalid query parameter name.

Resolves Azure#23084
gogopavl added a commit to gogopavl/azure-sdk-for-java that referenced this issue Aug 4, 2021
Replace any non-alphanumeric characters in the generated query parameter name with an underscore
so that criteria with references to nested properties such as user['first name'] do not result
in a bad request due to invalid query parameter name.

Resolves Azure#23084
@gogopavl
Copy link
Contributor Author

gogopavl commented Aug 4, 2021

@kushagraThapar issued a PR that should resolve this bug - #23341

@kushagraThapar
Copy link
Member

@gogopavl - thank you for your contribution, I will review it soon.

gogopavl added a commit to gogopavl/azure-sdk-for-java that referenced this issue Aug 13, 2021
Assert the change in the behaviour of the query parameter name generator by adding more tests
using the reactive cosmos template

References Azure#23084
gogopavl added a commit to gogopavl/azure-sdk-for-java that referenced this issue Aug 20, 2021
Assert the change in the behaviour of the query parameter name generator by adding more tests
using the reactive cosmos template

References Azure#23084
kushagraThapar pushed a commit that referenced this issue Aug 23, 2021
* fix query parameter name generator in azure-spring-data-cosmos

Replace any non-alphanumeric characters in the generated query parameter name with an underscore
so that criteria with references to nested properties such as user['first name'] do not result
in a bad request due to invalid query parameter name.

Resolves #23084

* add test cases for parameter name generator using async API

Assert the change in the behaviour of the query parameter name generator by adding more tests
using the reactive cosmos template

References #23084
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants