Skip to content

Commit c3d2501

Browse files
chore(aws-api-appsync) Use class as value instead of simpleName for JavaFieldType in case of duplicated Class simple name. (#839)
We were using class's simple name as identifier , this will cause issues when classes have same name (different package) , In fact, there are already two duplicated classes: java.lang.Date vs Temporal.Date , former is used in our model class. Co-authored-by: zhong qing
1 parent 2679a0c commit c3d2501

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

aws-api-appsync/src/main/java/com/amplifyframework/api/aws/SelectionSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ private static boolean isCustomType(@NonNull Field field) {
280280
return false;
281281
}
282282
try {
283-
JavaFieldType.from(cls.getSimpleName());
283+
JavaFieldType.from(cls);
284284
return false;
285285
} catch (IllegalArgumentException exception) {
286286
// if we get here then field is a custom type

aws-api-appsync/src/main/java/com/amplifyframework/core/model/types/JavaFieldType.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.amplifyframework.core.model.Model;
2121
import com.amplifyframework.core.model.temporal.Temporal;
2222

23+
import java.util.Date;
24+
2325
/**
2426
* Enumerate the types used in the fields
2527
* of {@link com.amplifyframework.core.model.Model} classes.
@@ -28,86 +30,83 @@ public enum JavaFieldType {
2830
/**
2931
* Represents the boolean data type.
3032
*/
31-
BOOLEAN(Boolean.class.getSimpleName()),
33+
BOOLEAN(Boolean.class),
3234

3335
/**
3436
* Represents the int data type.
3537
*/
36-
INTEGER(Integer.class.getSimpleName()),
38+
INTEGER(Integer.class),
3739

3840
/**
3941
* Represents the long data type.
4042
*/
41-
LONG(Long.class.getSimpleName()),
43+
LONG(Long.class),
4244

4345
/**
4446
* Represents the float data type.
4547
*/
46-
FLOAT(Float.class.getSimpleName()),
48+
FLOAT(Float.class),
4749

4850
/**
4951
* Represents the String data type.
5052
*/
51-
STRING(String.class.getSimpleName()),
53+
STRING(String.class),
54+
55+
/**
56+
* Represents the java.lang.Date data type.
57+
*/
58+
JAVA_DATE(Date.class),
5259

5360
/**
5461
* Represents the Date data type.
5562
*/
56-
DATE(Temporal.Date.class.getSimpleName()),
63+
DATE(Temporal.Date.class),
5764

5865
/**
5966
* Represents the DateTime data type.
6067
*/
61-
DATE_TIME(Temporal.DateTime.class.getSimpleName()),
68+
DATE_TIME(Temporal.DateTime.class),
6269

6370
/**
6471
* Represents the Time data type.
6572
*/
66-
TIME(Temporal.Time.class.getSimpleName()),
73+
TIME(Temporal.Time.class),
6774

6875
/**
6976
* Represents the Timestamp data type.
7077
*/
71-
TIMESTAMP(Temporal.Timestamp.class.getSimpleName()),
78+
TIMESTAMP(Temporal.Timestamp.class),
7279

7380
/**
7481
* Represents the Enum type.
7582
*/
76-
ENUM(Enum.class.getSimpleName()),
83+
ENUM(Enum.class),
7784

7885
/**
7986
* Represents the Model type.
8087
*/
81-
MODEL(Model.class.getSimpleName()),
88+
MODEL(Model.class),
8289

8390
/**
8491
* Represents any custom type (objects that are not models).
8592
*/
86-
CUSTOM_TYPE(Object.class.getSimpleName());
93+
CUSTOM_TYPE(Object.class);
8794

88-
private final String javaFieldType;
95+
private final Class<?> javaFieldType;
8996

90-
JavaFieldType(@NonNull String javaFieldType) {
97+
JavaFieldType(@NonNull Class<?> javaFieldType) {
9198
this.javaFieldType = javaFieldType;
9299
}
93100

94-
/**
95-
* Return the string that represents the value of the enumeration constant.
96-
* @return the string that represents the value of the enumeration constant.
97-
*/
98-
public String stringValue() {
99-
return this.javaFieldType;
100-
}
101-
102101
/**
103102
* Construct and return the JavaFieldType enumeration for the given string
104103
* representation of the field type.
105104
* @param javaFieldType the string representation of the field type.
106105
* @return the enumeration constant.
107106
*/
108-
public static JavaFieldType from(@NonNull String javaFieldType) {
107+
public static JavaFieldType from(@NonNull Class<?> javaFieldType) {
109108
for (final JavaFieldType type : JavaFieldType.values()) {
110-
if (javaFieldType.equals(type.stringValue())) {
109+
if (javaFieldType.equals(type.javaFieldType)) {
111110
return type;
112111
}
113112
}

aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/TypeConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private TypeConverter() {
6262
JAVA_TO_SQL.put(JavaFieldType.STRING, SQLiteDataType.TEXT);
6363
JAVA_TO_SQL.put(JavaFieldType.ENUM, SQLiteDataType.TEXT);
6464
JAVA_TO_SQL.put(JavaFieldType.DATE, SQLiteDataType.TEXT);
65+
JAVA_TO_SQL.put(JavaFieldType.JAVA_DATE, SQLiteDataType.TEXT);
6566
JAVA_TO_SQL.put(JavaFieldType.DATE_TIME, SQLiteDataType.TEXT);
6667
JAVA_TO_SQL.put(JavaFieldType.TIME, SQLiteDataType.TEXT);
6768
JAVA_TO_SQL.put(JavaFieldType.TIMESTAMP, SQLiteDataType.INTEGER);
@@ -77,7 +78,7 @@ static JavaFieldType getJavaFieldType(@NonNull ModelField field) {
7778
return JavaFieldType.ENUM;
7879
}
7980
try {
80-
return JavaFieldType.from(field.getType().getSimpleName());
81+
return JavaFieldType.from(field.getType());
8182
} catch (IllegalArgumentException exception) {
8283
// fallback to custom type, which will result in the field being converted to a JSON string
8384
return JavaFieldType.CUSTOM_TYPE;
@@ -97,7 +98,7 @@ public static JavaFieldType getJavaFieldTypeFromValue(@NonNull Object value) {
9798
return JavaFieldType.ENUM;
9899
}
99100
try {
100-
return JavaFieldType.from(value.getClass().getSimpleName());
101+
return JavaFieldType.from(value.getClass());
101102
} catch (IllegalArgumentException exception) {
102103
// fallback to custom type, which will result in the field being converted to a JSON string
103104
return JavaFieldType.CUSTOM_TYPE;

0 commit comments

Comments
 (0)