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

Solr.add with datetime instance causes TypeError #317

Open
2 of 3 tasks
o-felixz opened this issue Apr 23, 2020 · 3 comments
Open
2 of 3 tasks

Solr.add with datetime instance causes TypeError #317

o-felixz opened this issue Apr 23, 2020 · 3 comments

Comments

@o-felixz
Copy link

Calling pysolr.Solr.add with a dict containing a datetime object causes the following stack trace:

  File "<>/.local/lib/python3.7/site-packages/pysolr.py", line 1018, in add
    m = json.dumps(message).encode("utf-8")
  File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable

I believe this change to be the breaking change.

Before this change the datetime object could propagate via self._build_doc to self._from_python where the first check would have converted it to a string.

I have

  • Tested with the latest release
  • Tested with the current master branch
  • Searched for similar existing issues

Expected behaviour

No exception

Actual behaviour

TypeError: Object of type datetime is not JSON serializable

Configuration

  • Operating system version: debian 10
  • Search engine version: 8.5.1
  • Python version: 3.7
  • pysolr version: 3.9.0
@roebius
Copy link

roebius commented Jul 16, 2020

I see there was no reply yet to this issue. Is there any plan to solve it or at least any suggested workaround?
Thx

@acdha
Copy link
Collaborator

acdha commented Jul 17, 2020

You can go back to using the XML interface using the optional parameter, call isoformat() on the date objects before passing them in. It would be nice if we had a JSON encoder which handles this, too, if you have time to contribute one.

@roebius
Copy link

roebius commented Jul 17, 2020

Thanks for the suggestion.
I am now just passing datetime(...).isoformat() instead of datetime(...) and everything works fine. I did not try the XML interface.

Regarding the encoder, I will not have the time, unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants