forked from iheartradio/play-swagger
/
SwaggerSpecGeneratorSpec.scala
462 lines (351 loc) · 16.7 KB
/
SwaggerSpecGeneratorSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package com.iheart.playSwagger
import com.iheart.playSwagger.Domain.{ CustomTypeMapping, CustomMappings }
import org.specs2.mutable.Specification
import play.api.libs.json._
case class Track(name: String, genre: Option[String], artist: Artist, related: Seq[Artist], numbers: Seq[Int])
case class Artist(name: String, age: Int)
case class Student(name: String, teacher: Option[Teacher])
case class Teacher(name: String)
case class Animal(name: String, keeper: Keeper)
case class Keeper(internalFieldName1: String, internalFieldName2: Int)
case class PolymorphicContainer(item: PolymorphicItem)
trait PolymorphicItem
case class EnumContainer(javaEnum: SampleJavaEnum, scalaEnum: SampleScalaEnum.SampleScalaEnum)
case class AllOptional(a: Option[String], b: Option[String])
class SwaggerSpecGeneratorSpec extends Specification {
implicit val cl = getClass.getClassLoader
val gen = SwaggerSpecGenerator()
"full path" >> {
"combine routePath with prefix" >> {
gen.fullPath("/p", "d") === "/p/d"
}
"ignore prefix when it's just a /" >> {
gen.fullPath("/", "d") === "/d"
}
"avoid double slash at the beginning" >> {
gen.fullPath("/", "/d") === "/d"
}
"avoid double slash at the middle" >> {
gen.fullPath("p/", "/d") === "/p/d"
}
"allow internal double slash" >> {
gen.fullPath("p/", "/d//c") === "/p/d//c"
}
"respect trailing slash from previous element when in route path is root /" >> {
gen.fullPath("p/", "/") === "/p/"
}
"remove top level trailing slash" >> {
gen.fullPath("p", "/") === "/p"
}
}
"getCfgFile" >> {
"valid swagger-custom-mappings yml" >> {
val result = gen.readCfgFile[CustomMappings]("swagger-custom-mappings.yml")
result must beSome[CustomMappings]
val mappings = result.get
mappings.size must be_>(2)
mappings.head.`type` mustEqual "java\\.time\\.LocalDate"
mappings.head.specAsParameter === List(Json.obj("type" → "string", "format" → "date"))
mappings.head.specAsProperty must beEmpty
}
"invalid swagger-settings yml" >> {
gen.readCfgFile[CustomMappings]("swagger-custom-mappings_invalid.yml") must throwA[JsResultException]
}
}
}
class SwaggerSpecGeneratorIntegrationSpec extends Specification {
implicit val cl = getClass.getClassLoader
"integration" >> {
lazy val defaultRoutesFile = SwaggerSpecGenerator(ExcludingDomainQualifier).generate()
"Use default routes file when no argument is given" >> {
val json = defaultRoutesFile.get
(json \ "paths" \ "/player/{pid}/context/{bid}").asOpt[JsObject] must beSome
}
lazy val json = SwaggerSpecGenerator("com.iheart").generate("test.routes").get
lazy val pathJson = json \ "paths"
lazy val definitionsJson = json \ "definitions"
lazy val postBodyJson = (pathJson \ "/post-body" \ "post").as[JsObject]
lazy val artistJson = (pathJson \ "/api/artist/{aid}/playedTracks/recent" \ "get").as[JsObject]
lazy val stationJson = (pathJson \ "/api/station/{sid}/playedTracks/last" \ "get").as[JsObject]
lazy val addTrackJson = (pathJson \ "/api/station/playedTracks" \ "post").as[JsObject]
lazy val playerJson = (pathJson \ "/api/player/{pid}/context/{bid}" \ "get").as[JsObject]
lazy val playerAddTrackJson = (pathJson \ "/api/player/{pid}/playedTracks" \ "post").as[JsObject]
lazy val resourceJson = (pathJson \ "/api/resource").as[JsObject]
lazy val allOptionalDefJson = (definitionsJson \ "com.iheart.playSwagger.AllOptional").as[JsObject]
lazy val artistDefJson = (definitionsJson \ "com.iheart.playSwagger.Artist").as[JsObject]
lazy val trackJson = (definitionsJson \ "com.iheart.playSwagger.Track").as[JsObject]
lazy val studentJson = (definitionsJson \ "com.iheart.playSwagger.Student").asOpt[JsObject]
lazy val teacherJson = (definitionsJson \ "com.iheart.playSwagger.Teacher").asOpt[JsObject]
lazy val polymorphicContainerJson = (definitionsJson \ "com.iheart.playSwagger.PolymorphicContainer").asOpt[JsObject]
lazy val polymorphicItemJson = (definitionsJson \ "com.iheart.playSwagger.PolymorphicItem").asOpt[JsObject]
lazy val enumContainerJson = (definitionsJson \ "com.iheart.playSwagger.EnumContainer").asOpt[JsObject]
lazy val overriddenDictTypeJson = (definitionsJson \ "com.iheart.playSwagger.DictType").as[JsObject]
def parametersOf(json: JsValue): Seq[JsValue] = {
(json \ "parameters").as[JsArray].value
}
"reads json comment" >> {
(artistJson \ "summary").as[String] === "get recent tracks"
}
"reads optional field" >> {
val limitParamJson = (artistJson \ "parameters").as[JsArray].value(1).as[JsObject]
(limitParamJson \ "name").as[String] === "limit"
(limitParamJson \ "format").as[String] === "int32"
(limitParamJson \ "required").as[Boolean] === false
}
"merge comment in" >> {
val sidPara = (stationJson \ "parameters").as[JsArray].value(0).as[JsObject]
(sidPara \ "name").as[String] === "sid"
(sidPara \ "description").as[String] === "station id"
(sidPara \ "type").as[String] === "integer"
(sidPara \ "required").as[Boolean] === true
}
"override generated with comment in" >> {
val sidPara = (stationJson \ "parameters").as[JsArray].value(0).as[JsObject]
(sidPara \ "format").as[String] === "int"
}
"not generate consumes in get" >> {
(artistJson \ "consumes").toOption must beEmpty
}
"read definition from referenceTypes" >> {
(trackJson \ "properties" \ "name" \ "type").as[String] === "string"
}
"read schema of referenced type" >> {
(trackJson \ "properties" \ "artist" \ "$ref").asOpt[String] === Some("#/definitions/com.iheart.playSwagger.Artist")
}
"read seq of referenced type" >> {
val relatedProp = (trackJson \ "properties" \ "related")
(relatedProp \ "type").asOpt[String] === Some("array")
(relatedProp \ "items" \ "$ref").asOpt[String] === Some("#/definitions/com.iheart.playSwagger.Artist")
}
"read seq of primitive type" >> {
val numberProps = (trackJson \ "properties" \ "numbers")
(numberProps \ "type").asOpt[String] === Some("array")
(numberProps \ "items" \ "type").asOpt[String] === Some("integer")
}
"read definition from referenced referenceTypes" >> {
(artistDefJson \ "properties" \ "age" \ "type").as[String] === "integer"
}
"read trait with container" >> {
polymorphicContainerJson must beSome[JsObject]
(polymorphicContainerJson.get \ "properties" \ "item" \ "$ref").asOpt[String] === Some("#/definitions/com.iheart.playSwagger.PolymorphicItem")
polymorphicItemJson must beSome[JsObject]
}
"read java enum with container" >> {
enumContainerJson must beSome[JsObject]
(enumContainerJson.get \ "properties" \ "javaEnum" \ "enum").asOpt[Seq[String]] === Some(Seq("DISABLED", "ACTIVE"))
}
"read scala enum with container" >> {
enumContainerJson must beSome[JsObject]
(enumContainerJson.get \ "properties" \ "scalaEnum" \ "enum").asOpt[Seq[String]] === Some(Seq("One", "Two"))
}
"definition property have no name" >> {
(artistDefJson \ "properties" \ "age" \ "name").toOption must beEmpty
}
"generate post path with consumes" >> {
(addTrackJson \ "consumes").as[JsArray].value(0).as[String] === "application/json"
}
"generate body parameter with in " >> {
val params = parametersOf(addTrackJson)
params.length === 1
(params.head \ "in").asOpt[String] === Some("body")
(params.head \ "schema" \ "$ref").asOpt[String] === Some("#/definitions/com.iheart.playSwagger.Track")
}
"generate body parameter without in if already provided" >> {
val params = parametersOf((pathJson \ "/iWantAQueryBody" \ "post").as[JsObject])
params.length === 1
(params.head \ "in").asOpt[String] === Some("query")
}
"does not generate for end points marked as hidden" >> {
(pathJson \ "/api/station/hidden" \ "get").toOption must beEmpty
}
"generate path correctly with missing type (String by default) in controller description" >> {
(playerJson \ "summary").asOpt[String] === Some("get player")
}
"generate tags for an endpoint" >> {
(playerJson \ "tags").asOpt[Seq[String]] === Some(Seq("player"))
}
"generate parameter for more path" >> {
parametersOf(playerJson).length === 2
}
"generate tags definition" >> {
val tags = (json \ "tags").asOpt[Seq[JsObject]]
tags must beSome[Seq[JsObject]]
tags.get.map(tO ⇒ (tO \ "name").as[String]).sorted must containAllOf(Seq("customResource", "liveMeta", "player", "resource"))
}
"merge tag description from base" >> {
val tags = (json \ "tags").asOpt[Seq[JsObject]]
tags must beSome[Seq[JsObject]]
val playTag = tags.get.find(t ⇒ (t \ "name").as[String] == "player")
(playTag.get \ "description").asOpt[String] === Some("this is player api")
}
"get both body and url params" >> {
val params = (playerAddTrackJson \ "parameters").as[JsArray].value
params.length === 2
params.map(p ⇒ (p \ "name").as[String]).toSet === Set("body", "pid")
}
"get parameter type of" >> {
val playerSearchJson = (pathJson \ "/api/player/{pid}/tracks/search" \ "get").as[JsObject]
val params: Seq[JsValue] = parametersOf(playerSearchJson)
params.length === 2
"path params" >> {
(params.head \ "name").as[String] === "pid"
(params.head \ "in").as[String] === "path"
}
"query params" >> {
(params.last \ "in").as[String] === "query"
}
}
"allow multiple routes with the same path" >> {
resourceJson.keys.toSet === Set("get", "post", "delete", "put")
}
"parse controller with custom namespace" >> {
(pathJson \ "/api/customResource" \ "get").asOpt[JsObject] must beSome[JsObject]
}
"parse class referenced in option type" >> {
studentJson must beSome[JsObject]
teacherJson must beSome[JsObject]
(teacherJson.get \ "properties" \ "name" \ "type").as[String] === "string"
}
"parse param with default value as optional field" >> {
val endPointJson = (pathJson \ "/api/students/defaultValueParam" \ "put").asOpt[JsObject]
endPointJson must beSome[JsObject]
val paramJson: JsValue = parametersOf(endPointJson.get).head
(paramJson \ "name").as[String] === "aFlag"
"set required as false" >> {
(paramJson \ "required").as[Boolean] === false
}
"set in as query" >> {
(paramJson \ "in").as[String] === "query"
}
"set default value" >> {
(paramJson \ "default").as[Boolean] === true
}
}
"parse param with default triple quoted string value as optional field" >> {
val endPointJson = (pathJson \ "/api/students/defaultValueParamString3" \ "put").asOpt[JsObject]
endPointJson must beSome[JsObject]
val paramJson: JsValue = parametersOf(endPointJson.get).head
(paramJson \ "name").as[String] === "strFlag"
"set required as false" >> {
(paramJson \ "required").as[Boolean] === false
}
"set in as query" >> {
(paramJson \ "in").as[String] === "query"
}
"set default value" >> {
(paramJson \ "default").as[String] === """defaultValue with triple quotes"""
}
}
"parse param with default string value as optional field" >> {
val endPointJson = (pathJson \ "/api/students/defaultValueParamString" \ "put").asOpt[JsObject]
endPointJson must beSome[JsObject]
val paramJson: JsValue = parametersOf(endPointJson.get).head
(paramJson \ "name").as[String] === "strFlag"
"set required as false" >> {
(paramJson \ "required").as[Boolean] === false
}
"set in as query" >> {
(paramJson \ "in").as[String] === "query"
}
"set default value" >> {
(paramJson \ "default").as[String] === "defaultValue"
}
}
"should contain schemas in responses" >> {
(postBodyJson \ "responses" \ "200" \ "schema" \ "$ref").asOpt[String] === Some("#/definitions/com.iheart.playSwagger.FooWithSeq2")
}
"should contain schemas in requests" >> {
val paramJson = parametersOf(addTrackJson).head
(paramJson \ "schema" \ "$ref").asOpt[String] === Some("#/definitions/com.iheart.playSwagger.Track")
}
"excluded domain object should contain only spec from swagger.json" >> {
overriddenDictTypeJson === Json.obj(
"type" → "object",
"properties" → Json.obj(
"id" → Json.obj("type" → "string"),
"value" → Json.obj("type" → "string")))
}
"definition properties does not contain 'required' boolean field" >> {
definitionsJson.as[JsObject].values.forall { definition ⇒
(definition \ "properties").as[JsObject].values.forall { property ⇒
(property \ "required").toOption === None
}
}
}
"definitions exposes 'required' array if there are required properties" >> {
val requiredFields = Seq("name", "artist", "related", "numbers")
(trackJson \ "required").as[Seq[String]] must contain(allOf(requiredFields: _*).exactly)
}
"definitions does not expose 'required' array if there are no required properties" >> {
(allOptionalDefJson \ "required").asOpt[Seq[String]] === None
}
"handle multiple levels of includes" >> {
val tags = (pathJson \ "/level1/level2/level3" \ "get" \ "tags").asOpt[Seq[String]]
tags must beSome.which(_ == Seq("level2"))
}
"not contain tags that are empty" >> {
val tags = (json \ "tags").as[Seq[JsObject]]
.map(o ⇒ (o \ "name").as[String])
tags must not contain "no"
}
"handle type aliases in post body" >> {
val properties = (definitionsJson \ "com.iheart.playSwagger.FooWithSeq2" \ "properties").as[JsObject]
(properties \ "abc1" \ "items" \ "type").as[String] === "string"
(properties \ "abc2" \ "items" \ "items" \ "type").as[String] === "integer"
}
"accept parameter references" >> {
val parameters = (pathJson \ "/references/magic/echoMagic/{type}" \ "post" \ "parameters").as[Seq[JsObject]]
parameters must contain((entry: JsObject) ⇒
entry.value.get("$ref").contains(JsString("#/parameters/magic")))
.exactly(1.times)
parameters must contain((entry: JsObject) ⇒
entry.value.get("name").contains(JsString("notMagic")))
.exactly(1.times)
}
"hide parameter set to ignore in the custom type mappings" >> {
val parameters = (pathJson \ "/zoo/zone/{zid}/animals/{aid}" \ "get" \ "parameters").as[Seq[JsObject]]
parameters.size === 1
parameters must contain((entry: JsObject) ⇒
entry.value.get("name").contains(JsString("aid")))
.exactly(1.times).not
}
"map parameter set according to the custom type mappings" >> {
val parameters = (pathJson \ "/zoo/zone/{zid}/animals/{aid}" \ "get" \ "parameters").as[Seq[JsObject]]
parameters.size === 1
parameters.head.value.get("name") === Some(JsString("zid"))
parameters.head.value.get("type") === Some(JsString("string"))
parameters.head.value.get("required") === Some(JsBoolean(true))
}
"generate definition reference according to custom type mappings" >> {
val properties = (definitionsJson \ "com.iheart.playSwagger.Animal" \ "properties").as[JsObject]
(properties \ "keeper" \ "$ref").as[String] === "#/definitions/Keeper"
(definitionsJson \ "com.iheart.playSwagger.Keeper").toOption must beEmpty
}
"custom type mappings in definition should be included in required" >> {
val required = (definitionsJson \ "com.iheart.playSwagger.Animal" \ "required").as[JsArray]
required.value must contain(JsString("keeper"))
}
"add operation ids" >> {
(addTrackJson \ "operationId").as[String] ==== "addPlayedTracks"
}
"should maintain route file order" >> {
// use students routes
val pathJsonObj = pathJson.as[JsObject]
val fieldKeys: Seq[String] = pathJsonObj.fields.map(_._1)
val fieldsWithIndexMap = fieldKeys.zipWithIndex.toMap
val first = fieldsWithIndexMap.get("/api/students/{name}")
val second = fieldsWithIndexMap.get("/api/students/defaultValueParam")
val third = fieldsWithIndexMap.get("/api/students/defaultValueParamString")
val fourth = fieldsWithIndexMap.get("/api/students/defaultValueParamString3")
// all paths must be retrieved
first must beSome[Int]
second must beSome[Int]
third must beSome[Int]
fourth must beSome[Int]
// must be in exact order with nothing inbetween
second.get - first.get === 1
third.get - second.get === 1
fourth.get - third.get === 1
}
}
}