Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# SPDX-License-Identifier: Apache-2.0

import json

from http import HTTPStatus

import pytest

from tika import parser
from tika.parser import _parse


def test_remote_pdf():
Expand Down Expand Up @@ -43,3 +48,25 @@ def test_local_path(test_file_path):
"""parse file path"""
assert parser.from_file(str(test_file_path))


@pytest.mark.parametrize(
"first,second,expected",
[
("a", "b", ["a", "b"]),
("a", ["b", "c"], ["a", "b", "c"]),
(["a", "b"], "c", ["a", "b", "c"]),
(["a", "b"], ["c", "d"], ["a", "b", "c", "d"]),
],
ids=["scalar+scalar", "scalar+list", "list+scalar", "list+list"],
)
def test_metadata_merge_across_embedded_objects(first, second, expected):
"""When the same metadata key appears in multiple embedded objects (nested
PDFs, archives, ...), values must be merged into a single flat list rather
than appended as nested lists."""
rmeta = [
{"X-TIKA:content": "first", "key": first},
{"X-TIKA:content": "second", "key": second},
]
result = _parse((HTTPStatus.OK, json.dumps(rmeta)))
assert result["metadata"]["key"] == expected

14 changes: 11 additions & 3 deletions tika/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,17 @@ def _parse(output, service='all'):
for n in js:
if n != "X-TIKA:content":
if n in parsed["metadata"]:
if not isinstance(parsed["metadata"][n], list):
parsed["metadata"][n] = [parsed["metadata"][n]]
parsed["metadata"][n].append(js[n])
existing = parsed["metadata"][n]
new_val = js[n]
if isinstance(new_val, list):
if isinstance(existing, list):
existing.extend(new_val)
else:
parsed["metadata"][n] = [existing] + new_val
elif isinstance(existing, list):
existing.append(new_val)
else:
parsed["metadata"][n] = [existing, new_val]
else:
parsed["metadata"][n] = js[n]

Expand Down