Validate UUIDField values when a queryset is filtered, not when it runs

Đang mở
#425 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
54/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
django, python
Lĩnh vực
backend, databases

Hướng nghiên cứu

Read morango/models/fields/uuids.py and compare UUIDField's preparation path with Django's UUIDField.get_prep_value. Add or update tests for filter(), __in lookups, and accepted UUID formats; the issue does not name a test file. Done means malformed values raise ValidationError with code invalid when the queryset is built, while valid values still filter and save as 32-character hex.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Overview

morango's UUIDField checks values only in get_db_prep_value, which runs when a queryset executes. Django's fields check values in get_prep_value, which runs when .filter() is called, so callers that wrap .filter() in try (Django forms, django-filter, DRF's get_object_or_404) never see morango's error. Kolibri's API returns a 500 for malformed ids in many places because of this.

Complexity: Low
Target branch: release-v0.9.x

Context

In morango/models/fields/uuids.py:

  • Model.objects.filter(id="abc") succeeds. Evaluating it raises ValueError: badly formed hexadecimal UUID string.
  • Model.objects.filter(id=5) or filter(id={"a": 1}) raises KeyError: 'invalid' on evaluation, because get_db_prep_value reads self.error_messages["invalid"], which CharField doesn't define.

Django's own UUIDField.get_prep_value calls to_python, which raises django.core.exceptions.ValidationError with code invalid.

The Change
  • morango's UUIDField should reject a malformed value when the lookup is built, raising django.core.exceptions.ValidationError with code invalid, as Django's UUIDField does.
  • Values the field accepts today (32-char hex, dashed UUID strings, uuid.UUID instances) keep working and keep being stored as 32-char hex.
Acceptance Criteria
  • filter(id="abc") raises ValidationError with code invalid when .filter() is called.
  • filter(id=5), filter(id={"a": 1}) and filter(id=["a"]) raise the same ValidationError, not KeyError or TypeError.
  • filter(id__in=["abc"]) raises the same ValidationError.
  • Hex, dashed and uuid.UUID values filter and save as before.
  • Released in a 0.9.x release.

AI usage

I had Claude Code sweep Kolibri's API code for unvalidated request input and trace the malformed-id 500s to this field. I chose the fix. Claude Code drafted the issue. Kolibri tracking issue: learningequality/kolibri#15838.

Ngôn ngữ chính
Python
Star
15
Fork
27
Merge trung bình
6 ngày 44 phút
Pull request đã merge (30 ngày)
13

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của learningequality/morango

Tất cả issue của learningequality/morango

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.