#[non_exhaustive]pub enum DuplicateKeyPolicy {
Refuse,
KeepLast,
KeepFirst,
}Expand description
What to do when one dictionary names the same key twice (§7.3.7).
§The file that forced this, and why the strict answer was too expensive
An operator’s 46 KB drawing opens in Acrobat and was refused whole by
pdfcer: its document catalog carries /PageMode twice, with different
values —
… /PageMode /UseOC /PageLayout /SinglePage /PageLabels << … >>
/PageMode /UseOutlines /Outlines 58 0 R /Metadata 59 0 R >>One repeated key in one object cost the whole document, on a file every other reader opens without comment. That is the wrong trade for a malformed entry in an otherwise sound file: pdfcer’s fail-clean posture is about never silently doing the wrong thing, and a counted, disclosed choice is not silent.
§Why this is a POLICY and not simply a relaxation
The spec’s own rule and pdfcer’s obligation are different questions. The
rule is recorded at ParseErrorKind::DuplicateDictKey; what a reader may
do about a violation is not stated, which makes the winner pdfcer’s
choice to make and to disclose — this project’s standing shape for a spec
ambiguity (a setting, a documented default, never a silent decision).
Self::Refuse stays the type’s Default so every caller that did not
ask — fuzz targets, the recovery confirmation pass, any future one — keeps
the strict behaviour it was written against. The document loader opts
in; the parser does not opt in for it.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Refuse
Refuse the object with ParseErrorKind::DuplicateDictKey.
The default, and the right answer for a caller whose job is to decide whether a file is well-formed rather than to open it.
KeepLast
Keep the last occurrence, and count it.
§Why last rather than first
Nothing in the standard prescribes either — see
ParseErrorKind::DuplicateDictKey for the sourcing — so this is
pdfcer’s choice, and it is made on observed reader behaviour:
qpdf, pdf.js and pdfium all keep the last occurrence, and none of
the three refuses the file. qpdf even says so out loud — “dictionary
has duplicated key <key>; last occurrence overrides earlier ones” —
which is the same disclosure pdfcer now makes.
CORRECTED BEFORE SHIPPING. This paragraph first argued from §7.5.6 — “every other override-by-repetition in PDF is last-wins; an incremental update’s newer definition supersedes the older one” — and that is an analogy dressed as a citation. §7.5.6 orders objects across incremental updates, an ordering the standard deliberately makes meaningful; §7.3.7’s own preceding sentence says the ordering of a dictionary’s entries “shall be ignored”. Citing it here would have borrowed authority from a clause that says the opposite thing about this exact structure.
The distinction is not pedantry: ISO’s one resolved duplicate-key erratum (#3, inline images) picked the winner by content, and its submission rejected first/last positional logic by name, for precisely that reason.
The alternative is defensible and is not silently unavailable: a
caller wanting the first value has Self::KeepFirst, and every
decision is recorded with BOTH values so a shell can offer the operator
the swap. What is NOT on offer is pdfcer choosing quietly.
KeepFirst
Keep the first occurrence, and count it.
The other half of the operator’s choice. A shell that shows “this file
names /PageMode twice — UseOC or UseOutlines?” re-loads under
this policy when the operator picks the earlier value; nothing else in
the document changes.
Not the default only because Self::KeepLast matches every other
override-by-repetition in PDF; on the evidence available neither is
wrong, which is exactly why both ship.