Skip to content

Commit a56fd29

Browse files
committed
fix for #486, @JsonProperty not honored for property names
1 parent 6ee5793 commit a56fd29

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

modules/swagger-core/src/main/scala/com/wordnik/swagger/converter/ModelPropertyParser.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,14 @@ class ModelPropertyParser(cls: Class[_], t: Map[String, String] = Map.empty) (im
113113
}
114114

115115
try {
116-
val fieldAnnotations = getDeclaredField(this.cls, name).getAnnotations()
117-
var propAnnoOutput = processAnnotations(name, fieldAnnotations)
116+
val fieldAnnotations = getDeclaredField(this.cls, originalName).getAnnotations()
117+
var propAnnoOutput = processAnnotations(originalName, fieldAnnotations)
118118
var propPosition = propAnnoOutput("position").asInstanceOf[Int]
119119

120+
if (name == null || name.equals(originalName)) {
121+
name = propAnnoOutput("name").asInstanceOf[String]
122+
}
123+
120124
if(allowableValues == None)
121125
allowableValues = propAnnoOutput("allowableValues").asInstanceOf[Option[AllowableValues]]
122126
if(description == None && propAnnoOutput.contains("description") && propAnnoOutput("description") != null)
@@ -126,6 +130,8 @@ class ModelPropertyParser(cls: Class[_], t: Map[String, String] = Map.empty) (im
126130
isFieldExists = true
127131
if (!isTransient) isTransient = propAnnoOutput("isTransient").asInstanceOf[Boolean]
128132
if (!isXmlElement) isXmlElement = propAnnoOutput("isXmlElement").asInstanceOf[Boolean]
133+
134+
if (name == null) name = originalName
129135
isJsonProperty = propAnnoOutput("isJsonProperty").asInstanceOf[Boolean]
130136
} catch {
131137
//this means there is no field declared to look for field level annotations.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package converter
2+
3+
import models._
4+
5+
import com.wordnik.swagger.converter._
6+
7+
import com.wordnik.swagger.core.util._
8+
9+
import java.util.Date
10+
11+
import org.junit.runner.RunWith
12+
import org.scalatest.junit.JUnitRunner
13+
import org.scalatest.FlatSpec
14+
import org.scalatest.matchers.ShouldMatchers
15+
16+
@RunWith(classOf[JUnitRunner])
17+
class JsonPropertyModelTest extends FlatSpec with ShouldMatchers {
18+
val models = ModelConverters.readAll(classOf[ModelWithJsonProperty])
19+
JsonSerializer.asJson(models) should be ("""[{"id":"ModelWithJsonProperty","properties":{"theCount":{"type":"integer","format":"int32"}}}]""")
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package converter.models;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
public class ModelWithJsonProperty {
5+
@JsonProperty("theCount")
6+
private Integer count;
7+
8+
public void setCount(Integer count) {
9+
this.count = count;
10+
}
11+
12+
public Integer getCount() {
13+
return count;
14+
}
15+
}

0 commit comments

Comments
 (0)