Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camelizing schema generator #79

Open
knyghty opened this issue Feb 11, 2020 · 3 comments
Open

Camelizing schema generator #79

knyghty opened this issue Feb 11, 2020 · 3 comments

Comments

@knyghty
Copy link

knyghty commented Feb 11, 2020

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
Copy link

nicam commented Feb 17, 2020

I have the same problem :)

@t-ionut
Copy link

t-ionut commented Dec 21, 2020

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 👌
Hope this helps.

@tevariou
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants