Skip to content

Commit

Permalink
Fix output of expiry dates (gitlabform#656)
Browse files Browse the repository at this point in the history
The datetime.date object used for access
expiration etc. is not supporting JSON serialization.
Trying to do this for debug output reasons
ends with an exception. The json.dumps() function
offers to specify a default handling of
unsupported types which is utilized here.

Resolves: gitlabform#654

Co-authored-by: Roger Lehmann <[email protected]>
  • Loading branch information
HontoNoRoger and Roger Lehmann authored Dec 23, 2023
1 parent aa26e66 commit 0c85c05
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gitlabform/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
def to_str(a_dict: dict) -> str:
# arguably the most readable form of a dict in a single line
# is JSON with sorted keys
return json.dumps(a_dict, sort_keys=True)
return json.dumps(a_dict, sort_keys=True, default=str)
20 changes: 20 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import datetime

from gitlabform.util import to_str


def test_date_stringification():
example_project_config = {
"members": {
"enforce": True,
"users": {
"project_204_bot": {"access_level": 40},
"project_204_bot_211c9f071a88910e7ed518cc5d81436a": {
"access_level": 40,
"expires_at": datetime.date(2024, 12, 12),
},
},
}
}
output = to_str(example_project_config)
assert type(output) is str

0 comments on commit 0c85c05

Please sign in to comment.