Skip to content

Commit c2ee200

Browse files
committed
reduce use of java null
1 parent 489f9de commit c2ee200

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory
1616

1717
import java.util
1818
import scala.collection.Seq
19+
import scala.collection.JavaConverters._
1920
import scala.util.Try
2021
import scala.util.control.NonFatal
2122

@@ -76,6 +77,7 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
7677
chain: util.Iterator[ModelConverter]): Option[Schema[_]] = {
7778
if (chain.hasNext) {
7879
Option(chain.next().resolve(`type`, context, chain)).map { schema =>
80+
val schemaProperties = nullSafeMap(schema.getProperties)
7981
val introspector = BeanIntrospector(cls)
8082
val erasedProperties = ErasureHelper.erasedOptionalPrimitives(cls)
8183
introspector.properties.foreach { property =>
@@ -85,16 +87,16 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
8587
case s: SchemaAnnotation if s.implementation() != VoidClass => s.implementation()
8688
}
8789
val propertyName = property.name
88-
if (schema.getProperties != null && schemaOverrideClass.isEmpty) {
90+
if (schemaProperties.nonEmpty && schemaOverrideClass.isEmpty) {
8991
erasedProperties.get(propertyName).foreach { erasedType =>
90-
val primitiveType = PrimitiveType.fromType(erasedType)
91-
val property = schema.getProperties.get(propertyName)
92-
if (primitiveType != null && property != null) {
93-
if (isOptional) {
94-
schema.addProperty(propertyName, correctSchema(property, primitiveType))
95-
}
96-
if (isIterable(propertyClass) && !isMap(propertyClass)) {
97-
schema.addProperty(propertyName, updateTypeOnItemsSchema(primitiveType, property))
92+
schemaProperties.get(propertyName).foreach { property =>
93+
Option(PrimitiveType.fromType(erasedType)).foreach { primitiveType =>
94+
if (isOptional) {
95+
schema.addProperty(propertyName, correctSchema(property, primitiveType))
96+
}
97+
if (isIterable(propertyClass) && !isMap(propertyClass)) {
98+
schema.addProperty(propertyName, updateTypeOnItemsSchema(primitiveType, property))
99+
}
98100
}
99101
}
100102
}
@@ -140,7 +142,7 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
140142

141143
private def getRequiredSettings(annotatedType: AnnotatedType): Seq[Boolean] = annotatedType match {
142144
case _: AnnotatedTypeForOption => Seq.empty
143-
case _ => getRequiredSettings(nullSafeList(annotatedType.getCtxAnnotations))
145+
case _ => getRequiredSettings(nullSafeSeq(annotatedType.getCtxAnnotations))
144146
}
145147

146148
private def getRequiredSettings(annotations: Seq[Annotation]): Seq[Boolean] = {
@@ -293,8 +295,13 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
293295
private def isMap(cls: Class[_]): Boolean = MapClass.isAssignableFrom(cls)
294296
private def isCaseClass(cls: Class[_]): Boolean = ProductClass.isAssignableFrom(cls)
295297

296-
private def nullSafeList[T](array: Array[T]): List[T] = Option(array) match {
298+
private def nullSafeSeq[T](array: Array[T]): Seq[T] = Option(array) match {
297299
case None => List.empty[T]
298300
case Some(arr) => arr.toList
299301
}
302+
303+
private def nullSafeMap[K, V](map: java.util.Map[K, V]): Map[K, V] = Option(map) match {
304+
case None => Map[K, V]()
305+
case Some(m) => m.asScala.toMap
306+
}
300307
}

0 commit comments

Comments
 (0)