Skip to content

Commit b0d0dc3

Browse files
committed
small refactor on nullSafe list/map support
1 parent c2ee200 commit b0d0dc3

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
301301
}
302302

303303
private def nullSafeMap[K, V](map: java.util.Map[K, V]): Map[K, V] = Option(map) match {
304-
case None => Map[K, V]()
304+
case None => Map.empty[K, V]
305305
case Some(m) => m.asScala.toMap
306306
}
307307
}

src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
3333
val stringOpt = model.value.getProperties().get("stringOpt")
3434
stringOpt should not be (null)
3535
stringOpt.isInstanceOf[StringSchema] should be(true)
36-
nullSafeList(stringOpt.getRequired) shouldBe empty
36+
nullSafeSeq(stringOpt.getRequired) shouldBe empty
3737
val stringWithDataType = model.value.getProperties().get("stringWithDataTypeOpt")
3838
stringWithDataType should not be (null)
3939
stringWithDataType shouldBe a [StringSchema]
40-
nullSafeList(stringWithDataType.getRequired) shouldBe empty
40+
nullSafeSeq(stringWithDataType.getRequired) shouldBe empty
4141

4242
val ipAddress = model.value.getProperties().get("ipAddress")
4343
ipAddress should not be (null)
4444
ipAddress shouldBe a[StringSchema]
4545
ipAddress.getDescription shouldBe "An IP address"
4646
ipAddress.getFormat shouldBe "IPv4 or IPv6"
47-
nullSafeList(ipAddress.getRequired) shouldBe empty
47+
nullSafeSeq(ipAddress.getRequired) shouldBe empty
4848
}
4949

5050
it should "process Option[Model] as Model" in {
@@ -68,7 +68,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
6868
model.value.getProperties should not be (null)
6969
val field = model.value.getProperties().get("field")
7070
field shouldBe a [NumberSchema]
71-
nullSafeList(model.value.getRequired) should not be empty
71+
nullSafeSeq(model.value.getRequired) should not be empty
7272
}
7373

7474
it should "process Model with Scala BigInt as Number" in {
@@ -81,7 +81,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
8181
model.value.getProperties should not be (null)
8282
val field = model.value.getProperties().get("field")
8383
field shouldBe a [IntegerSchema]
84-
nullSafeList(model.value.getRequired) should not be empty
84+
nullSafeSeq(model.value.getRequired) should not be empty
8585
}
8686

8787
it should "process Model with Scala Option BigDecimal" in {
@@ -93,7 +93,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
9393
val optBigDecimal = model.value.getProperties().get("optBigDecimal")
9494
optBigDecimal should not be (null)
9595
optBigDecimal shouldBe a [NumberSchema]
96-
nullSafeList(model.value.getRequired) shouldBe empty
96+
nullSafeSeq(model.value.getRequired) shouldBe empty
9797
}
9898

9999
it should "process Model with Scala Option BigInt" in {
@@ -105,7 +105,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
105105
val optBigInt = model.value.getProperties().get("optBigInt")
106106
optBigInt should not be (null)
107107
optBigInt shouldBe a [IntegerSchema]
108-
nullSafeList(model.value.getRequired) shouldBe empty
108+
nullSafeSeq(model.value.getRequired) shouldBe empty
109109
}
110110

111111
it should "process Model with Scala Option Int" in {
@@ -122,7 +122,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
122122
optInt shouldBe a[IntegerSchema]
123123
optInt.asInstanceOf[IntegerSchema].getFormat shouldEqual "int32"
124124
}
125-
nullSafeList(model.value.getRequired) shouldBe empty
125+
nullSafeSeq(model.value.getRequired) shouldBe empty
126126
}
127127

128128
it should "process Model with nested Scala Option Int" in {
@@ -139,7 +139,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
139139
optInt shouldBe a[IntegerSchema]
140140
optInt.asInstanceOf[IntegerSchema].getFormat shouldEqual "int32"
141141
}
142-
nullSafeList(model.value.getRequired) shouldBe empty
142+
nullSafeSeq(model.value.getRequired) shouldBe empty
143143
}
144144

145145
it should "process Model without any properties" in {
@@ -167,7 +167,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
167167
optInt.asInstanceOf[IntegerSchema].getFormat shouldEqual "int32"
168168
}
169169
optInt.getDescription shouldBe "This is an optional int"
170-
nullSafeList(model.value.getRequired) shouldBe empty
170+
nullSafeSeq(model.value.getRequired) shouldBe empty
171171
}
172172

173173
it should "process Model with Scala Option Int with Schema Override" in {
@@ -185,7 +185,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
185185
optInt.asInstanceOf[IntegerSchema].getFormat shouldEqual "int32"
186186
}
187187
optInt.getDescription shouldBe "This is an optional int"
188-
nullSafeList(model.value.getRequired) shouldBe empty
188+
nullSafeSeq(model.value.getRequired) shouldBe empty
189189
}
190190

191191
it should "allow annotation to override required with Scala Option Int" in {
@@ -202,7 +202,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
202202
optInt shouldBe a[IntegerSchema]
203203
optInt.asInstanceOf[IntegerSchema].getFormat shouldEqual "int32"
204204
}
205-
nullSafeList(model.value.getRequired) shouldEqual Seq("optInt")
205+
nullSafeSeq(model.value.getRequired) shouldEqual Seq("optInt")
206206
}
207207

208208
it should "process Model with Scala Option Long" in {
@@ -219,7 +219,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
219219
optLong shouldBe a[IntegerSchema]
220220
optLong.asInstanceOf[IntegerSchema].getFormat shouldEqual "int64"
221221
}
222-
nullSafeList(model.value.getRequired) shouldBe empty
222+
nullSafeSeq(model.value.getRequired) shouldBe empty
223223
}
224224

225225
it should "process Model with Scala Option Long with Schema Override" in {
@@ -232,7 +232,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
232232
optLong should not be (null)
233233
optLong shouldBe a [IntegerSchema]
234234
optLong.asInstanceOf[IntegerSchema].getFormat shouldEqual "int64"
235-
nullSafeList(model.value.getRequired) shouldBe empty
235+
nullSafeSeq(model.value.getRequired) shouldBe empty
236236
}
237237

238238
it should "process Model with Scala Option Long with Schema Int Override" in {
@@ -245,7 +245,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
245245
optLong should not be (null)
246246
optLong shouldBe a[IntegerSchema]
247247
optLong.asInstanceOf[IntegerSchema].getFormat shouldEqual "int32"
248-
nullSafeList(model.value.getRequired) shouldBe empty
248+
nullSafeSeq(model.value.getRequired) shouldBe empty
249249
}
250250

251251
it should "process Model with Scala Option Boolean" in {
@@ -257,7 +257,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
257257
val optBoolean = model.value.getProperties().get("optBoolean")
258258
optBoolean should not be (null)
259259
optBoolean shouldBe a [Schema[_]]
260-
nullSafeList(model.value.getRequired) shouldBe empty
260+
nullSafeSeq(model.value.getRequired) shouldBe empty
261261
}
262262

263263
it should "process Model with Scala Option Boolean with Schema Override" in {
@@ -269,7 +269,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
269269
val optBoolean = model.value.getProperties().get("optBoolean")
270270
optBoolean should not be (null)
271271
optBoolean shouldBe a [BooleanSchema]
272-
nullSafeList(model.value.getRequired) shouldBe empty
272+
nullSafeSeq(model.value.getRequired) shouldBe empty
273273
}
274274

275275
it should "process all properties as required barring Option[_] or if overridden in annotation" in {
@@ -294,7 +294,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
294294
val forcedOptional = model.getProperties().get("forcedOptional")
295295
forcedOptional should not be (null)
296296

297-
val requiredItems = nullSafeList(model.getRequired)
297+
val requiredItems = nullSafeSeq(model.getRequired)
298298
requiredItems shouldBe List("forcedRequired", "required")
299299
}
300300

@@ -320,7 +320,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
320320
val forcedOptional = model.getProperties().get("forcedOptional")
321321
forcedOptional should not be (null)
322322

323-
val requiredItems = nullSafeList(model.getRequired)
323+
val requiredItems = nullSafeSeq(model.getRequired)
324324
requiredItems shouldBe List("forcedRequired", "required")
325325
}
326326

@@ -345,7 +345,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
345345
model.value.getProperties should not be (null)
346346
val field = model.value.getProperties.get("field")
347347
field shouldBe a [StringSchema]
348-
nullSafeList(model.value.getRequired) shouldEqual Seq("field")
348+
nullSafeSeq(model.value.getRequired) shouldEqual Seq("field")
349349
}
350350

351351
it should "process Model with Scala BigInt with annotation" in {
@@ -356,7 +356,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
356356
model.value.getProperties should not be (null)
357357
val field = model.value.getProperties.get("field")
358358
field shouldBe a [StringSchema]
359-
nullSafeList(model.value.getRequired) shouldEqual Seq("field")
359+
nullSafeSeq(model.value.getRequired) shouldEqual Seq("field")
360360
}
361361

362362
it should "process Model with Scala Enum with annotation" in {
@@ -369,7 +369,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
369369
field shouldBe a[StringSchema]
370370
val stringSchema = field.asInstanceOf[StringSchema]
371371
stringSchema.getDescription shouldEqual "enum value"
372-
nullSafeList(model.value.getRequired) shouldEqual Seq("field")
372+
nullSafeSeq(model.value.getRequired) shouldEqual Seq("field")
373373
}
374374

375375
it should "process ListReply Model" in {
@@ -399,7 +399,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
399399
arraySchema.getUniqueItems() shouldBe (null)
400400
arraySchema.getItems shouldBe a [StringSchema]
401401
nullSafeMap(arraySchema.getProperties()) shouldBe empty
402-
nullSafeList(arraySchema.getRequired()) shouldBe empty
402+
nullSafeSeq(arraySchema.getRequired()) shouldBe empty
403403
}
404404

405405
it should "process Model with Scala Seq Int" in {
@@ -420,7 +420,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
420420
arraySchema.getItems shouldBe a[IntegerSchema]
421421
}
422422
nullSafeMap(arraySchema.getProperties()) shouldBe empty
423-
nullSafeList(arraySchema.getRequired()) shouldBe empty
423+
nullSafeSeq(arraySchema.getRequired()) shouldBe empty
424424
}
425425

426426
it should "process Model with Scala Seq Int (annotated)" in {
@@ -443,7 +443,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
443443
}
444444
arraySchema.getItems.getDescription shouldBe "These are ints"
445445
nullSafeMap(arraySchema.getProperties()) shouldBe empty
446-
nullSafeList(arraySchema.getRequired()) shouldBe empty
446+
nullSafeSeq(arraySchema.getRequired()) shouldBe empty
447447
}
448448

449449
it should "process Model with Scala Set" in {
@@ -458,7 +458,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
458458
arraySchema.getUniqueItems() shouldBe true
459459
arraySchema.getItems shouldBe a [StringSchema]
460460
nullSafeMap(arraySchema.getProperties()) shouldBe empty
461-
nullSafeList(arraySchema.getRequired()) shouldBe empty
461+
nullSafeSeq(arraySchema.getRequired()) shouldBe empty
462462
}
463463

464464
it should "process Model with Java List" in {
@@ -473,7 +473,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
473473
arraySchema.getUniqueItems() shouldBe (null)
474474
arraySchema.getItems shouldBe a [StringSchema]
475475
nullSafeMap(arraySchema.getProperties()) shouldBe empty
476-
nullSafeList(arraySchema.getRequired()) shouldBe empty
476+
nullSafeSeq(arraySchema.getRequired()) shouldBe empty
477477
}
478478

479479
it should "process Model with Scala Map" in {
@@ -487,7 +487,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
487487
val mapSchema = stringsField.asInstanceOf[MapSchema]
488488
mapSchema.getUniqueItems() shouldBe (null)
489489
nullSafeMap(mapSchema.getProperties()) shouldBe empty
490-
nullSafeList(mapSchema.getRequired()) shouldBe empty
490+
nullSafeSeq(mapSchema.getRequired()) shouldBe empty
491491
}
492492

493493
it should "process Model with Java Map" in {
@@ -501,7 +501,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
501501
val mapSchema = stringsField.asInstanceOf[MapSchema]
502502
mapSchema.getUniqueItems() shouldBe (null)
503503
nullSafeMap(mapSchema.getProperties()) shouldBe empty
504-
nullSafeList(mapSchema.getRequired()) shouldBe empty
504+
nullSafeSeq(mapSchema.getRequired()) shouldBe empty
505505
}
506506

507507
it should "process EchoList" in {
@@ -522,15 +522,15 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
522522
val schemas = converter.readAll(classOf[ModelWStringSeqAnnotated]).asScala.toMap
523523
val model = findModel(schemas, "ModelWStringSeqAnnotated")
524524
model should be(defined)
525-
nullSafeList(model.value.getRequired) shouldBe empty
525+
nullSafeSeq(model.value.getRequired) shouldBe empty
526526
}
527527

528528
it should "process Array-Model with forced required Scala Option Seq (annotated)" in {
529529
val converter = ModelConverters.getInstance()
530530
val schemas = converter.readAll(classOf[ModelWOptionStringSeqAnnotated]).asScala.toMap
531531
val model = findModel(schemas, "ModelWOptionStringSeqAnnotated")
532532
model should be(defined)
533-
nullSafeList(model.value.getRequired) shouldEqual Seq("listOfStrings")
533+
nullSafeSeq(model.value.getRequired) shouldEqual Seq("listOfStrings")
534534
}
535535

536536
it should "process scala Iterable[T] classes" in {
@@ -547,7 +547,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
547547
val schemas = converter.readAll(classOf[ModelWOptionStringSeq]).asScala.toMap
548548
val model = findModel(schemas, "ModelWOptionStringSeq")
549549
model should be(defined)
550-
nullSafeList(model.value.getRequired) shouldBe empty
550+
nullSafeSeq(model.value.getRequired) shouldBe empty
551551
}
552552

553553
it should "process case class with Duration field" in {
@@ -572,13 +572,13 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
572572
}
573573
}
574574

575-
private def nullSafeList[T](list: java.util.List[T]): List[T] = Option(list) match {
576-
case None => List[T]()
577-
case Some(l) => l.asScala.toList
575+
private def nullSafeSeq[T](list: java.util.List[T]): Seq[T] = Option(list) match {
576+
case None => List.empty[T]
577+
case Some(l) => l.asScala.toSeq
578578
}
579579

580580
private def nullSafeMap[K, V](map: java.util.Map[K, V]): Map[K, V] = Option(map) match {
581-
case None => Map[K, V]()
581+
case None => Map.empty[K, V]
582582
case Some(m) => m.asScala.toMap
583583
}
584584
}

0 commit comments

Comments
 (0)