Skip to content

Commit

Permalink
Honour @JsonIgnore on Panache entities
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand authored and gsmet committed Jun 30, 2020
1 parent 0230e20 commit 6bc4947
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.MethodInfo;
import org.objectweb.asm.AnnotationVisitor;
Expand Down Expand Up @@ -48,6 +49,8 @@ public abstract class PanacheEntityEnhancer<MetamodelType extends MetamodelInfo<
private static final String JSON_PROPERTY_BINARY_NAME = "com/fasterxml/jackson/annotation/JsonProperty";
private static final String JSON_PROPERTY_SIGNATURE = "L" + JSON_PROPERTY_BINARY_NAME + ";";

private static final DotName JSON_IGNORE_DOT_NAME = DotName.createSimple("com.fasterxml.jackson.annotation.JsonIgnore");

protected MetamodelType modelInfo;
protected final ClassInfo panacheEntityBaseClassInfo;
protected final IndexView indexView;
Expand Down Expand Up @@ -242,7 +245,7 @@ protected void generateAccessors() {
}
// Add an explicit Jackson annotation so that the entire property is not ignored due to having @XmlTransient
// on the field
if (!field.hasAnnotation(JSON_PROPERTY_SIGNATURE)) {
if (shouldAddJsonProperty(field)) {
mv.visitAnnotation(JSON_PROPERTY_SIGNATURE, true);
}
mv.visitEnd();
Expand Down Expand Up @@ -287,6 +290,22 @@ protected void generateAccessors() {
}
}

private boolean shouldAddJsonProperty(EntityField entityField) {
if (isAnnotatedWithJsonIgnore(entityField)) {
return false;
}
return !entityField.hasAnnotation(JSON_PROPERTY_SIGNATURE);
}

private boolean isAnnotatedWithJsonIgnore(EntityField entityField) {
FieldInfo field = entityInfo.field(entityField.name);
if (field != null) {
return field.hasAnnotation(JSON_IGNORE_DOT_NAME);
}

return false;
}

protected abstract void generateAccessorSetField(MethodVisitor mv, EntityField field);

protected abstract void generateAccessorGetField(MethodVisitor mv, EntityField field);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.it.panache;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import com.fasterxml.jackson.annotation.JsonIgnore;

import io.quarkus.hibernate.orm.panache.PanacheEntityBase;

@Entity
public class Book extends PanacheEntityBase {

@Id
@GeneratedValue
public Integer id;

public String name;

@JsonIgnore
public String author;

public Book(String name, String author) {
this.name = name;
this.author = author;
}

public Book() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.it.panache;

import javax.enterprise.context.ApplicationScoped;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;

@ApplicationScoped
public class BookDao implements PanacheRepositoryBase<Book, Integer> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.it.panache;

import java.util.List;

import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/book")
public class BookResource {

@Inject
BookDao bookDao;

@Transactional
@GET
@Path("/{name}/{author}")
@Produces(MediaType.APPLICATION_JSON)
public List<Book> addAndListAll(@PathParam("name") String name, @PathParam("author") String author) {
bookDao.persist(new Book(name, author));
return bookDao.listAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.it.panache;

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

import java.util.List;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;

@QuarkusTest
public class JacksonTest {

@Test
public void testJsonIgnoreHonoured() {
List<Book> books = RestAssured.when().get("/book/Berlin/Beevor").then().extract().body().jsonPath().getList(".",
Book.class);
assertThat(books).hasSize(1).filteredOn(book -> book.author != null).isEmpty();
}
}

0 comments on commit 6bc4947

Please sign in to comment.