json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Configure fieldRename for enums in build.yaml

Open ookami-kb opened this issue 3 years ago • 7 comments

We have now support for fieldRename on a per-enum basis (rather than per-value), which simplifies the setup.

In our case, all enums coming from the backend are serialized in PascalCase, so it would be great to have an option to configure it in build.yaml, the same as we can do for other fields with field_rename.

ookami-kb avatar Nov 02 '21 14:11 ookami-kb

I need it too

Aqluse avatar Apr 24 '22 18:04 Aqluse

Seems to work or am I missing something? Just add the following to build.yaml -->

targets:
  $default:
    builders:
      json_serializable:
        options:
          field_rename: snake

JPM84 avatar Aug 10 '22 13:08 JPM84

@JPM84 – nope. This does fields within a class. The request here is for enum values.

kevmoo avatar Nov 17 '22 23:11 kevmoo

I'm looking into this and I'm taking a stab at it. I I've got it more or less working now but I do have a big question mark to solve left. Currently I've implemented it so the JsonEnum config is read from the same place as JsonSerializable, but that would mean that field_rename would always be shared between the two.

For example this:

targets:
  $default:
    builders:
      json_serializable:
        options:
          field_rename: kebab

would result in kebab-case as fallback for all Enums and classes annotated with JsonSerializable.

Any ideas or recommendations on how we should solve this properly?

lohnn avatar Jul 22 '23 15:07 lohnn

Ping @kevmoo 😊

lohnn avatar Aug 15 '23 16:08 lohnn

I just stumbled upon this. It was a bit of a paper cut, I didn't expect field_rename not to work for JsonEnum (indeed build.yaml configurations are documented as "global").

It also felt more like a bug, rather than a missing feature, or a lack of documentation.

lucavenir avatar Sep 11 '23 07:09 lucavenir

Any progress on this? Just encountered this when adding the constant_identifier_names lint on a project with a bunch of notCamelCase enum fields. Is there any particular reason why field_rename in build.yaml shouldn't affect enums? If there is, we could also have json_field_rename or something.

alexobviously avatar Jan 31 '24 13:01 alexobviously