humps icon indicating copy to clipboard operation
humps copied to clipboard

Dashes removed by camelize

Open alexandersoto opened this issue 4 years ago • 4 comments

Describe the bug Dashes are replaced and treated as underscores. Is this intentional? It seems to be based on: https://github.com/nficano/humps/blob/2bbaf1e7504654e24c8dabc1e5ada7a46727cf3b/humps/main.py#L27

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"

alexandersoto avatar Feb 01 '21 21:02 alexandersoto

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 https://github.com/nficano/humps/blob/f0e3fe03d3ea24be1857111deda7394880e50368/tests/test_camelize.py#L102

orchowski avatar Feb 13 '22 20:02 orchowski

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: https://github.com/orchowski/humps/commit/27bd9d346b163d041d4def49e5839a54f41e8686

image

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

orchowski avatar Feb 13 '22 21:02 orchowski

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...

jonasao avatar Jan 24 '24 10:01 jonasao

@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.

keyBeatz avatar Mar 24 '24 02:03 keyBeatz