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

Camel case serialization/deserialization for Schema #33

Closed
fletcheaston opened this issue Nov 8, 2020 · 2 comments
Closed

Camel case serialization/deserialization for Schema #33

fletcheaston opened this issue Nov 8, 2020 · 2 comments

Comments

@fletcheaston
Copy link

I'm currently using DRF and a third-party package called djangorestframework-camel-case (DRFCC). DRFCC provides "Camel case JSON support for Django REST framework", which simply outputs snake case to camel case, and vice versa for input. This is really helpful for my counterpart on the front-end/React side of our application, so he doesn't need to camel case fields himself. DRFCC has 348 stars at this point, so I believe this is a fairly popular use case.

An implementation of this seems fairly straightforward using Pydantic's alias generators, and I see that you've already configured some specific Pydantic Config options for your Schema class.

Is this something you'd be interested in having included as an option to your Schema class? I'll be implementing this myself regardless, but I'm happy to discuss this in greater detail and submit a PR, I can also document this addition to the PR.

@fletcheaston
Copy link
Author

fletcheaston commented Nov 11, 2020

I've basically solved this myself without library additions, the solution isn't perfect but I'm pretty happy with it.

I'm using the pyhumps package for camel-casing my strings.

from ninja import Schema
from humps import camelize


def to_camel(string: str) -> str:
    return camelize(string)


class CamelCaseSchema(Schema):

    class Config:
        alias_generator = to_camel
        allow_population_by_field_name = True

    def dict(self, ignore_alias: bool = False, by_alias: bool = True, **kwargs) -> 'DictStrAny':
        if ignore_alias:
            return super().dict(by_alias=False, **kwargs)

        return super().dict(by_alias=True, **kwargs)

And then I just use CamelCaseSchema as a drop-in for Schema everywhere.

It's a little funky, and I'm ignoring the by_alias entirely (because Ninja seems to have its own mind about using aliases). The only time I don't want to use by_alias is when I'm directly serializing the object to a Django Model, then I have to use...

MyModel.objects.create(**myschema.dict(ignore_alias=True))

This method allows me to serialize directly to a Django Model and the schema shows up in the Swagger documentation correctly. I personally think adding this to the library would be beneficial. I'm happy to submit a PR and update the Ninja docs to highlight this use case @vitalik. If you reopen the issue, I can throw this and the docs together and submit a PR within a day or so.

@bolinocroustibat
Copy link

Thanks @fletcheaston , very useful!

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

2 participants