mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	Fix: Dont allow null custom_fields property via API (#5063)
This commit is contained in:
		@@ -591,7 +591,11 @@ class DocumentSerializer(
 | 
			
		||||
    archived_file_name = SerializerMethodField()
 | 
			
		||||
    created_date = serializers.DateField(required=False)
 | 
			
		||||
 | 
			
		||||
    custom_fields = CustomFieldInstanceSerializer(many=True, allow_null=True)
 | 
			
		||||
    custom_fields = CustomFieldInstanceSerializer(
 | 
			
		||||
        many=True,
 | 
			
		||||
        allow_null=False,
 | 
			
		||||
        required=False,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    owner = serializers.PrimaryKeyRelatedField(
 | 
			
		||||
        queryset=User.objects.all(),
 | 
			
		||||
 
 | 
			
		||||
@@ -400,6 +400,32 @@ class TestCustomField(DirectoriesMixin, APITestCase):
 | 
			
		||||
        self.assertEqual(CustomFieldInstance.objects.count(), 0)
 | 
			
		||||
        self.assertEqual(len(doc.custom_fields.all()), 0)
 | 
			
		||||
 | 
			
		||||
    def test_custom_field_not_null(self):
 | 
			
		||||
        """
 | 
			
		||||
        GIVEN:
 | 
			
		||||
            - Existing document
 | 
			
		||||
        WHEN:
 | 
			
		||||
            - API request with custom_fields set to null
 | 
			
		||||
        THEN:
 | 
			
		||||
            - HTTP 400 is returned
 | 
			
		||||
        """
 | 
			
		||||
        doc = Document.objects.create(
 | 
			
		||||
            title="WOW",
 | 
			
		||||
            content="the content",
 | 
			
		||||
            checksum="123",
 | 
			
		||||
            mime_type="application/pdf",
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        resp = self.client.patch(
 | 
			
		||||
            f"/api/documents/{doc.id}/",
 | 
			
		||||
            data={
 | 
			
		||||
                "custom_fields": None,
 | 
			
		||||
            },
 | 
			
		||||
            format="json",
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
 | 
			
		||||
 | 
			
		||||
    def test_bidirectional_doclink_fields(self):
 | 
			
		||||
        """
 | 
			
		||||
        GIVEN:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user