vbabiy / djangorestframework-camel-case

Camel case JSON support for Django REST framework.
Other
646 stars 122 forks source link

Camelizing schema generator #79

Open knyghty opened 4 years ago

knyghty commented 4 years ago

Right now if you use this package and DRF's OpenAPI schema generator, the fields don't end up camelized. I have kludged together a quick fix for this:

class CamelizingAutoSchema(AutoSchema):
    def _map_serializer(self, serializer):
        result = super()._map_serializer(serializer)
        camelized_properties = {
            camelize_str(field_name): schema
            for field_name, schema in result["properties"].items()
        }
        new_result = {"type": "object", "properties": camelized_properties}
        if "required" in result:
            new_result["required"] = list(map(camelize_str, result["required"]))

        return new_result

It works for me and it seems like a good fit for this project, but I thought I'd check in first before making a PR because I'm not sure if it's an amazing idea. It's very useful and works, but overriding a private API (_map_serializer()) doesn't seem great, especially since DRF's schema generation is quite new and a bit of a moving target at the moment.

But if you'd like I can add some tests for this and submit a PR, but it might need some maintenance.

nicam commented 4 years ago

I have the same problem :)

t-ionut commented 3 years ago

I think I've found a way to make this work by doing a bit of explicit configuration. I just had to implement the OpenAPI schema endpoint from the DRF docs and then passed the renderer classes configured according to this package's docs:

from django.urls import re_path
from rest_framework.schemas import get_schema_view
from rest_framework.settings import api_settings

urlpatterns = [
    re_path(
        "^openapi/$",
        get_schema_view(
            title="My API",
            renderer_classes=api_settings.DEFAULT_RENDERER_CLASSES,
        ),
        name="openapi-schema",
    ),
]

Next time I access my OpenAPI schema url or my Swagger url all fields are camelCased :ok_hand: Hope this helps.

tevariou commented 3 years ago

https://github.com/tfranzel/drf-spectacular supports djangorestframework-camel-case