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

Support for Jackson field-renames-getter heuristic #461

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class ModelPropertyParser(cls: Class[_], t: Map[String, String] = Map.empty) (im

var required = processedAnnotations("required").asInstanceOf[Boolean]
var position = processedAnnotations("position").asInstanceOf[Int]
var renamed = (processedAnnotations("name").asInstanceOf[String] != processedAnnotations("originalName").asInstanceOf[String])

var description = {
if(processedAnnotations.contains("description") && processedAnnotations("description") != null)
Expand All @@ -110,10 +111,13 @@ class ModelPropertyParser(cls: Class[_], t: Map[String, String] = Map.empty) (im
}

try {
val fieldAnnotations = getDeclaredField(this.cls, name).getAnnotations()
val fieldAnnotations = getDeclaredField(this.cls, originalName).getAnnotations()
var propAnnoOutput = processAnnotations(name, fieldAnnotations)
var propPosition = propAnnoOutput("position").asInstanceOf[Int]

if(!renamed && propAnnoOutput("isJsonProperty").asInstanceOf[Boolean])
name = propAnnoOutput("name").asInstanceOf[String]

if(allowableValues == None)
allowableValues = propAnnoOutput("allowableValues").asInstanceOf[Option[AllowableValues]]
if(description == None && propAnnoOutput.contains("description") && propAnnoOutput("description") != null)
Expand Down Expand Up @@ -261,6 +265,7 @@ class ModelPropertyParser(cls: Class[_], t: Map[String, String] = Map.empty) (im
output += "isXmlElement" -> isXmlElement
output += "isDocumented" -> isDocumented
output += "isJsonProperty" -> isJsonProperty
output += "originalName" -> name
output += "name" -> updatedName
output += "required" -> required
output += "defaultValue" -> defaultValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ class ModelPropertyParserTest extends FlatSpec with ShouldMatchers {
val parser = new ModelPropertyParser(cls)
parser.parse
}

it should "mimic Jackson's field->method annotation inheritance" in {
val cls = classOf[ModelWithJacksonAnnotatedPrivateField]
implicit val properties = new scala.collection.mutable.LinkedHashMap[String, ModelProperty]
val parser = new ModelPropertyParser(cls)
parser.parse

properties.keySet.size should be (4)
properties.getOrElse("rawFieldProp", null) should not be (null)
properties.getOrElse("renamedJacksonProp", null) should not be (null)
properties.getOrElse("renamedJacksonMethod", null) should not be (null)
properties.getOrElse("fieldLevelJacksonProp", null) should not be (null)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package converter.models;

import com.fasterxml.jackson.annotation.*;

public class ModelWithJacksonAnnotatedPrivateField {

@JsonProperty("hiddenFieldLevelJacksonProp") private String hiddenFieldProp;
@JsonProperty public String rawFieldProp;
@JsonProperty("fieldLevelJacksonProp") public String renamableFieldProp;

@JsonProperty("renamedJacksonProp") private String renamableJackonProp;
@JsonProperty("renamedJacksonProp2") private String renamableJackonProp2;
@JsonIgnore private String ignoredJacksonProp;

public String getIgnoredJacksonProp() {
return ignoredJacksonProp;
}

public String getRenamableJackonProp() {
return renamableJackonProp;
}

@JsonProperty("renamedJacksonMethod") public String getRenamableJackonProp2() {
return renamableJackonProp2;
}

@JsonIgnore public String getIgnoredJacksonMethod() {
return "";
}

}