rharter / auto-value-gson

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

Unable to deserialize null field and not @Nullable #213

Closed chris9871 closed 5 years ago

chris9871 commented 5 years ago

I'm uncertain if this is an AutoValue issue or an auto-value-gson issue or simple pebkac.

I was unable to deserialize content that omits the annotations, getting the exception below. I also can't set it to Nullable because of Property annotations has a property builder so it cannot be @Nullable. I also tried removing the annotationsBuilder call and replacing it with a setter, but for some reason this still resulted in the same error.

This is the member class:

    @AutoValue
    public static abstract class Metadata {
        public abstract String name();

        public abstract String namespace();

        @Nullable
        public abstract String resourceVersion();

        public abstract ImmutableMap<String, String> annotations();

        public abstract Builder toBuilder();

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

        @AutoValue.Builder
        public abstract static class Builder {
            public abstract Builder setName(String value);

            public abstract Builder setNamespace(String value);

            public abstract Builder setResourceVersion(String value);

            public abstract ImmutableMap.Builder<String, String> annotationsBuilder();

            public Builder addAnnotation(final String key, final String value) {
                annotationsBuilder().put(key, value);
                return this;
            }

            public abstract Metadata build();
        }

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

And the exception:

java.lang.NullPointerException: Null annotations
        at ...kubernetes.$AutoValue_IstioServiceEntry_Metadata.<init>($AutoValue_IstioServiceEntry_Metadata.java:37)
        at ...kubernetes.AutoValue_IstioServiceEntry_Metadata.<init>(AutoValue_IstioServiceEntry_Metadata.java:26)
        at ...kubernetes.AutoValue_IstioServiceEntry_Metadata$GsonTypeAdapter.read(AutoValue_IstioServiceEntry_Metadata.java:150)
        at ...kubernetes.AutoValue_IstioServiceEntry_Metadata$GsonTypeAdapter.read(AutoValue_IstioServiceEntry_Metadata.java:29)
        at ...kubernetes.AutoValue_IstioServiceEntry$GsonTypeAdapter.read(AutoValue_IstioServiceEntry.java:133)
        at ...kubernetes.AutoValue_IstioServiceEntry$GsonTypeAdapter.read(AutoValue_IstioServiceEntry.java:27)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
        at ...util.GsonHelper$ImmutableListTypeAdapterFactory$1.read(GsonHelper.java:86)
        at ...kubernetes.AutoValue_IstioServiceEntryList$GsonTypeAdapter.read(AutoValue_IstioServiceEntryList.java:144)
        at ...kubernetes.AutoValue_IstioServiceEntryList$GsonTypeAdapter.read(AutoValue_IstioServiceEntryList.java:30)
        at com.google.gson.Gson.fromJson(Gson.java:927)
        at com.google.gson.Gson.fromJson(Gson.java:994)
        at com.google.gson.Gson.fromJson(Gson.java:967)
...
chris9871 commented 5 years ago

I was able to coerce it to be @nullable, so I'm not sure why I was having problems. Pebkac.