def test_convert_dict_key_case(): camelData = { "appLabel": "hello", "model": "Something", "nestedList": [{ "someObject": "someValue", "nestWithinNest": [{ "anotherKey": "andAValue" }] }], } snake_data = convert_dict_key_case(camelData, camel_to_snake_case) assert snake_data == { "app_label": "hello", "model": "Something", "nested_list": [{ "some_object": "someValue", "nest_within_nest": [{ "another_key": "andAValue" }] }], } assert camelData == convert_dict_key_case(snake_data, snake_to_camel_case)
def build_incident_attachment(action, incident, metric_value=None, method=None): from sentry.api.serializers.rest_framework.base import ( camel_to_snake_case, convert_dict_key_case, ) data = incident_attachment_info(incident, metric_value, action=action, method=method) return { "metric_alert": convert_dict_key_case( serialize(incident, serializer=IncidentSerializer()), camel_to_snake_case), "description_text": data["text"], "description_title": data["title"], "web_url": data["title_link"], }
def to_internal_value(self, data): if data is None: return None missing_keys = self.REQUIRED_KEYS - set(data.keys()) if missing_keys: missing_key_str = ", ".join( sorted(snake_to_camel_case(key) for key in missing_keys)) raise serializers.ValidationError( f"Missing required keys: {missing_key_str}") layout_to_store = {} for key in self.REQUIRED_KEYS: value = data.get(key) if value is None: continue if not isinstance(value, int): raise serializers.ValidationError( f"Expected number for: {key}") layout_to_store[key] = value # Store the layout with camel case dict keys because they'll be # served as camel case in outgoing responses anyways return convert_dict_key_case(layout_to_store, snake_to_camel_case)
def build_incident_attachment( incident: Incident, new_status: IncidentStatus, metric_value: str | None = None, ) -> Mapping[str, str]: from sentry.api.serializers.rest_framework.base import ( camel_to_snake_case, convert_dict_key_case, ) data = incident_attachment_info(incident, new_status, metric_value) return { "metric_alert": convert_dict_key_case( serialize(incident, serializer=IncidentSerializer()), camel_to_snake_case ), "description_text": data["text"], "description_title": data["title"], "web_url": data["title_link"], }