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

JSON deserialization ignores case of setters #1830

Closed
cushon opened this issue Mar 10, 2023 · 0 comments · Fixed by #1831
Closed

JSON deserialization ignores case of setters #1830

cushon opened this issue Mar 10, 2023 · 0 comments · Fixed by #1831
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@cushon
Copy link

cushon commented Mar 10, 2023

The following example shows that json deserialization ignores the case of the setters, and deserializes two different fields that differ only in case to the same setter.

I stepped through in a debugger, and I think the issue is this logic that setups up a FieldInfo for deserialization ignores case:

if (Ascii.toLowerCase(method.getName()).equals("set" + Ascii.toLowerCase(field.getName()))
&& method.getParameterTypes().length == 1) {

That logic was added as part of ff93479


Repro

With com.google.http-client:google-http-client-gson:1.28.0

Serialized: {"passCode":"pass1","passcode":"pass2"}
{"passCode":"pass2"}

With com.google.http-client:google-http-client-gson:1.27.0

Serialized: {"passCode":"pass1","passcode":"pass2"}
{"passCode":"pass1","passcode":"pass2"}
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.Key;

import java.io.StringReader;
import java.io.StringWriter;

public class Repro {

  public static final class Data extends GenericJson {
    @Key String passcode;
    @Key String passCode;

    public String getPasscode() {
      return passcode;
    }

    public Data setPasscode(String passcode) {
      this.passcode = passcode;
      return this;
    }

    public String getPassCode() {
      return passCode;
    }

    public Data setPassCode(String passCode) {
      this.passCode = passCode;
      return this;
    }
  }

  public static void main(String[] args) throws Exception {
    String serialized;
    {
      Data c = new Data().setPassCode("pass1").setPasscode("pass2");
      StringWriter writer = new StringWriter();
      new GsonFactory().createJsonGenerator(writer).serialize(c);
      serialized = writer.toString();
    }
    System.out.println("Serialized: " + serialized);

    Data parsedData =
        new GsonFactory()
            .createJsonObjectParser()
            .parseAndClose(new StringReader(serialized), Data.class);

    System.out.println(parsedData);
  }
}
@meltsufin meltsufin added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Mar 10, 2023
meltsufin added a commit that referenced this issue Mar 11, 2023
Fixes the problem of JSON deserialization ignoring the case of setters.
Now case-sensitive setter method matches are considered first.

Fixes: #1830.
meltsufin added a commit that referenced this issue Mar 14, 2023
* fix: JSON deserialization setter case priority

Fixes the problem of JSON deserialization ignoring the case of setters.
Now case-sensitive setter method matches are considered first.

Fixes: #1830.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants