Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Skip certain Jackson-annotated properties
Browse files Browse the repository at this point in the history
Getters annotated with @JsonAnyGetter, @JsonIgnore, @JsonUnwrapped or @JsonValue should definitely not have @JsonProperty annotations added to the Builder.

Annotating a Map property with @JsonAnyGetter would ideally result in a @JsonAnySetter annotation on the Builder (issue #104), but skipping the @JsonProperty annotations is critical.
  • Loading branch information
alicederyn committed Jan 22, 2016
1 parent 095e874 commit 27ae878
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import static org.inferred.freebuilder.processor.util.ModelUtils.findAnnotationMirror;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;

import org.inferred.freebuilder.processor.Metadata.Property;
import org.inferred.freebuilder.processor.util.Excerpt;
import org.inferred.freebuilder.processor.util.QualifiedName;
import org.inferred.freebuilder.processor.util.SourceBuilder;

import java.util.Set;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
Expand All @@ -19,6 +22,12 @@ class JacksonSupport {
"com.fasterxml.jackson.databind.annotation.JsonDeserialize";
private static final QualifiedName JSON_PROPERTY =
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonProperty");
/** Annotations which disable automatic generation of JsonProperty annotations. */
private static final Set<QualifiedName> DISABLE_PROPERTY_ANNOTATIONS = ImmutableSet.of(
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonAnyGetter"),
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonIgnore"),
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonUnwrapped"),
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonValue"));

public static Optional<JacksonSupport> create(TypeElement userValueType) {
if (findAnnotationMirror(userValueType, JSON_DESERIALIZE).isPresent()) {
Expand All @@ -34,11 +43,23 @@ public void addJacksonAnnotations(
Optional<AnnotationMirror> annotation = findAnnotationMirror(getterMethod, JSON_PROPERTY);
if (annotation.isPresent()) {
resultBuilder.addAccessorAnnotations(new AnnotationExcerpt(annotation.get()));
} else {
} else if (generateDefaultAnnotations(getterMethod)) {
resultBuilder.addAccessorAnnotations(new JsonPropertyExcerpt(resultBuilder.getName()));
}
}

private static boolean generateDefaultAnnotations(ExecutableElement getterMethod) {
for (AnnotationMirror annotationMirror : getterMethod.getAnnotationMirrors()) {
TypeElement annotationTypeElement =
(TypeElement) (annotationMirror.getAnnotationType().asElement());
QualifiedName annotationType = QualifiedName.of(annotationTypeElement);
if (DISABLE_PROPERTY_ANNOTATIONS.contains(annotationType)) {
return false;
}
}
return true;
}

private static class AnnotationExcerpt implements Excerpt {

private final AnnotationMirror annotation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

Expand All @@ -35,6 +36,8 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.Map;

import javax.lang.model.element.TypeElement;

/** Unit tests for {@link Analyser}. */
Expand Down Expand Up @@ -105,6 +108,23 @@ public void jacksonAnnotationAddedWithImplicitName() throws CannotGenerateCodeEx
assertPropertyHasJsonPropertyAnnotation(property, "fooBar");
}

@Test
public void jsonAnyGetterAnnotationDisablesImplicitProperty() throws CannotGenerateCodeException {
TypeElement dataType = model.newType(
"package com.example;",
"@" + JsonDeserialize.class.getName() + "(builder = DataType.Builder.class)",
"public interface DataType {",
" @" + JsonAnyGetter.class.getName(),
" " + Map.class.getName() + "<Integer, String> getFooBar();",
" class Builder extends DataType_Builder {}",
"}");

Metadata metadata = analyser.analyse(dataType);

Property property = getOnlyElement(metadata.getProperties());
assertThat(property.getAccessorAnnotations()).named("property accessor annotations").isEmpty();
}

private static void assertPropertyHasJsonPropertyAnnotation(
Property property, String propertyName) {
assertThat(property.getAccessorAnnotations()).named("property accessor annotations").hasSize(1);
Expand Down

0 comments on commit 27ae878

Please sign in to comment.