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

Ignore UUID in parser #140

Open
jpsuozzi1 opened this issue Jul 24, 2023 · 1 comment
Open

Ignore UUID in parser #140

jpsuozzi1 opened this issue Jul 24, 2023 · 1 comment

Comments

@jpsuozzi1
Copy link

I am trying to use the CamelCaseJSONParser on an api that takes in an object where some of the keys are dynamic UUID values. I've come across an issue where the parser underscore-izes these UUID values resulting in invalid UUIDs. I can get around it by using ignore_fields, but I feel that UUIDs don't ever need to be underscore-ized.

I'm able to fix this issue by overriding the underscoreize util like so:

def underscoreize(data, **options):
    from uuid import UUID
    
    ignore_fields = options.get("ignore_fields") or ()
    ignore_keys = options.get("ignore_keys") or ()
    if isinstance(data, dict):
        new_dict = {}
        if type(data) == MultiValueDict:
            new_data = MultiValueDict()
            for key, value in data.items():
                new_data.setlist(camel_to_underscore(key, **options), data.getlist(key))
            return new_data
        for key, value in _get_iterable(data):
            if isinstance(key, str):
                new_key = camel_to_underscore(key, **options)
                # START CHANGES
                try:
                    UUID(key)
                except ValueError:
                    # key is not a UUID, so it's okay to add underscores
                    new_key = camel_to_underscore(key, **options)
                else:
                    new_key = key
                # END CHANGES
            else:
                new_key = key

            if key not in ignore_fields and new_key not in ignore_fields:
                result = underscoreize(value, **options)
            else:
                result = value
            if key in ignore_keys or new_key in ignore_keys:
                new_dict[key] = result
            else:
                new_dict[new_key] = result

        if isinstance(data, QueryDict):
            new_query = QueryDict(mutable=True)
            for key, value in new_dict.items():
                new_query.setlist(key, value)
            return new_query
        return new_dict
    if is_iterable(data) and not isinstance(data, (str, File)):
        return [underscoreize(item, **options) for item in data]

    return data
@jpsuozzi1 jpsuozzi1 changed the title Ignore UUID in parser #210 Ignore UUID in parser Jul 24, 2023
@banagale
Copy link

@vbabiy would you take a PR on this?

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