Skip to content

fix(open): honour the requested type for every document container - #735

Open
andiwand wants to merge 1 commit into
feat/odf-flat-xmlfrom
fix/open-as-exact-type
Open

fix(open): honour the requested type for every document container#735
andiwand wants to merge 1 commit into
feat/odf-flat-xmlfrom
fix/open-as-exact-type

Conversation

@andiwand

Copy link
Copy Markdown
Member

🤖 Generated with Claude Code

Stacked on #731 — review it after that one, and the base will retarget to main when #731 merges.

Why

A local review of #731 flagged that its new flat-odf branch is the only one in open_file_as that checks what it decoded against what was asked for:

open(packaged_odt_bytes, FileType::opendocument_graphics)  // -> a text document
open(flat_odt_bytes,     FileType::opendocument_graphics)  // -> throws

Same document, two encodings, two answers. The looseness is older than #731 — it sits in the odf, ooxml and legacy-MS branches alike — but #731 is what makes it visible, so this fixes all three rather than relaxing the new one. open_file_as already documents itself as decoding "as exactly as", and the probing path in open_file treats a throw as "not this type, try the next", so tightening costs it nothing and makes probing more accurate.

What

  • The odf, ooxml (zip and cfb) and legacy-MS branches check file_type() == as before returning, mirroring what the flat branch does.
  • One exception, deliberate: an encrypted ooxml names no inner type until it is decrypted, so is_the_requested_ooxml lets it answer for whichever type was asked for. html_output_test relies on this for every encrypted docx and xlsx in the reference set — the first cut without it failed exactly those two.

Tests

File.opening_as_the_wrong_document_type_throws covers odt/docx/doc, File.a_flat_document_and_a_package_answer_a_wrong_type_alike pins the asymmetry itself, and File.an_encrypted_ooxml_opens_as_the_type_asked_for pins the exception. All three wrong-type cases were verified to fail against the branch without the fix.

Full suite: 1005 passed, 0 failed (reference output included).

`open(file, as)` returned whatever an odf, ooxml or legacy MS container
turned out to hold, whatever `as` said, while the flat-odf branch added
alongside them checked. So `open(packaged_odt, opendocument_graphics)`
answered with a text document and `open(flat_odt, opendocument_graphics)`
threw - the public contract depended on which encoding of the same
document the caller happened to hold. `open_file_as` documents itself as
decoding "as exactly @p as", and the probing path already treats a throw
as "not this type, try the next", so the check costs it nothing.

An encrypted ooxml is the one reading that is not its own container's: it
names no inner type until it is decrypted, so it still answers for the
type that was asked for - which is what `html_output_test` expects of
every encrypted docx and xlsx in the reference set.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015E5WrxdYxfoBE7JUotBtqh
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.

1 participant