Skip to content

[#7446] Handle special characters followed by digits in http-client-java's CodeNamer.getEnumMemberName #7448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

er1c
Copy link

@er1c er1c commented May 22, 2025

No description provided.

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:java Issue for the Java client emitter: @typespec/http-client-java label May 22, 2025
@@ -94,6 +94,15 @@ public static String getEnumMemberName(String name) {
return name;
}

// Special handling for inputs that start with special characters followed by numbers
if (name.length() > 1 && !Character.isLetterOrDigit(name.charAt(0)) && Character.isDigit(name.charAt(1))) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could add name.length() >= 2 if that is more readable/less of an index out of bounds mind phrack

Copy link
Contributor

No changes needing a change description found.

Copy link
Contributor

@weidongxu-microsoft weidongxu-microsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution Eric!

This was a bug in conversion logic and your fixed definitely improves upon it.

I've let a comment about what we do consistently on some slightly different input like #$100 Gift Card.

BTW, let us know which tool you uses when you encounter this bug (so that we can schedule a release after the fix).

@@ -32,6 +32,12 @@ public void testEnumMemberName() {

Assertions.assertEquals("ONE_ZEROMINUTELY", CodeNamer.getEnumMemberName("_10minutely"));

Assertions.assertEquals("DOLLAR_SIGN_100_GIFT_CARD", CodeNamer.getEnumMemberName("$100 Gift Card"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, without the fix the output would be 100_GIFT_CARD, which is not a valid var name in Java.

There is 2 concern from me, on the output.

  1. _100 Gift Card would output ONE_ZERO_ZERO_GIFT_CARD, thus from existing logic, it may be more reasonable to output same for $100 Gift Card (I know this ONE_ZERO_ZERO_GIFT_CARD looks bad...)
  2. #$100 Gift Card would still generate 100_GIFT_CARD

I am of pretty weak opinion on item (1), but I would like to see if we had something consistent for item (2).

BTW, if one uses TypeSpec, one would have the option to explicitly write the name of the enum, thus avoid this logic to infer the var name.
https://typespec.io/docs/language-basics/enums/#assigning-values-to-enums

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:java Issue for the Java client emitter: @typespec/http-client-java
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants