Skip to content

Codegen has started messing up casing of enums since v13.0.1 #3286

Open
@PeteDuncanson

Description

@PeteDuncanson

How did you install the Amplify CLI?

yarn

If applicable, what version of Node.js are you using?

No response

Amplify CLI Version

13.0.1

What operating system are you using?

Windows

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

Nope

Describe the bug

I have a script in my package.json which will rebuild all my graphql types. I run this whenever we add to our schema.

"scripts" : {
  "gql": "amplify api gql-compile && amplify codegen && amplify codegen models"
}

This still works but since upgrading to v13.0.1 of Amplify it has started creating an error with the casing of our enum's within our GraphQL schema. It seems to be the codegen models causing the problem. Given this schema:

enum FrontCleanChangeCAMResponse {
  ACCEPTED
  REJECTED
}

It will create in the /models/index.js with a const like so:

const FrontCleanChangeCamResponse = {
  "ACCEPTED": "ACCEPTED",
  "REJECTED": "REJECTED"
};

export {
  FrontCleanChangeCAMResponse
}

Notice how its PascalCased the enum name, CAM has become Cam. Yet in the export down the bottom it references it with the correctly intended casing but the reference doesn't exist as the casing for the const is PascalCased.

To work around this I've tried quite a few things but it seems the naming convention setting for codegen isn't passed through if set in /.graphqlconfig.yaml so I ended up creating a dirty hack script to fix it to get me unblocked in the meantime.

// patch-enum-casing.js
/* This is a hack to get arouund an issue that started appearing in Amplify v13.0.1 (as of 15/5/2025)

When compiling its changing the case of our enums and then changing it back again when it exports it which is leading to an error as it can't find the enum.

So FrontCleanChangeCAMResponse has a cosnt created called FrontCleanChangeCamResponse which is then tries to export as FrontCleanChangeCAMResponse but it can't find it!

Lost a lot of time trying to fix this so in the end reverted to a dirty hack to just patch it once we compile our graphql!

Hopefully this will get fix in a future version adn then we can remove this.

*/
// patch-enum-casing.js
const fs = require("fs");
const path = require("path");

const apiFile = path.join(__dirname, "src", "models", "index.js");

// List of enums to patch: [incorrect, correct]
const enumPatches = [
  ["FrontCleanChangeCamResponse", "FrontCleanChangeCAMResponse"],
  // Add more as needed
];

let content = fs.readFileSync(apiFile, "utf8");
let originalContent = content;

enumPatches.forEach(([wrong, correct]) => {
  const regex = new RegExp(wrong, "g");
  const matches = content.match(regex);
  if (matches) {
    console.log(
      `Replacing ${matches.length} occurrence(s) of "${wrong}" with "${correct}"`
    );
    content = content.replace(regex, correct);
  } else {
    console.log(`No occurrences of "${wrong}" found.`);
  }
});

if (content !== originalContent) {
  fs.writeFileSync(apiFile, content, "utf8");
  console.log("Patched enum casing in API.ts");
} else {
  console.log("No changes made to API.ts");
}

Which I've added to the end of my gql build script chain:

"gql": "amplify api gql-compile && amplify codegen && amplify codegen models && node patch-enum-casing.js"

Expected behavior

The generated const enum should keep the existing casing for Acronyms like it used to.

Reproduction steps

The above code samples should be enough to get you running. I think you only need to run codegen models though not all the GraphQL build stuff I mentioned.

Project Identifier

This never works for me, ever.

Log output

# Put your logs below this line


Additional information

This is a really long lived Amplify setup (5+ years now I think) and firmly on Gen 1. Based on a Create React App base (yes, doing something about that is on my todo list one day I promise).

Before submitting, please confirm:

  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions