rharter / auto-value-gson

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

Issue with default empty collections still being null #158

Closed cammace closed 5 years ago

cammace commented 6 years ago

I'm currently in the middle of upgrading one of our pure Java libraries using AutoValue and this extension. I'm running into an issue however while trying to set the compiler options in my submodules build.gradle I see no errors but it doesn't seem to change anything.

My understanding of the default collections to empty is that any list will never be null but rather just an empty list? This isn't working properly for me right now and the list still ends up being null.

compileJava {
  options.compilerArgs << "-Aautovaluegson.defaultCollectionsToEmpty=true"
}

When I try setting the list a default value using:

options.compilerArgs << "-Aautovaluegson.mutableAdaptersWithDefaultSetters=true"

I just get an error that there is no such method for setDefaultVoiceInstructions

public static TypeAdapter<LegStep> typeAdapter(Gson gson) {
    return new AutoValue_LegStep.GsonTypeAdapter(gson)
      .setDefaultVoiceInstructions(new ArrayList<VoiceInstructions>());
  }

Am I missing something here or does this functionality not work properly anymore?

For reference, this is the class I am trying to implement this in, inside this project.

ZacSweers commented 5 years ago

I think we should make a call for now to just not support these. Modeling absence in general Java code is one thing, but modeling it in JSON is very different, and I'd rather opt for no solution rather than a limited/awkward one and encourage better support for normalization from AutoValue via #132.