Skip to content

Conversation

pinzon
Copy link
Member

@pinzon pinzon commented Aug 29, 2025

Motivation

I encountered this issue when writing an EKS/CFn test during a deletion of Stack:

Traceback (most recent call last):
  File "/home/cristopher/Work/ls_workspace/localstack-pro/.venv/lib/python3.11/site-packages/localstack_snapshot/snapshots/prototype.py", line 121, in _persist_state
    state_to_dump = json.dumps(full_state, indent=2)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cristopher/.pyenv/versions/3.11.12/lib/python3.11/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "/home/cristopher/.pyenv/versions/3.11.12/lib/python3.11/json/encoder.py", line 202, in encode
    chunks = list(chunks)
             ^^^^^^^^^^^^
  File "/home/cristopher/.pyenv/versions/3.11.12/lib/python3.11/json/encoder.py", line 432, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/home/cristopher/.pyenv/versions/3.11.12/lib/python3.11/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "/home/cristopher/.pyenv/versions/3.11.12/lib/python3.11/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "/home/cristopher/.pyenv/versions/3.11.12/lib/python3.11/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  [Previous line repeated 2 more times]
  File "/home/cristopher/.pyenv/versions/3.11.12/lib/python3.11/json/encoder.py", line 439, in _iterencode
    o = _default(o)
        ^^^^^^^^^^^
  File "/home/cristopher/.pyenv/versions/3.11.12/lib/python3.11/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable

So I fixed locally. Is there a particular reason the CustomJsonEncoder is not used in _persis_state?

@pinzon pinzon requested a review from tiurin August 29, 2025 15:45
@simonrw
Copy link

simonrw commented Aug 29, 2025

Presumably this is post transformation, which implies we have missed transforming a datetime that we probably should, right? We should not let datetimes (even serialized ones) into our snapshots IMO

Or is this an issue when saving the raw snapshot?

@pinzon
Copy link
Member Author

pinzon commented Aug 29, 2025

The _persist_raw operation uses the encoder. So it seems strange to me that the _persist_state doesn't use it.

@simonrw
Copy link

simonrw commented Aug 29, 2025

The _persist_raw operation uses the encoder. So it seems strange to me that the _persist_state doesn't use it.

Because the _persist_state method is called after transformation right? so values are transformed to be primitives (including datetimes which are not only transformed to strings to remove the need for a custom encoder, but to set their values to a known constant so we can compare the LocalStack implementation).

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

Successfully merging this pull request may close these issues.

2 participants