Skip to content

Commit

Permalink
JSONfeed: validate the presence of content_html or content_text
Browse files Browse the repository at this point in the history
The spec says that at least one element is mandatory. Verify this and
raise an exception if both are missing.

Signed-off-by: Beat Bolli <[email protected]>
  • Loading branch information
bbolli committed Apr 22, 2024
1 parent 97c648c commit 6a68b8d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 5 additions & 3 deletions feedparser/parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def parse_entry(self, e):
if src in e:
entry[dst] = e[src]

content = []
entry["content"] = content = []
if "content_html" in e:
c = FeedParserDict()
c["value"] = sanitize_html(
Expand All @@ -98,8 +98,10 @@ def parse_entry(self, e):
c["value"] = e["content_text"]
c["type"] = "text"
content.append(c)
if content:
entry["content"] = content
if not content:
raise ValueError(
f"item {entry['id']=} has neither 'content_text' nor 'content_html'"
)

if "date_published" in e:
entry["published"] = e["date_published"]
Expand Down
17 changes: 17 additions & 0 deletions tests/json/no_content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"__TEST__": "Description: basic JSON tests Expect: bozo and 'neither' in str(bozo_exception) -->",
"version": "https://jsonfeed.org/version/1",
"title": "no content",
"home_page_url": "https://example.org/",
"feed_url": "https://example.org/feed.json",
"icon": "https://example.org/feed.png",
"author": { "name": "me" },
"items": [
{
"id": "1",
"author": { "name": "you", "url": "http://example.net/~you" },
"summary": "Hello, world!\n",
"url": "https://example.org/initial-post"
}
]
}

0 comments on commit 6a68b8d

Please sign in to comment.