ariadne-codegen icon indicating copy to clipboard operation
ariadne-codegen copied to clipboard

Fix enum name processing and add uppercase flag

Open slessans opened this issue 1 year ago • 2 comments

This PR fixes the following bugs in the processing of enum names:

  1. the snake_case conversion function did not properly handle names with all uppercase eg DESC_NULLS_LAST was being converted into LAST
  2. The enum generator was not calling process_name and so could not be hooked into the plugin customization pipeline
  3. The enum generator did not respect the convert_to_snake_case option

This PR additionally adds the convert_enums_names_to_upper_snake_case option to convert enum members to upper snake case like:

class SomeEnum(str, Enum):
    FOO_BAR = "fooBar"
    BAZ_BOW = "bazBow"
    TO_STATUS = "toStatus"

I think this should be a core option rather than a plugin since it is more idiomatic. It also avoid enum name conflicts. For instance I had an enum with title and name as members, which conflicted with the built-in enum methods of the same name. I set the flag to default False for backwards compat.

slessans avatar Dec 21 '23 21:12 slessans

@mat-sop @rafalp Let me know how you want me to break it up/structure it and I am happy to do it, I just needed to unblock a project.

slessans avatar Dec 24 '23 20:12 slessans

Hello, apologies for delay in tacking this:

  1. Valid bug
  2. Enum names should go through process_name like other names.

Name conflict with Python keywords should be solved by appending the _ to final name, eg title_

This PR additionally adds the convert_enums_names_to_upper_snake_case option to convert enum members to upper snake case like

This is odd. Are you naming your GraphQL enum members like title or name instead of TITLE and NAME? If thats the case, its schema design issue and not something codegen should handle out of the box.

rafalp avatar Jan 19 '24 18:01 rafalp