def traverse_errors(fields, node, stack=""): # Does the list contain errors or more fields? if isinstance(node, list): if any([isinstance(i, ErrorDetail) for i in node]): stack_key = copy(stack) fields.append(dict(name=humps.camelize(stack_key), values=node)) else: for i, item in enumerate(node): # if item is an empty dict, stop. if item == {}: continue stack_key = copy(stack) stack_key = f"{stack_key}[{i}]" traverse_errors(fields, item, stack_key) # If node is a dict, use the keys if isinstance(node, dict): for name, errors in node.items(): # Copy key so the ref is lost and the chain becomes unique stack_key = copy(stack) if bool(stack_key): if isinstance(name, int): stack_key = f"{stack_key}[{name}]" else: stack_key = f"{stack_key}.{name}" else: stack_key = name traverse_errors(fields, errors, stack=stack_key)
def traverse(node, stack=""): for name, errors in node.items(): inner_stack = copy(stack) if bool(inner_stack): if isinstance(name, int): inner_stack = f"{inner_stack}[{name}]" else: inner_stack = f"{inner_stack}.{name}" else: inner_stack = name if isinstance(errors, list): # final condition fields.append( dict(name=humps.camelize(inner_stack), values=errors)) else: traverse(errors, stack=inner_stack)
def camelize_json(response): if response.headers['Content-Type'] == 'application/json': response.set_data( json.dumps(camelize(json.loads(response.get_data()))))
def snake2camelback(snake_dict: dict): """Convert the passed dictionary's keys from snake_case to camelBack case.""" return camelize(snake_dict)
def camelize_json(response): if (response.headers['Content-Type'] == 'application/json' and 'swagger.json' not in request.base_url): response.set_data( json.dumps(camelize(json.loads(response.get_data()))))