rharter / auto-value-gson

AutoValue Extension to add Gson De/Serializer support
Apache License 2.0
607 stars 103 forks source link

AutoValueGsonExtension fails on immutable collections. #249

Closed jstaaf closed 4 years ago

jstaaf commented 4 years ago

Given the example from google/auto:

@AutoValue
public abstract class Animal {
  public abstract String name();
  public abstract int numberOfLegs();
  public abstract ImmutableSet<String> countries();

  public static Builder builder() {
    return new AutoValue_Animal.Builder();
  }

  @AutoValue.Builder
  public abstract static class Builder {
    public abstract Builder setName(String value);
    public abstract Builder setNumberOfLegs(int value);
    public abstract Builder setCountries(Set<String> value);
    public abstract Builder setCountries(String... value);
    public abstract Animal build();
  }
}

If I attempt to use auto-value-gson with this class, like this:

@AutoValue
public abstract class Animal {

    @SerializedName("name")
    public abstract String name();
    @SerializedName("numberOfLegs")
    public abstract int numberOfLegs();
    @SerializedName("countries")
    public abstract ImmutableSet<String> countries();

    public static Builder builder() {
        return new AutoValue_Animal.Builder();
    }

    public static TypeAdapter<Animal> typeAdapter(Gson gson) {
        return new AutoValue_Animal.GsonTypeAdapter(gson);
    }

    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Builder setName(String value);
        public abstract Builder setNumberOfLegs(int value);
        public abstract Builder setCountries(Set<String> value);
        public abstract Builder setCountries(String... value);
        public abstract Animal build();
    }
}

the annotation processor will fail.

Error:java: java.lang.IllegalArgumentException: Setter not found for countries()
/Users/jensstaaf/repo/player-common/android/player-common/src/main/java/com/spotify/player/model/Animal.java
Error:(12, 17) java: @AutoValue processor threw an exception: java.lang.IllegalArgumentException: Setter not found for countries()
    at com.ryanharter.auto.value.gson.AutoValueGsonExtension.addBuilderFieldSetting(AutoValueGsonExtension.java:572)
    at com.ryanharter.auto.value.gson.AutoValueGsonExtension.createReadMethod(AutoValueGsonExtension.java:792)
    at com.ryanharter.auto.value.gson.AutoValueGsonExtension.createTypeAdapter(AutoValueGsonExtension.java:510)
    at com.ryanharter.auto.value.gson.AutoValueGsonExtension.generateClass(AutoValueGsonExtension.java:283)
    at com.google.auto.value.processor.AutoValueProcessor.writeExtensions(AutoValueProcessor.java:292)
    at com.google.auto.value.processor.AutoValueProcessor.processType(AutoValueProcessor.java:254)
    at com.google.auto.value.processor.AutoValueOrOneOfProcessor.process(AutoValueOrOneOfProcessor.java:312)

The expected outcome would be that the generated type adapter works with common enough models that use immutable collections - as supported out of the box by autovalue.

ZacSweers commented 4 years ago

See comment here for why we can't support this case safely: https://github.com/rharter/auto-value-moshi/issues/166#issuecomment-582242087