Skip to content

Commit 489f9de

Browse files
authored
Simplify update logic (#172)
* Simplify update logic and avoid NPE for corner cases in which `schema.getProperties.get(propertyName)` returns null * Remove unnecessary method branch:
1 parent c70c439 commit 489f9de

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.swagger.v3.oas.models.media.Schema
1515
import org.slf4j.LoggerFactory
1616

1717
import java.util
18+
import scala.collection.Seq
1819
import scala.util.Try
1920
import scala.util.control.NonFatal
2021

@@ -83,14 +84,18 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
8384
val schemaOverrideClass = propertyAnnotations.collectFirst {
8485
case s: SchemaAnnotation if s.implementation() != VoidClass => s.implementation()
8586
}
86-
if (schemaOverrideClass.isEmpty && schema.getProperties != null) {
87-
erasedProperties.get(property.name).foreach { erasedType =>
87+
val propertyName = property.name
88+
if (schema.getProperties != null && schemaOverrideClass.isEmpty) {
89+
erasedProperties.get(propertyName).foreach { erasedType =>
8890
val primitiveType = PrimitiveType.fromType(erasedType)
89-
if (primitiveType != null && isOptional) {
90-
updateTypeOnSchema(schema, primitiveType, property.name)
91-
}
92-
if (primitiveType != null && isIterable(propertyClass) && !isMap(propertyClass)) {
93-
updateTypeOnItemsSchema(schema, primitiveType, property.name)
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))
98+
}
9499
}
95100
}
96101
}
@@ -116,17 +121,10 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
116121
}
117122
}
118123

119-
private def updateTypeOnSchema(schema: Schema[_], primitiveType: PrimitiveType, propertyName: String) = {
120-
val property = schema.getProperties.get(propertyName)
121-
val updatedSchema = correctSchema(property, primitiveType)
122-
schema.addProperty(propertyName, updatedSchema)
123-
}
124-
125-
private def updateTypeOnItemsSchema(schema: Schema[_], primitiveType: PrimitiveType, propertyName: String) = {
126-
val property = schema.getProperties.get(propertyName)
127-
val updatedSchema = correctSchema(property.getItems, primitiveType)
128-
property.setItems(updatedSchema)
129-
schema.addProperty(propertyName, property)
124+
private def updateTypeOnItemsSchema(primitiveType: PrimitiveType, propertySchema: Schema[_]) = {
125+
val updatedSchema = correctSchema(propertySchema.getItems, primitiveType)
126+
propertySchema.setItems(updatedSchema)
127+
propertySchema
130128
}
131129

132130
private def correctSchema(itemSchema: Schema[_], primitiveType: PrimitiveType) = {

0 commit comments

Comments
 (0)