diff --git a/CHANGELOG.md b/CHANGELOG.md index bb545bc4e..d9e4da27a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v0.102 - 2023-07-05 +* Fix serialization of project check messages + + ## v0.101 - 2023-07-05 * Implement file upload in user notebook * Optimize image loading in markdown preview diff --git a/api/src/reportcreator_api/notifications/views.py b/api/src/reportcreator_api/notifications/views.py index b3278b898..d76d18533 100644 --- a/api/src/reportcreator_api/notifications/views.py +++ b/api/src/reportcreator_api/notifications/views.py @@ -1,7 +1,6 @@ from rest_framework import viewsets, mixins, permissions from rest_framework.settings import api_settings -from reportcreator_api.notifications.models import UserNotification from reportcreator_api.notifications.serializers import UserNotificationSerializer from reportcreator_api.users.views import UserSubresourceViewSetMixin diff --git a/api/src/reportcreator_api/pentests/serializers.py b/api/src/reportcreator_api/pentests/serializers.py index 9a8443d3e..99ad001ad 100644 --- a/api/src/reportcreator_api/pentests/serializers.py +++ b/api/src/reportcreator_api/pentests/serializers.py @@ -661,7 +661,7 @@ class PublishPdfOptionsSerializer(serializers.Serializer): class PdfResponseSerializer(serializers.Serializer): - messages = ErrorMessageSerializer(read_only=True) + messages = ErrorMessageSerializer(many=True, read_only=True) pdf = extend_schema_field(OpenApiTypes.BYTE)(serializers.CharField(allow_null=True, read_only=True)) @@ -800,14 +800,16 @@ def update(self, instance, validated_data): class PentestProjectCheckSerializer(serializers.ModelSerializer): - messages = ErrorMessageSerializer(read_only=True) + messages = ErrorMessageSerializer(many=True, read_only=True) class Meta: model = PentestProject fields = ['messages'] def update(self, instance, validated_data): - return instance.perform_checks() + return { + 'messages': instance.perform_checks() + } class CustomizeProjectTypeSerializer(serializers.ModelSerializer): diff --git a/api/src/reportcreator_api/utils/error_messages.py b/api/src/reportcreator_api/utils/error_messages.py index 035163d3b..2b6d4cc71 100644 --- a/api/src/reportcreator_api/utils/error_messages.py +++ b/api/src/reportcreator_api/utils/error_messages.py @@ -2,6 +2,7 @@ import enum from typing import Optional, Union from rest_framework import serializers +from rest_framework.fields import to_choices_dict, flatten_choices_dict class MessageLevel(enum.Enum): @@ -76,15 +77,29 @@ def from_dict(cls, data): }) +class EnumChoiceField(serializers.ChoiceField): + def __init__(self, choices, **kwargs): + self.choices_enum = choices + super().__init__(choices=list(map(lambda e: e.value, self.choices_enum)), **kwargs) + + def to_internal_value(self, data): + return self.choices_enum(super().to_internal_value(data)) + + def to_representation(self, value): + if isinstance(value, self.choices_enum): + value = value.value + return super().to_representation(value) + + class MessageLocationSerializer(serializers.Serializer): - type = serializers.ChoiceField(choices=list(map(lambda v: v.value, MessageLocationType))) + type = EnumChoiceField(choices=MessageLocationType) id = serializers.CharField(allow_null=True) name = serializers.CharField(allow_null=True) path = serializers.CharField(allow_null=True) class ErrorMessageSerializer(serializers.Serializer): - level = serializers.ChoiceField(choices=list(map(lambda v: v.value, MessageLevel))) + level = EnumChoiceField(choices=MessageLevel) message = serializers.CharField() details = serializers.CharField(allow_null=True) location = MessageLocationSerializer() diff --git a/deploy/.env b/deploy/.env index 8b9a547e8..e18cd5665 100644 --- a/deploy/.env +++ b/deploy/.env @@ -1 +1 @@ -SYSREPTOR_VERSION=0.101 +SYSREPTOR_VERSION=0.102