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

Fields with capital letters in the beginning are not deserialised by jackson. java #679

Open
Nazukin-Dmitry opened this issue Apr 20, 2024 · 0 comments

Comments

@Nazukin-Dmitry
Copy link

Hello. We have third party schema, where some fields begin with a capital letter.
As example:

type Query {
  user(id: Int): User
}

type User {
  ESIAPassport: String
  firstName: String
}

Generated java class for User looks like:

public class User {
  private String ESIAPassport;

  private String firstName;

  public String getESIAPassport() {
    return ESIAPassport;
  }

  public void setESIAPassport(String ESIAPassport) {
    this.ESIAPassport = ESIAPassport;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
}

We receive response like:

{
  "data": {
    "user": {
      "firstName": "A",
      "ESIAPassport": "1234"
    }
  }
}

If we try to read user object, then field ESIAPassport will not be deserialised by jackson, because jackson analyses setter name and expects json to contain field esiapassport.

        ObjectMapper objectMapper = new ObjectMapper()
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        String response = """
                {
                  "firstName": "A",
                  "ESIAPassport": "1234"
                }
                """;
        User user = objectMapper.readValue(response, User.class);
        
        assertEquals("A", user.getFirstName()); // ok
        assertEquals("1234", user.getESIAPassport()); // fails

I suppose fix would be to add @JsonProperty annotation on getter or both getter and setter - looks like it doesn't matter for jackson.

@Nazukin-Dmitry Nazukin-Dmitry changed the title fields with capital letters in the beginning are not deserialised by jackson. java Fields with capital letters in the beginning are not deserialised by jackson. java Apr 20, 2024
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

1 participant