Skip to content

Commit 6d0f3e1

Browse files
authored
fix: accept string-form version in posting_json_metadata (#386)
1 parent dbe717d commit 6d0f3e1

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

hive/utils/account.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ def safe_profile_metadata(account):
1111
# read from posting_json_metadata, if version==2
1212
prof = json.loads(account['posting_json_metadata'])['profile']
1313
assert isinstance(prof, dict)
14-
assert 'version' in prof and prof['version'] == 2
14+
# The "version" field is serialized inconsistently across Steem clients:
15+
# the official condenser writes a JSON number (2), but some third-party
16+
# clients write a JSON string ("2"). Accept both forms so that accounts
17+
# created/updated by third-party clients are not silently dropped to
18+
# empty profile data.
19+
# TODO: when a new profile version is introduced, standardize on a single
20+
# canonical type (JSON number) and migrate legacy string-form accounts.
21+
assert 'version' in prof and prof['version'] in (2, '2')
1522
except Exception:
1623
try:
1724
# fallback to json_metadata

tests/utils/test_utils_account.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@ def test_valid_account():
1919
for key, safe_value in safe_profile.items():
2020
assert raw_profile[key] == safe_value
2121

22+
def test_string_version_account():
23+
# Some third-party Steem clients serialize "version" as a JSON string ("2")
24+
# instead of a number (2). Such accounts should still be parsed correctly.
25+
raw_profile = dict(
26+
name='Test User',
27+
about='Hello world',
28+
location='Earth',
29+
website='https://example.com/',
30+
cover_image='https://example.com/cover.jpg',
31+
profile_image='https://example.com/avatar.jpg',
32+
version='2',
33+
)
34+
account = {'name': 'foo', 'json_metadata': '{}',
35+
'posting_json_metadata': json.dumps(dict(profile=raw_profile))}
36+
37+
safe_profile = safe_profile_metadata(account)
38+
assert safe_profile['name'] == 'Test User'
39+
assert safe_profile['about'] == 'Hello world'
40+
assert safe_profile['profile_image'] == 'https://example.com/avatar.jpg'
41+
assert safe_profile['cover_image'] == 'https://example.com/cover.jpg'
42+
2243
def test_invalid_account():
2344
raw_profile = dict(
2445
name='NameIsTooBigByOneChar',

0 commit comments

Comments
 (0)