oxan / djangorestframework-dataclasses

Dataclasses serializer for Django REST framework
BSD 3-Clause "New" or "Revised" License
431 stars 28 forks source link

'source' parameter is not properly handled in create #19

Closed gagarski closed 4 years ago

gagarski commented 4 years ago

Here is a minimal example:

from dataclasses import dataclass

from rest_framework.fields import CharField
from rest_framework_dataclasses.serializers import DataclassSerializer

@dataclass
class Foo:
    bar: str

class ASerializer(DataclassSerializer):
    class Meta:
        dataclass = Foo
        fields = ("renamed_bar",)

    renamed_bar = CharField(source="bar")

ser = ASerializer(data={"renamed_bar": "string"})

ser.is_valid(raise_exception=True)

foo = ser.create(ser.validated_data)

This code does not raise on is_valid(raise_exception=True) but fails with KeyError when calling create.

oxan commented 4 years ago

I've committed a fix for this. There's still one minor issue I want to fix, after that I'll release a new version.