RajSoundar
RajSoundar

Reputation: 341

ErrorSchemaValidation upon multiple filter queries on email subject (due to "The maximum read depth (32) has been exceeded")

I have a need to filter out emails with subjects that contain any of the several strings in a list I configure. I try building my query as follows with several Q expressions since there is no support currently (last I checked) for querying like subject__in=["somestring1", "somestring2", ... "somestring25"].

list_of_subject_filter_string_patterns = ["somestring1", "somestring2", ... "somestring25"]
filtered_items = my_exchange_account.inbox.filter(**kwargs)
query_sections = (Q(subject__icontains=pattern) for pattern in list_of_subject_filter_string_patterns)
query = reduce(operator.or_, query_sections)
filtered_items = filtered_items.filter(~query)

if filtered_items.exists():
    # do something
    ....

However this explodes when I pass a bit longer list of patterns in that Q building.

File "/usr/local/lib/python3.6/site-packages/exchangelib/services/common.py", line 329, in _raise_soap_errors
    raise vars(errors)[code](msg) 
ErrorSchemaValidation: The request failed schema validation. The maximum read depth (32) has been exceeded because XML data being read has more levels of nesting than is allowed by the quota. This quota may be increased by changing the MaxDepth property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 499.

Any thoughts on how I can achieve my intended query outcome of filtering out multiple subject patterns?

Upvotes: 0

Views: 92

Answers (1)

Erik Cederstrand
Erik Cederstrand

Reputation: 10245

I don't think your reduction does what you intended it to. reduce() will probably produces something like (((("somestring1" OR "somestring2") OR "somestring3") OR "somestring4") OR "somestring5) ... which is too much for the Exchange server. You can enable debug logging to check the exact format of the XML.

If you want an exact match on the strings, exchangelib does support subject__in=[...]. See https://ecederstrand.github.io/exchangelib/#searching

Upvotes: 1

Related Questions