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

Dashes removed by camelize #151

Open
alexandersoto opened this issue Feb 1, 2021 · 4 comments
Open

Dashes removed by camelize #151

alexandersoto opened this issue Feb 1, 2021 · 4 comments
Labels
help wanted Extra attention is needed

Comments

@alexandersoto
Copy link

Describe the bug
Dashes are replaced and treated as underscores. Is this intentional? It seems to be based on:

UNDERSCORE_RE = re.compile(r"([^\-_\s])[\-_\s]+([^\-_\s])")

My use case is to convert dictionaries with snake case variables to camel case (python code to a JS client). The dictionaries sometimes contain uuids as keys which contain dashes. Those should not be modified. Is there a way to support this use case?

To Reproduce
humps.camelize("9e3f4a89-9989-4190-89a0-f50ffa3826db") => "9e3f4a899989419089a0F50ffa3826db"

Expected behavior
humps.camelize("9e3f4a89-9989-4190-89a0-f50ffa3826db") => "9e3f4a89-9989-4190-89a0-f50ffa3826db"

@nficano nficano added the help wanted Extra attention is needed label Feb 19, 2021
@ificiana ificiana mentioned this issue Oct 14, 2021
5 tasks
@orchowski
Copy link

I had this issue today too, but the value of the dictionary key was replaced. It shouldn't be the case looking at the tests

"_API_": "test_upper",

orchowski added a commit to orchowski/humps that referenced this issue Feb 13, 2022
@orchowski
Copy link

okay sorry, that was my mistake :) @alexandersoto are you using pydantic or something? I was trying to camelize JSON string instead of a dictionary and it removed dashes and even spaces.

I wrote a test:
orchowski@27bd9d3

image

For keys or strings like in your example - dashes, underscores, and spaces should be reduced - that's something I'd expect at least

@jonasao
Copy link

jonasao commented Jan 24, 2024

Pydantic's Object.model_dump_json() in combination with humps.camelize() produces some unexpected results.

I have a pydantic object, which contains properties with UUID-values. When serializing to JSON-string using the Object.model_dump_json() method of pydantic, the values are correctly serialized, but all keys in the JSON-string appears "decamelized".

As I wanted the keys output in the JSON-string to be "camelized", I wrapped the method-call inside the humps.camelize() method:

json_str = hump.camelize(PydanticObject.model_dump_json())

The PydanticObject is serialized to a JSON-string, but the UUID-values appears without "hyphens".

E.g. the UUID-value
0fd28827-3cd4-4499-b888-e819b92d5244
is converted to
0fd288273cd44499b888e819b92d5244

There seems to be a bug in the humps.camelize() method that causes this.
The humps.camelize() method should not affect values, only the keys...

@keyBeatz
Copy link

@jonasao, I've also encountered this issue. However, the problem doesn't lie with pyhumps. The issue arises because model_dump_json returns a string, and camelize processes strings differently than dictionaries (obviously :D). You should probably use it like this:
json.dumps(camelize(message.model_dump(mode="json")))
If you don't include mode="json", your UUIDs won't be converted to strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants