jacob-macleod / Dolphin-Flashcard-App

http://www.dolphinflashcards.com
MIT License
1 stars 3 forks source link

TypeError on create_flashcard endpoint (regex) #89

Closed jaymeklein closed 3 months ago

jaymeklein commented 3 months ago

When testing the create_flashcard endpoint using example data, during the check_request_json function, i got a TypeError: unhashable type: 'list':

In re.match(expected_value, request_values[i]): the values are:

expected_value

[
  {
    "front": "",
    "back": "",
    "reviewStatus": "^\\d+\\.\\d+$",
    "lastReview": "^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\\d{4}$"
  }
]

request_values[i]

[
  {
    "front": "Front 1",
    "back": "Back 1",
    "reviewStatus": "0.0",
    "lastReview": "01/01/1969"
  },
  {
    "front": "Front 2",
    "back": "Back 2",
    "reviewStatus": "0.0",
    "lastReview": "01/01/1969"
  }
]

Dummy data

data = {
            "userID": "1",
            "flashcardName": "My new set",
            "flashcardDescription": "This is\nmy description",
            "cards": [
                {
                    "front": "Front 1",
                    "back": "Back 1",
                    "reviewStatus": "0.0",
                    "lastReview": "01/01/1969"
                },
                {
                    "front": "Front 2",
                    "back": "Back 2",
                    "reviewStatus": "0.0",
                    "lastReview": "01/01/1969"
                }
            ]
        }

The above test results in:

  File "Flashcard-App\backend\verification\api_error_checking.py", line 44, in check_request_json
    if not re.match(expected_value, request_values[i]):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\re\__init__.py", line 167, in match
    return _compile(pattern, flags).match(string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\re\__init__.py", line 285, in _compile
    return _cache2[type(pattern), pattern, flags]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'
jacob-macleod commented 3 months ago

I've fixed this now, so if you merge from main the changes should be applied. The error was because in the function which turns a dictionary with sub items to a flat dictionary to check each value in turn using regex to see if you have passed it the right values, dictionary items which were lists weren't being handled by the function. Thanks for creating the two issues!

jaymeklein commented 3 months ago

Hey @jacob-macleod, thanks for fixing this issue. I'm glad you managed to solve this, and i hope it was well documented so you don't struggle trying to reproduce it.