Skip to content
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

Capitalization in variable names not preserved in some cases #357

Closed
justaparth opened this issue May 27, 2019 · 4 comments
Closed

Capitalization in variable names not preserved in some cases #357

justaparth opened this issue May 27, 2019 · 4 comments

Comments

@justaparth
Copy link

Hello,

I found that if a private instance variable with a standard getter (generated by Lombok or by hand) starts with a single lowercase letter followed by a capital letter, the capitalization is not preserved correctly.

I've made a reproducible and runnable version of my code here: https://github.com/justaparth/typescriptgentest

@Value
class TestClassLombok {
    int xPosition;
    int aLongerVariableName;
    int abPosition;
}

produces:

interface TestClassLombok {
    abPosition: number;
    xposition: number;
    alongerVariableName: number;
}

where I would have expected:

interface TestClassLombok {
    abPosition: number;
    xPosition: number;
    aLongerVariableName: number;
}

I suspect it is related to the getter somehow, since if I don't use lombok and define the getter as getxPosition instead of getXPosition, then the capitalization works: (https://github.com/justaparth/typescriptgentest/blob/master/src/main/java/com/parth/testing/Classes.java#L27).

Any help would be appreciated, thanks!

@vojtechhabarta
Copy link
Owner

Hello, thanks for the test repo.
I added main method which serializes TestClassLombok instance to JSON:

public static void main(String[] args) throws Exception {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    System.out.println(objectMapper.writeValueAsString(new TestClassLombok(10, 10, 10)));
}

and the output was:

{
  "abPosition" : 10,
  "alongerVariableName" : 10,
  "xposition" : 10
}

so it seems that typescript-generator handles these names correctly.
It gets list of properties from Jackson library so it should be consistent with real JSON data.

This naming depends on Jackson cooperation with Lombok.

@vojtechhabarta
Copy link
Owner

@justaparth were you able to make it work as needed?

@justaparth
Copy link
Author

@vojtechhabarta Sorry for the late response, and thanks for the clarification. Just as you said, I confirmed it was consistent with the JSON serialization, which makes sense as the behavior typescript generator would want.

Just in case anyone else comes across this, it seems at least some other people have had this issue with Lombok-generated getters for variable names of this type, i.e. xPosition's getter ends up becoming getXPosition instead of getxPosition. But it seems that various implementations do different things, so its a bit of a gray area :( (projectlombok/lombok#1661).

In any case, after reading your comment, I was able to resolve it by not using lombok getters and defining them myself, getting consistent variable names and json serialization. Thanks again for your help!

class TestClassLombok {
    int xPosition;
    int aLongerVariableName;
    int abPosition;
    public int getxPosition() { .... }
    ....
}

@vojtechhabarta
Copy link
Owner

@justaparth thanks for your comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants