Skip to content

Commit

Permalink
Improve check for @JSONVIEW and @Version fields
Browse files Browse the repository at this point in the history
  • Loading branch information
radovanradic committed Jan 27, 2025
1 parent c7a0ff3 commit 8933f54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -700,16 +700,11 @@ private void buildUpdateStatement(AnnotationMetadata annotationMetadata, QuerySt

PersistentEntity entity = queryState.getEntity();
boolean jsonEntity = isJsonEntity(annotationMetadata, queryState.getEntity());
if (jsonEntity) {
if (jsonEntity && propertiesToUpdate.size() == 1) {
checkDialectSupportsJsonEntity(entity);
// JsonView updates only DATA column
String jsonEntityColumn = getJsonEntityColumn(annotationMetadata);
if (propertiesToUpdate.size() > 1 && propertiesToUpdate.keySet().contains(jsonEntityColumn)) {
List<String> nonSupportedColumns = propertiesToUpdate.keySet().stream().filter(s -> !jsonEntityColumn.equals(s)).collect(Collectors.toList());
throw new IllegalStateException("Json View supports only `" + jsonEntityColumn + "` column to be updated. Cannot update: " + nonSupportedColumns);
}
// Update JsonView DATA column
String name = propertiesToUpdate.keySet().iterator().next();
String jsonEntityColumn = getJsonEntityColumn(annotationMetadata);
if (name.equals(jsonEntityColumn)) {
Object value = propertiesToUpdate.get(name);
queryString.append(queryState.getRootAlias()).append(DOT).append(jsonEntityColumn).append("=");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public SourcePersistentEntity(
allPersistentProperties.put(id.getName(), id);
} else if (propertyElement.hasStereotype(Version.class)) {
version = new SourcePersistentProperty(this, propertyElement);
if (hasAnnotation(JsonView.class)) {
throw new MappingException("@JsonView mapped entities do not support @Version fields.");
}
allPersistentProperties.put(version.getName(), version);
} else {
SourcePersistentProperty prop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,19 @@ record Person(@Id @GeneratedValue @MappedProperty("id") Long id, String name, in
def ex = thrown(RuntimeException)
ex.message.contains("@JsonView identity @MappedProperty value cannot be set to value different than '_id'")
}

void "test JsonView entity with @Version not supported"() {
when:
buildEntity('test.Person', '''
import io.micronaut.data.annotation.JsonView;
import io.micronaut.data.annotation.MappedProperty;
import io.micronaut.data.annotation.Version;
@JsonView
record Person(@Id @GeneratedValue @MappedProperty("_id") Long id, String name, int age, @Version Long version) {}
''')
then:
def ex = thrown(RuntimeException)
ex.message.contains("@JsonView mapped entities do not support @Version fields")
}
}

0 comments on commit 8933f54

Please sign in to comment.