Skip to content

Commit

Permalink
fix for #486, @JsonProperty not honored for property names
Browse files Browse the repository at this point in the history
  • Loading branch information
fehguy committed Mar 14, 2014
1 parent 6ee5793 commit a56fd29
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,14 @@ class ModelPropertyParser(cls: Class[_], t: Map[String, String] = Map.empty) (im
}

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

if (name == null || name.equals(originalName)) {
name = propAnnoOutput("name").asInstanceOf[String]
}

if(allowableValues == None)
allowableValues = propAnnoOutput("allowableValues").asInstanceOf[Option[AllowableValues]]
if(description == None && propAnnoOutput.contains("description") && propAnnoOutput("description") != null)
Expand All @@ -126,6 +130,8 @@ class ModelPropertyParser(cls: Class[_], t: Map[String, String] = Map.empty) (im
isFieldExists = true
if (!isTransient) isTransient = propAnnoOutput("isTransient").asInstanceOf[Boolean]
if (!isXmlElement) isXmlElement = propAnnoOutput("isXmlElement").asInstanceOf[Boolean]

if (name == null) name = originalName
isJsonProperty = propAnnoOutput("isJsonProperty").asInstanceOf[Boolean]
} catch {
//this means there is no field declared to look for field level annotations.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package converter

import models._

import com.wordnik.swagger.converter._

import com.wordnik.swagger.core.util._

import java.util.Date

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers

@RunWith(classOf[JUnitRunner])
class JsonPropertyModelTest extends FlatSpec with ShouldMatchers {
val models = ModelConverters.readAll(classOf[ModelWithJsonProperty])
JsonSerializer.asJson(models) should be ("""[{"id":"ModelWithJsonProperty","properties":{"theCount":{"type":"integer","format":"int32"}}}]""")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package converter.models;

import com.fasterxml.jackson.annotation.*;
public class ModelWithJsonProperty {
@JsonProperty("theCount")
private Integer count;

public void setCount(Integer count) {
this.count = count;
}

public Integer getCount() {
return count;
}
}

0 comments on commit a56fd29

Please sign in to comment.