huma icon indicating copy to clipboard operation
huma copied to clipboard

Case Sensitivity For JSON tags

Open jchen1122-sibros opened this issue 1 year ago • 5 comments

Case sensitivity is enforced with huma via struct tags. In several other frameworks, case sensitivity is NOT enforced. Therefore, migration to huma may break some client integrations. Is it possible to introduce a tag to allow case insensitivity?

jchen1122-sibros avatar Sep 18 '24 23:09 jchen1122-sibros

@jchen1122-sibros do you have an example? Do you want Huma to e.g. always produce lower case output? Part of this is controlled by the serialization format and the marshaler implementation, e.g. https://pkg.go.dev/encoding/json#Marshal rather than within Huma itself. You might be able to use a custom JSON marshaler to do what you want and register it with your huma.Config for the application/json format.

This is how the default formatter works BTW: https://github.com/danielgtaylor/huma/blob/main/defaults.go#L8-L21

danielgtaylor avatar Oct 08 '24 23:10 danielgtaylor

The native golang json marshal/unmarshal is case insensitive . Huma json tag validation is case sensitive. It makes it is backward incompatible as we migrate to Huma. Would be a good idea to have a flag in Huma to make the validation case insensitive.

jc-fireball avatar Oct 22 '24 16:10 jc-fireball

@danielgtaylor what do you think?

jc-fireball avatar Oct 22 '24 16:10 jc-fireball

Aaaah, thank you @jc-fireball I think I wasn't understanding this before. So you are saying that since something like this works: https://go.dev/play/p/2qbvJKIkt9J you would expect Huma's validation to behave the same, ignoring the fact that FOO isn't the same as foo.

I think we can probably support this in a reasonable way to help people migrate over legacy systems.

danielgtaylor avatar Oct 23 '24 01:10 danielgtaylor

@danielgtaylor correct. We are migrating services to Huma but this json field name issue blocks us. The change should be straightforward to lower all the cases when validation happens. If you can prioritize it, it will be much better. Thanks in advance.

jc-fireball avatar Oct 23 '24 01:10 jc-fireball

@jc-fireball take a look at #629 and let me know what you think!

danielgtaylor avatar Oct 28 '24 15:10 danielgtaylor

It looks good to me. Let's ship it !

jc-fireball avatar Oct 28 '24 18:10 jc-fireball