@@ -28,10 +28,25 @@ def get_or_create_dynamic_params(self, child):
2828
2929 @staticmethod
3030 def split_param (dynamic_param ):
31+ """
32+ Splits a dynamic parameter into a key value pair. The key is used to
33+ index into the serializers context to pass nested parameters
34+
35+ document__fields=name -> ['document', 'fields=name']
36+ document__licenses__fields=name -> ['document', 'licenses__fields=name']
37+
38+ '__'s after the first are preserved so that the dynamic parameter can
39+ be passed recursively down to more deeply-nested serializers
40+ """
3141 crumbs = dynamic_param .split ("__" )
3242 return crumbs [0 ], "__" .join (crumbs [1 :]) if len (crumbs ) > 1 else None
3343
3444 def set_dynamic_params_for_children (self , dynamic_params ):
45+ """
46+ Passes nested dynamic params to child serializer. ie.
47+ "document__fields=name" would pass the query param "fields=name" to the
48+ nested Document Serializer via the 'context' prop
49+ """
3550 for param , fields in dynamic_params .items ():
3651 child , child_dynamic_param = self .split_param (param )
3752 if child in set (self .fields .keys ()):
@@ -41,11 +56,14 @@ def set_dynamic_params_for_children(self, dynamic_params):
4156 @staticmethod
4257 def is_param_dynamic (p ):
4358 """
44- Currently only the 'fields' query param is supported
59+ Only the 'fields' query param supported, so we test for that
4560 """
4661 return p .endswith ("fields" )
4762
4863 def get_dynamic_params_for_root (self , request ):
64+ """
65+ Checks query params in request and returns dynamic parameters
66+ """
4967 query_params = request .query_params .items ()
5068 return {k : v for k , v in query_params if self .is_param_dynamic (k )}
5169
@@ -59,32 +77,37 @@ def get_dynamic_params(self):
5977 return self .parent ._context .get ("dynamic_params" , {})
6078 return self ._context .get ("dynamic_params" , {})
6179
80+ def set_depth (self , depth ):
81+ """
82+ Add appropriate 'depth' param to self.Meta based on 'depth' query param
83+ """
84+ if not depth :
85+ self .Meta .depth = 0
86+ return
87+ try :
88+ depth = int (depth )
89+ if depth > 0 and depth < 3 :
90+ # This value going above 1 could cause performance issues.
91+ # Limited to 1 and 2 for now.
92+ self .Meta .depth = depth
93+ # Depth does not reset by default on subsequent requests with malformed urls.
94+ else :
95+ self .Meta .depth = 0
96+ except ValueError :
97+ pass # it was not castable to an int.
98+
99+
62100 def __init__ (self , * args , ** kwargs ):
63101 request = kwargs .get ("context" , {}).get ("request" )
64102 super ().__init__ (* args , ** kwargs )
65103
104+
66105 # "request" doesn't exist on the child serializers, or when generating OAS spec
106+ # So this if only evaluates as true on the root serializer
107+
67108 is_root = bool (request )
68109 if is_root :
69- if request .method != "GET" :
70- return
71-
72- if depth := request .query_params .get ('depth' ):
73- try :
74- depth_value = int (depth )
75- if depth_value > 0 and depth_value < 3 :
76- # This value going above 1 could cause performance issues.
77- # Limited to 1 and 2 for now.
78- self .Meta .depth = depth_value
79- # Depth does not reset by default on subsequent requests with malformed urls.
80- else :
81- self .Meta .depth = 0
82- except ValueError :
83- pass # it was not castable to an int.
84- else :
85- self .Meta .depth = 0 #The default.
86-
87- #
110+ self .set_depth (request .query_params .get ('depth' ))
88111 dynamic_params = self .get_dynamic_params_for_root (request )
89112 self ._context .update ({"dynamic_params" : dynamic_params })
90113
0 commit comments