Skip to content

Fix parsing of bookmark objects #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
18 changes: 9 additions & 9 deletions kubernetes_asyncio/watch/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ def unmarshal_event(self, data: str, response_type):
reason = "{}: {}".format(obj['reason'], obj['message'])
raise client.exceptions.ApiException(status=obj['code'], reason=reason)

# If possible, compile the JSON response into a Python native response
# type, eg `V1Namespace` or `V1Pod`,`ExtensionsV1beta1Deployment`, ...
if response_type:
js['object'] = self._api_client.deserialize(
response=SimpleNamespace(data=json.dumps(js['raw_object'])),
response_type=response_type
)

if js['type'].lower() != 'bookmark':
# If possible, compile the JSON response into a Python native response
# type, eg `V1Namespace` or `V1Pod`,`ExtensionsV1beta1Deployment`, ...
if response_type:
js['object'] = self._api_client.deserialize(
response=SimpleNamespace(data=json.dumps(js['raw_object'])),
response_type=response_type
)

# decode and save resource_version to continue watching
if hasattr(js['object'], 'metadata'):
self.resource_version = js['object'].metadata.resource_version
Expand All @@ -123,7 +123,7 @@ def unmarshal_event(self, data: str, response_type):
self.resource_version = js['object']['metadata']['resourceVersion']

elif js['type'].lower() == 'bookmark':
self.resource_version = js['object']['metadata']['resourceVersion']
self.resource_version = js['raw_object']['metadata']['resourceVersion']

Comment on lines +126 to 127
Copy link
Preview

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For bookmark events, ensure that js['raw_object'] always contains a 'metadata' key with 'resourceVersion' to avoid potential KeyErrors. Consider adding a conditional check if there's any chance that this field might be missing.

Suggested change
self.resource_version = js['raw_object']['metadata']['resourceVersion']
if ('metadata' in js['raw_object'] and
'resourceVersion' in js['raw_object']['metadata']):
self.resource_version = js['raw_object']['metadata']['resourceVersion']
else:
raise Exception(("Malformed JSON response for bookmark event, "
"'metadata' or 'resourceVersion' field is missing. "
"JSON: {}").format(js))

Copilot uses AI. Check for mistakes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add some checks for malformed events, similar are for standard events.

return js

Expand Down
21 changes: 19 additions & 2 deletions kubernetes_asyncio/watch/watch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,23 @@ async def test_unmarshal_bookmark_succeeds_and_preserves_resource_version(self):

# make sure the resource version is preserved,
# and the watcher's resource_version is updated
self.assertTrue(isinstance(event['object'], dict))
self.assertEqual("1", event['object']['metadata']['resourceVersion'])
self.assertTrue(isinstance(event['raw_object'], dict))
self.assertEqual("1", event['raw_object']['metadata']['resourceVersion'])
self.assertEqual("1", w.resource_version)

async def test_unmarshal_job_bookmark_succeeds_and_preserves_resource_version(self):
w = Watch()
event = w.unmarshal_event('{"type": "BOOKMARK", "object": {"apiVersion":'
'"batch/v1","kind":"Job","metadata":'
'{"name": "bar", "resourceVersion": "1"},'
'"spec": {"template": {"metadata": '
'{"creationTimestamp":null}, "spec": '
'{"containers":null}}}}}',
'object')
self.assertEqual("BOOKMARK", event['type'])

# make sure the resource version is preserved,
# and the watcher's resource_version is updated
self.assertTrue(isinstance(event['raw_object'], dict))
self.assertEqual("1", event['raw_object']['metadata']['resourceVersion'])
self.assertEqual("1", w.resource_version)