vbabiy / djangorestframework-camel-case

Camel case JSON support for Django REST framework.
Other
631 stars 123 forks source link

camelCase is not working when POSTing data. #160

Open amitv9493 opened 1 month ago

amitv9493 commented 1 month ago

The camelCase is working fine with the GET data, but it is not working with the POST data whe using drf-spectacular.

GET Data:

 {
        "id": 1,
        "createdAt": "2024-07-14T09:41:50.010621Z",
        "updatedAt": "2024-07-14T09:41:50.010647Z",
        "clientName": "q",
        "clientPhone": "q",
        "clientEmail": "amitv9493@gmail.com",
        "taskTime": "2024-07-14T09:41:19Z",
        "notes": "1",
}

But When accessing the POST api endpoint in swagger doc. It shows this example data which is confusing the api consumers.

{
  "client_name": "string",
  "client_phone": "string",
  "client_email": "user@example.com",
  "task_time": "2024-07-28T14:15:05.809Z",
  "notes": "string",
  "payment_amount": 9223372036854776000,
  "payment_method": "ONLINE",
  "is_completed": true,
  "job_type": "APPLY",
  "job_deadline": "2024-07-28T14:15:05.809Z",
  "apply_deadline": "2024-07-28T14:15:05.809Z",
  "brokerage": "MY-BROKERAGE",
  "show_client_phone_number": true,
  "access_information": "string",
  "property": 0,
  "assigned_to": 0
}

Is there something I am doing wrong? I followed the docs and still nothing works for me

juhanakristian commented 1 month ago

I was able to make it work by adding the following settings in settings.py for drf-spectacular

SPECTACULAR_SETTINGS = {
    'CAMELIZE_NAMES': False, 
    'POSTPROCESSING_HOOKS': [
        'drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields',
        'drf_spectacular.hooks.postprocess_schema_enums'
    ],
}
amitv9493 commented 1 month ago

wow. Thank you so much @juhanakristian

amitv9493 commented 1 month ago

Hi @juhanakristian

When there is any validation error of a field. It is still showing the camel case name like this .

{
  "type": "validation_error",
  "errors": [
    {
      "code": "null",
      "detail": "This field may not be null.",
      "attr": "client_name"
    },
    {
      "code": "does_not_exist",
      "detail": "Invalid pk \"0\" - object does not exist.",
      "attr": "property"
    }
  ]
}

Is there any setting that I am missing right now ?