Skip to content

Commit

Permalink
* db: validate enum must have @Property for json field List<Enum>
Browse files Browse the repository at this point in the history
  > to make it consistent with JSON serialization and ensure refactoring safety

Signed-off-by: neo <1100909+neowu@users.noreply.github.com>
  • Loading branch information
neowu committed Feb 8, 2024
1 parent 45f5a86 commit 595f38a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* executor: tweak shutdown handling, print all tasks not complete
* jre: published neowu/jre:21.0.2
* db: validate enum must have @Property for json field List<Enum>
> to make it consistent with JSON serialization and ensure refactoring safety
### 9.0.5 (1/10/2024 - 1/29/2024)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ core-ng is a webapp framework, it's designed to support our own projects.
```
repositories {
maven {
url 'https://neowu.github.io/maven-repo/'
url = uri("https://neowu.github.io/maven-repo/")
content {
includeGroupByRegex 'core\\.framework.*'
includeGroupByRegex("core\\.framework.*")
}
}
maven {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,16 @@ private void validateJSONField(Field field) {
if (GenericTypes.isList(fieldType)) {
if (!GenericTypes.isGenericList(fieldType))
throw new Error("db json list field must be List<T> and T must be enum or value class, field=" + Fields.path(field));

Class<?> valueClass = GenericTypes.listValueClass(fieldType);
if (!valueClass.isEnum() && !allowedValueClasses.contains(valueClass)) {
throw new Error("db json list field must be List<T> and T must be enum or value class, field=" + Fields.path(field));
if (valueClass.isEnum()) {
JSONClassValidator.validateEnum(valueClass);
return;
}
// use db.allowedValues not json.allowedValues to keep json db field consistent with db entity
if (allowedValueClasses.contains(valueClass)) return;

throw new Error("db json list field must be List<T> and T must be enum or value class, field=" + Fields.path(field));
} else {
Class<?> fieldClass = GenericTypes.rawClass(fieldType);
if (fieldClass.isEnum() || allowedValueClasses.contains(fieldClass))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void validateEntityClass() {
new DatabaseClassValidator(AssignedIdEntity.class, false).validate();
new DatabaseClassValidator(AutoIncrementIdEntity.class, false).validate();
new DatabaseClassValidator(CompositeKeyEntity.class, false).validate();
new DatabaseClassValidator(JSONEntity.class, false).validate();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ public class JSONEntity {
public TestJSON jsonField;

@Column(name = "enum_list", json = true)
public List<TestEnum> enumList;
public List<TestJSONEnum> enumList;

@Column(name = "int_list", json = true)
public List<Integer> intList;

public enum TestJSONEnum {
@Property(name = "DB_V1")
V1,
@Property(name = "DB_V2")
V2
}

public static class TestJSON {
@Property(name = "data")
public String data;

}
}
2 changes: 1 addition & 1 deletion core-ng/src/test/resources/db-test/row-mapper-json.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ public Object map(core.framework.internal.db.ResultSetWrapper resultSet) {
core.framework.internal.db.JSONEntity entity = new core.framework.internal.db.JSONEntity();
entity.id = resultSet.getString("id");
entity.jsonField = (core.framework.internal.db.JSONEntity.TestJSON) core.framework.internal.db.JSONHelper.fromJSON(resultSet.getString("json"), core.framework.internal.db.JSONEntity.TestJSON.class);
entity.enumList = (java.util.List) core.framework.internal.db.JSONHelper.fromJSON(resultSet.getString("enum_list"), core.framework.util.Types.list(core.framework.internal.db.TestEnum.class));
entity.enumList = (java.util.List) core.framework.internal.db.JSONHelper.fromJSON(resultSet.getString("enum_list"), core.framework.util.Types.list(core.framework.internal.db.JSONEntity.TestJSONEnum.class));
entity.intList = (java.util.List) core.framework.internal.db.JSONHelper.fromJSON(resultSet.getString("int_list"), core.framework.util.Types.list(java.lang.Integer.class));
return entity;
}
Expand Down

0 comments on commit 595f38a

Please sign in to comment.