Skip to content

Commit

Permalink
Merge pull request #3499 from ebean-orm/feature/3489
Browse files Browse the repository at this point in the history
Potentially breaking change: Throw PersistenceException for unknown property in select clause
  • Loading branch information
rbygrave authored Oct 18, 2024
2 parents 20cf6a9 + 100d33c commit 423d906
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.ebeaninternal.server.deploy.TableJoin;
import io.ebeaninternal.server.querydefn.OrmQueryDetail;
import io.ebeaninternal.server.querydefn.OrmQueryProperties;
import jakarta.persistence.PersistenceException;

import java.util.*;

Expand Down Expand Up @@ -440,12 +441,9 @@ private void addProperty(SqlTreeProperties selectProps, STreeType desc, OrmQuery

} else {
// find the property including searching the
// sub class hierarchy if required
STreeProperty p = desc.findPropertyWithDynamic(propName, queryProps.getPath());
if (p == null) {
log.log(ERROR, "property [{0}] not found on {1} for query - excluding it.", propName, desc);
p = desc.findProperty("id");
selectProps.add(p);
throw new PersistenceException("Property not found - " + SplitName.add(queryProps.getPath(), propName));

} else if (p.isId() && excludeIdProperty()) {
// do not bother to include id for normal queries as the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

import static org.assertj.core.api.Assertions.assertThat;

public class WriteJsonTest {
class WriteJsonTest {

@Test
public void test_push() {
void test_push() {

ResetBasicData.reset();

FetchPath fetchPath = PathProperties.parse("id,status,name,customer(id,name,billingAddress(street,city)),details(qty,product(sku,prodName))");
FetchPath fetchPath = PathProperties.parse("id,status,customer(id,name,billingAddress(line1,city)),details(orderQty,product(sku,name))");

Query<Order> query = DB.find(Order.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void test() {
// select includes a transient property
TMapSuperEntity e2 = DB.find(TMapSuperEntity.class)
.where().idEq(e.getId())
.select("id, name, myint, someObject, bananan")
.select("id, name, myint, someObject")
.findOne();

assertNotNull(e2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import io.ebean.xtest.BaseTestCase;
import io.ebean.DB;
import jakarta.persistence.PersistenceException;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class TestInsertNoIdBean extends BaseTestCase {

Expand All @@ -19,4 +21,15 @@ void testInsert() {
int rowCount = DB.find(NoIdBean.class).findCount();
assertThat(rowCount).isGreaterThan(0);
}

@Test
void testInvalidSelectColumn_expect_PersistenceException() {
assertThatThrownBy(() ->
DB.find(NoIdBean.class)
.select("does_not_exist_user_id, name")
.where().eq("name", "foo")
.findList()
).isInstanceOf(PersistenceException.class)
.hasMessageContaining("Property not found - does_not_exist_user_id");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@

import static org.junit.jupiter.api.Assertions.assertThrows;

public class TestInvalidFetchPath extends BaseTestCase {
class TestInvalidFetchPath extends BaseTestCase {

@Test
public void invalidFetchPathAndProperties_expect_error() {
void invalidFetchPathAndProperties_expect_error() {
assertThrows(PersistenceException.class, () ->
DB.find(Customer.class)
.fetch("notValidPath", "notHaveProps")
.findList());
}

@Test
public void invalidFetchPath_expect_error() {
void invalidFetchPath_expect_error() {
assertThrows(PersistenceException.class, () ->
DB.find(Customer.class)
.fetch("notValidPath")
.findList());
}

@Test
public void fetchWithInvalidPropertyName_expect_allowed() {
DB.find(Customer.class)
.fetch("billingAddress", "invalidPropertyName")
.findList();
void fetchWithInvalidPropertyName_expect_error() {
assertThrows(PersistenceException.class, () ->
DB.find(Customer.class)
.fetch("billingAddress", "invalidPropertyName")
.findList());
}
}
18 changes: 9 additions & 9 deletions ebean-test/src/test/java/org/tests/types/TestFileType.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;

public class TestFileType extends BaseTestCase {
class TestFileType extends BaseTestCase {

private File file = getFile("/profile-image.jpg");
private File file2 = getFile("/java-64.png");
private final File file = getFile("/profile-image.jpg");
private final File file2 = getFile("/java-64.png");

private File newTempFile() throws IOException {
File tempFile = File.createTempFile("testfile", "txt");
Expand Down Expand Up @@ -113,7 +113,7 @@ public void test_insertNullFile() {
DB.save(bean0);

SomeFileBean bean1 = DB.find(SomeFileBean.class)
.select("name, file")
.select("name, content")
.setId(bean0.getId())
.findOne();

Expand All @@ -124,7 +124,7 @@ public void test_insertNullFile() {
DB.save(bean1);

SomeFileBean bean2 = DB.find(SomeFileBean.class)
.select("name, file")
.select("name, content")
.setId(bean0.getId())
.findOne();

Expand All @@ -146,7 +146,7 @@ public void test_insertUpdateDelete() {
DB.save(bean0);

SomeFileBean bean1 = DB.find(SomeFileBean.class)
.select("name, file")
.select("name, content")
.setId(bean0.getId())
.findOne();

Expand All @@ -161,7 +161,7 @@ public void test_insertUpdateDelete() {


SomeFileBean bean2 = DB.find(SomeFileBean.class)
.select("name, file")
.select("name, content")
.setId(bean0.getId())
.findOne();

Expand All @@ -173,7 +173,7 @@ public void test_insertUpdateDelete() {
DB.save(bean2);

SomeFileBean bean3 = DB.find(SomeFileBean.class)
.select("name, file")
.select("name, content")
.setId(bean0.getId())
.findOne();

Expand All @@ -194,7 +194,7 @@ void test_FileDirty() throws Exception {
DB.save(bean0);

SomeFileBean bean1 = DB.find(SomeFileBean.class)
.select("name, file")
.select("name, content")
.setId(bean0.getId())
.findOne();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void test_lazyFetch_statelessUpdate() {
DB.update(statelessUpdateBean);

SomeFileBean bean2 = DB.find(SomeFileBean.class)
.select("file")
.select("content")
.setId(bean0.getId())
.findOne();

Expand Down

0 comments on commit 423d906

Please sign in to comment.