From cf9c4e83bf8b07791b11212bc1599761a4d92ba5 Mon Sep 17 00:00:00 2001 From: Feanil Patel Date: Thu, 29 Jun 2023 13:01:18 -0400 Subject: [PATCH] fix: Don't assume there is a context. Don't assume that there will be an extra `context` kwarg when using the bookmark serializer. We use it this way in the current code but that's specific to us and not comon to all serializers. There are a lot of API documentation tools that automate introspecting serializers but they won't know that they have to send in a `context` to the serializer. To make this serializer behave more like other serializers without changing the behavior, we just need to check that the `context` value is defined before we dig into it. In the case that there is no `context` we just treat it the same as if there is no `request` in the `context`. --- openedx/core/djangoapps/bookmarks/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openedx/core/djangoapps/bookmarks/serializers.py b/openedx/core/djangoapps/bookmarks/serializers.py index 37148cc6f2e4..67ca301b675a 100644 --- a/openedx/core/djangoapps/bookmarks/serializers.py +++ b/openedx/core/djangoapps/bookmarks/serializers.py @@ -51,7 +51,7 @@ def __init__(self, *args, **kwargs): # Drop any fields that are not specified in the `fields` argument. required_fields = set(fields) - if 'request' in kwargs['context'] and is_schema_request(kwargs['context']['request']): + if 'context' in kwargs and 'request' in kwargs['context'] and is_schema_request(kwargs['context']['request']): # We are serving the schema: include everything required_fields.update(OPTIONAL_FIELDS)