Skip to content

Commit 1f46819

Browse files
author
Chris Birchall
committed
Fix deserialization of "authorizations" fields
Deserializer should expect a Map[String, AuthorizationType], not a List[String].
1 parent 5892fdb commit 1f46819

File tree

4 files changed

+72
-19
lines changed

4 files changed

+72
-19
lines changed

src/main/scala/com/wordnik/swagger/model/LegacySerializers.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ object LegacySerializers {
183183
class OperationSerializer extends CustomSerializer[Operation](formats => ({
184184
case json =>
185185
implicit val fmts: Formats = formats
186+
val authorizations = (json \ "authorizations").extractOpt[Map[String, AuthorizationType]] match {
187+
case Some(m) => m.values.toList
188+
case _ => List.empty
189+
}
186190
Operation(
187191
(json \ "httpMethod").extractOrElse(
188192
(json \ "method").extractOrElse({
@@ -204,7 +208,7 @@ object LegacySerializers {
204208
(json \ "produces").extract[List[String]],
205209
(json \ "consumes").extract[List[String]],
206210
(json \ "protocols").extract[List[String]],
207-
(json \ "authorizations").extract[List[String]],
211+
authorizations,
208212
(json \ "parameters").extract[List[Parameter]],
209213
(json \ "errorResponses").extract[List[ResponseMessage]],
210214
(json \ "deprecated").extractOpt[String]

src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ object SwaggerSerializers {
100100

101101
class ApiListingSerializer extends CustomSerializer[ApiListing](implicit formats => ({
102102
case json =>
103+
val authorizations = (json \ "authorizations").extractOpt[Map[String, AuthorizationType]] match {
104+
case Some(m) => m.values.toList
105+
case _ => List.empty
106+
}
103107
ApiListing(
104108
(json \ "apiVersion").extractOrElse({
105109
!!(json, RESOURCE, "apiVersion", "missing required field", ERROR)
@@ -120,7 +124,7 @@ object SwaggerSerializers {
120124
(json \ "produces").extract[List[String]],
121125
(json \ "consumes").extract[List[String]],
122126
(json \ "protocols").extract[List[String]],
123-
(json \ "authorizations").extract[List[String]],
127+
authorizations,
124128
(json \ "apis").extract[List[ApiDescription]],
125129
(json \ "models").extractOpt[Map[String, Model]],
126130
(json \ "description").extractOpt[String],
@@ -238,6 +242,10 @@ object SwaggerSerializers {
238242
class OperationSerializer extends CustomSerializer[Operation](implicit formats => ({
239243
case json =>
240244

245+
val authorizations = (json \ "authorizations").extractOpt[Map[String, AuthorizationType]] match {
246+
case Some(m) => m.values.toList
247+
case _ => List.empty
248+
}
241249
val t = SwaggerSerializers.jsonSchemaTypeMap.getOrElse(
242250
((json \ "type").extractOrElse(""), (json \ "format").extractOrElse(""))
243251
, (json \ "type").extractOrElse(""))
@@ -291,7 +299,7 @@ object SwaggerSerializers {
291299
(json \ "produces").extract[List[String]],
292300
(json \ "consumes").extract[List[String]],
293301
(json \ "protocols").extract[List[String]],
294-
(json \ "authorizations").extract[List[String]],
302+
authorizations,
295303
(json \ "parameters").extract[List[Parameter]],
296304
(json \ "responseMessages").extract[List[ResponseMessage]],
297305
(json \ "deprecated").extractOpt[String]

src/main/scala/com/wordnik/swagger/model/SwaggerModels.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ case class ApiListing (
7676
var produces: List[String] = List.empty,
7777
var consumes: List[String] = List.empty,
7878
var protocols: List[String] = List.empty,
79-
var authorizations: List[String] = List.empty,
79+
var authorizations: List[AuthorizationType] = List.empty,
8080
apis: List[ApiDescription] = List(),
8181
models: Option[Map[String, Model]] = None,
8282
description: Option[String] = None,
@@ -97,7 +97,7 @@ case class Operation (
9797
var produces: List[String] = List.empty,
9898
var consumes: List[String] = List.empty,
9999
var protocols: List[String] = List.empty,
100-
var authorizations: List[String] = List.empty,
100+
var authorizations: List[AuthorizationType] = List.empty,
101101
parameters: List[Parameter] = List.empty,
102102
responseMessages: List[ResponseMessage] = List.empty,
103103
`deprecated`: Option[String] = None)

src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@ class ResourceListingSerializersTest extends FlatSpec with ShouldMatchers {
3131
p.apiVersion should be ("1.2.3")
3232
p.swaggerVersion should be ("1.2")
3333
p.apis.size should be (0)
34+
p.authorizations.size should be (0)
3435
}
3536
case _ => fail("wrong type returned, should be ResourceListing")
3637
}
3738
}
3839

39-
it should "serialize an ApiListingReference with no apis" in {
40-
val l = ApiListingReference("/foo/bar", Some("the description"))
41-
write(l) should be ("""{"path":"/foo/bar","description":"the description"}""")
42-
}
43-
4440
it should "deserialize an ResourceListing" in {
4541
val jsonString = """
4642
{
@@ -54,7 +50,8 @@ class ResourceListingSerializersTest extends FlatSpec with ShouldMatchers {
5450
"path":"/c",
5551
"description":"path c apis"
5652
}
57-
]
53+
],
54+
"authorizations": {}
5855
}
5956
"""
6057
val json = parse(jsonString)
@@ -63,15 +60,12 @@ class ResourceListingSerializersTest extends FlatSpec with ShouldMatchers {
6360
p.apiVersion should be ("1.2.3")
6461
p.swaggerVersion should be ("1.2")
6562
p.apis.size should be (2)
63+
p.authorizations.size should be (0)
6664
}
6765
case _ => fail("wrong type returned, should be ResourceListing")
6866
}
6967
}
7068

71-
it should "serialize an ApiListingReference" in {
72-
val l = ApiListingReference("/foo/bar", Some("the description"))
73-
write(l) should be ("""{"path":"/foo/bar","description":"the description"}""")
74-
}
7569
}
7670

7771
@RunWith(classOf[JUnitRunner])
@@ -102,9 +96,52 @@ class ApiListingReferenceSerializersTest extends FlatSpec with ShouldMatchers {
10296
}
10397

10498
@RunWith(classOf[JUnitRunner])
105-
class ApiDescriptionSerializersTest extends FlatSpec with ShouldMatchers {
99+
class ApiListingSerializersTest extends FlatSpec with ShouldMatchers {
106100
implicit val formats = SwaggerSerializers.formats("1.2")
107101

102+
it should "deserialize an ApiListing" in {
103+
val jsonString = """
104+
{
105+
"apiVersion":"1.2.3",
106+
"swaggerVersion":"1.2",
107+
"basePath": "/foo/bar",
108+
"resourcePath": "/a/b",
109+
"produces": [ "application/json" ],
110+
"authorizations": {},
111+
"apis": []
112+
}
113+
"""
114+
val json = parse(jsonString)
115+
json.extract[ApiListing] match {
116+
case p: ApiListing=> {
117+
p.apiVersion should be ("1.2.3")
118+
p.swaggerVersion should be ("1.2")
119+
p.basePath should be ("/foo/bar")
120+
p.resourcePath should be ("/a/b")
121+
p.produces should be (List("application/json"))
122+
p.authorizations.size should be (0)
123+
p.models should be (None)
124+
p.description should be (None)
125+
p.position should be (0)
126+
}
127+
case _ => fail("wrong type returned, should be ApiListing")
128+
}
129+
}
130+
131+
it should "serialize an ApiListing" in {
132+
val l = ApiListing(
133+
apiVersion = "1.2.3",
134+
swaggerVersion = "1.2",
135+
basePath = "/foo/bar",
136+
resourcePath = "/a/b"
137+
)
138+
write(l) should be ("""{"apiVersion":"1.2.3","resourcePath":"/a/b","swaggerVersion":"1.2","basePath":"/foo/bar"}""")
139+
}
140+
}
141+
142+
@RunWith(classOf[JUnitRunner])
143+
class ApiDescriptionSerializersTest extends FlatSpec with ShouldMatchers {
144+
implicit val formats = SwaggerSerializers.formats("1.2")
108145
it should "deserialize an ApiDescription with no ops" in {
109146
val jsonString = """
110147
{
@@ -151,7 +188,8 @@ class ApiDescriptionSerializersTest extends FlatSpec with ShouldMatchers {
151188
"enum":["a","b","c"],
152189
"paramType":"query"
153190
}
154-
]
191+
],
192+
"authorizations":{}
155193
}
156194
]
157195
}
@@ -179,6 +217,7 @@ class ApiDescriptionSerializersTest extends FlatSpec with ShouldMatchers {
179217
m.dataType should be ("string")
180218
m.paramType should be ("query")
181219
})
220+
op.authorizations.size should be (0)
182221
})
183222
}
184223
case _ => fail("wrong type returned, should be ApiDescription")
@@ -230,7 +269,8 @@ class OperationSerializersTest extends FlatSpec with ShouldMatchers {
230269
"enum":["a","b","c"],
231270
"paramType":"query"
232271
}
233-
]
272+
],
273+
"authorizations":{}
234274
}
235275
"""
236276
val json = parse(jsonString)
@@ -252,6 +292,7 @@ class OperationSerializersTest extends FlatSpec with ShouldMatchers {
252292
m.dataType should be ("string")
253293
m.paramType should be ("query")
254294
})
295+
op.authorizations.size should be (0)
255296
}
256297
case _ => fail("wrong type returned, should be Operation")
257298
}
@@ -833,4 +874,4 @@ class AllowableValuesSerializersTest extends FlatSpec with ShouldMatchers {
833874
val l = AllowableRangeValues("-1", "3")
834875
write(l) should be ("""{"valueType":"RANGE","min":"-1","max":"3"}""")
835876
}
836-
}
877+
}

0 commit comments

Comments
 (0)