fix(958): base64-encode binary map data in /api/protmap/ - #959
Open
claude-im wants to merge 1 commit into
Open
Conversation
SiteObservation.event_file is a binary electron-density map, but ProtMapInfoSerializer.get_map_data read it with open(..., encoding='utf-8').read(). On real map data this raised UnicodeDecodeError on the first non-UTF-8 byte and surfaced as a 500 on /api/protmap/ (the production traceback failed on byte 0x9c). Read the file as bytes and return it base64-encoded so the JSON response is valid and lossless; clients base64-decode to recover the raw map. Also use a context manager so the file handle is always closed. Add a regression test that exercises get_map_data with binary content (including 0x9c) and asserts the result round-trips, plus the no-file case. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #958.
Problem
/api/protmap/returns 500 in production.SiteObservation.event_fileis a binary electron-density map, butProtMapInfoSerializer.get_map_dataread it as UTF-8 text:On real map data this raises
UnicodeDecodeErroron the first non-UTF-8 byte (the production traceback failed on0x9c). The field was historically a textmap_info; when it became the binaryevent_filethe text read was never updated (an in-codeTODOalready flagged "it's binary and not read").Fix
Read the file as bytes and return it base64-encoded, so the JSON response is valid and lossless; clients base64-decode to recover the raw map. Also switched to a context manager so the file handle is always closed.
map_datanow contains base64, not raw text. This is unavoidable for binary content in a JSON field — and the endpoint currently always 500s on real maps, so there is no working behaviour to preserve. Any consumer of/api/protmap/must base64-decodemap_data. The sibling text endpoints (/api/protpdb/,/api/protpdbbound/) are unchanged — PDB files are ASCII text and read fine.Testing
New regression test
viewer/tests/test_protmap_serialization.py(written first, TDD):0x9cis base64-encoded and round-trips (it reproduced the exact productionUnicodeDecodeErrorbefore the fix);event_filecase still returnsNone.Full suite: 102 passed, 1 skipped; pre-commit (isort/black/mypy/pylint) clean.
🤖 Generated with Claude Code