Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson's include-non-empty affects jsonb dirty check test #591

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,12 @@
package io.hypersistence.utils.hibernate.type.json;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.hypersistence.utils.hibernate.type.util.ObjectMapperSupplier;

public class JacksonPropertyInclusionNonEmptyObjectMapperSupplier implements ObjectMapperSupplier {
@Override
public ObjectMapper get() {
return new ObjectMapper().setDefaultPropertyInclusion(JsonInclude.Include.NON_EMPTY);
}
}
@@ -0,0 +1,154 @@
package io.hypersistence.utils.hibernate.type.json;

import io.hypersistence.utils.hibernate.type.util.Configuration;
import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest;
import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.junit.Test;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest extends AbstractPostgreSQLIntegrationTest {
@Override
protected Class<?>[] entities() {
return new Class[]{
MyEntity.class,
};
}

@Override
public void init() {
System.setProperty(
Configuration.PROPERTIES_FILE_PATH,
"PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.properties"
);
super.init();
}

private MyEntity _myEntity;

@Override
protected void afterInit() {
SQLStatementCountValidator.reset();

doInJPA(entityManager -> {
MyEntity entity = new MyEntity();
entity.addAttr("JPA");
entityManager.persist(entity);

_myEntity = entity;
});

SQLStatementCountValidator.assertTotalCount(1);
SQLStatementCountValidator.assertInsertCount(1);
}

@Test
public void testLoad() {
SQLStatementCountValidator.reset();

doInJPA(entityManager -> {
MyEntity myEntity = entityManager.find(MyEntity.class, _myEntity.getId());
myEntity.addAttr("Hibernate");
entityManager.createQuery("select id from MyEntity where flag=false").getSingleResult();
});

SQLStatementCountValidator.assertTotalCount(3);
SQLStatementCountValidator.assertSelectCount(2);
SQLStatementCountValidator.assertUpdateCount(1);

SQLStatementCountValidator.reset();

doInJPA(entityManager -> {
MyEntity myEntity = entityManager.find(MyEntity.class, _myEntity.getId());
myEntity.clearAttr();
entityManager.createQuery("select id from MyEntity where flag=false").getSingleResult();
});

SQLStatementCountValidator.assertTotalCount(3);
SQLStatementCountValidator.assertSelectCount(2);
SQLStatementCountValidator.assertUpdateCount(1);
}

@TypeDefs({@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)})
@Entity(name = "MyEntity")
@Table(name = "my_entity")
public static class MyEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;

@Column
private boolean flag;

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private Post post;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Post getPost() {
return post;
}

public void addAttr(String attr) {
if (post == null) {
post = new Post();
}
post.addAttr(attr);
}

public void clearAttr() {
if (post != null) {
post.clearAttr();
}
}
}

public static class Post {
private List<String> attributes;

public void addAttr(String attr) {
if (attributes == null) {
attributes = new ArrayList<>();
}
attributes.add(attr);
}

public void clearAttr() {
if (attributes != null) {
attributes.clear();
}
}

public List<String> getAttributes() {
return attributes;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Post outside = (Post) o;
return Objects.equals(attributes, outside.attributes);
}

@Override
public int hashCode() {
return Objects.hash(attributes);
}
}

}
@@ -0,0 +1 @@
hypersistence.utils.jackson.object.mapper=io.hypersistence.utils.hibernate.type.json.JacksonPropertyInclusionNonEmptyObjectMapperSupplier