agrosner / DBFlow

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
MIT License
4.87k stars 598 forks source link

APT randomly picks a global implicit @TypeConverter #1645

Closed TWiStErRob closed 3 years ago

TWiStErRob commented 5 years ago

ISSUE_TEMPLATE

DBFlow Version: 3.3.1

Bug or Feature Request: APT randomly picks @TypeConverter, when there's ambiguity random choice is not good. The build should fail with a list of conflicting converters and let the user choose by removing the @TypeConverter annotations from all but one of the converters where the type signature matches.

See for example how Dagger deals with this: https://stackoverflow.com/questions/48534084/dagger-2-viewmodelprovider-factory-bound-multiple-times

Description: Not sure if it's reproducible in latest, but in our version having two classes

class Instant { ... }

@com.raizlabs.android.dbflow.annotation.TypeConverter
public class InstantDatabaseConverter extends TypeConverter<String, Instant> { }

@com.raizlabs.android.dbflow.annotation.TypeConverter
public class InstantOtherDatabaseConverter extends TypeConverter<String, Instant> { }

means that any implicitly converted entity columns will pick a random global converter:

@Table(database = SomethingDatabase.class, name = SomethingEntity.TABLE_NAME)
public class SomethingEntity extends BaseModel {
    public static final String TABLE_NAME = "Something";

    @Column
    public Instant createdAt;
}

public final class GeneratedDatabaseHolder extends DatabaseHolder {
  public GeneratedDatabaseHolder() {
    // this will change with each build
    typeConverters.put(Instant.class, new Instant[Other]DatabaseConverter());
  }
}

Note: if you specify type converter on the column

    @Column(typeConverter = InstantOtherDatabaseConverter.class)
    public Instant createdAt;

will not be random, as it's explicit.

agrosner commented 3 years ago

fixed in 5.0.0-alpha2.

  1. Default converters Always can get overwritten.
  2. Presence of two conflicting TypeConverters registered globally now throws an error during processing
  3. If you override default converters twice, its also an error.