From 855fa596b6afdf127f6c619926d6f81e085329c6 Mon Sep 17 00:00:00 2001 From: Sezgin ACER Date: Mon, 5 Jun 2023 22:59:07 +0300 Subject: [PATCH 1/2] Add check for `serializers.HiddenField` on fields_for_serializer function --- graphene_django/rest_framework/mutation.py | 1 + .../rest_framework/tests/test_mutation.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index 4062a4423..e2b9e4b05 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -39,6 +39,7 @@ def fields_for_serializer( field.read_only and is_input and lookup_field != name, # don't show read_only fields in Input + isinstance(field, serializers.HiddenField), # don't show hidden fields in Input ] ) diff --git a/graphene_django/rest_framework/tests/test_mutation.py b/graphene_django/rest_framework/tests/test_mutation.py index 5de823773..91d99f02c 100644 --- a/graphene_django/rest_framework/tests/test_mutation.py +++ b/graphene_django/rest_framework/tests/test_mutation.py @@ -164,6 +164,21 @@ class Meta: ), "'cool_name' is read_only field and shouldn't be on arguments" +def test_hidden_fields(): + class SerializerWithHiddenField(serializers.Serializer): + cool_name = serializers.CharField() + user = serializers.HiddenField(default=serializers.CurrentUserDefault()) + + class MyMutation(SerializerMutation): + class Meta: + serializer_class = SerializerWithHiddenField + + assert "cool_name" in MyMutation.Input._meta.fields + assert ( + "user" not in MyMutation.Input._meta.fields + ), "'user' is hidden field and shouldn't be on arguments" + + def test_nested_model(): class MyFakeModelGrapheneType(DjangoObjectType): class Meta: From a05bfd27c8652cd076323028aff7d0ae832444c1 Mon Sep 17 00:00:00 2001 From: Sezgin ACER Date: Mon, 5 Jun 2023 23:32:34 +0300 Subject: [PATCH 2/2] Add pre-commit changes --- graphene_django/rest_framework/mutation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index e2b9e4b05..b7393dad0 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -39,7 +39,9 @@ def fields_for_serializer( field.read_only and is_input and lookup_field != name, # don't show read_only fields in Input - isinstance(field, serializers.HiddenField), # don't show hidden fields in Input + isinstance( + field, serializers.HiddenField + ), # don't show hidden fields in Input ] )