Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
SwaggerScalaModelConverter

private val logger = LoggerFactory.getLogger(classOf[SwaggerScalaModelConverter])
private val VoidClass = classOf[Void]
private val EnumClass = classOf[scala.Enumeration]
private val OptionClass = classOf[scala.Option[_]]
private val IterableClass = classOf[scala.collection.Iterable[_]]
Expand Down Expand Up @@ -79,20 +80,24 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
val introspector = BeanIntrospector(cls)
val erasedProperties = ErasureHelper.erasedOptionalPrimitives(cls)
introspector.properties.foreach { property =>

val propertyClass = getPropertyClass(property)
val isOptional = isOption(propertyClass)

erasedProperties.get(property.name).foreach { erasedType =>
val primitiveType = PrimitiveType.fromType(erasedType)
if (primitiveType != null && isOptional) {
updateTypeOnSchema(schema, primitiveType, property.name)
}
if (primitiveType != null && isIterable(propertyClass) && !isMap(propertyClass)) {
updateTypeOnItemsSchema(schema, primitiveType, property.name)
val propertyAnnotations = getPropertyAnnotations(property)
val schemaOverrideClass = propertyAnnotations.collectFirst {
case s: SchemaAnnotation if s.implementation() != VoidClass => s.implementation()
}
if (schemaOverrideClass.isEmpty) {
erasedProperties.get(property.name).foreach { erasedType =>
val primitiveType = PrimitiveType.fromType(erasedType)
if (primitiveType != null && isOptional) {
updateTypeOnSchema(schema, primitiveType, property.name)
}
if (primitiveType != null && isIterable(propertyClass) && !isMap(propertyClass)) {
updateTypeOnItemsSchema(schema, primitiveType, property.name)
}
}
}
getPropertyAnnotations(property) match {
propertyAnnotations match {
case Seq() => {
if (isOptional && schema.getRequired != null && schema.getRequired.contains(property.name)) {
schema.getRequired.remove(property.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
nullSafeList(model.value.getRequired) shouldBe empty
}

it should "process Model with Scala Option Long with Schema Int Override" in {
val converter = ModelConverters.getInstance()
val schemas = converter.readAll(classOf[ModelWOptionLongSchemaIntOverride]).asScala.toMap
val model = schemas.get("ModelWOptionLongSchemaIntOverride")
model should be(defined)
model.value.getProperties should not be (null)
val optLong = model.value.getProperties().get("optLong")
optLong should not be (null)
optLong shouldBe a[IntegerSchema]
optLong.asInstanceOf[IntegerSchema].getFormat shouldEqual "int32"
nullSafeList(model.value.getRequired) shouldBe empty
}

it should "process Model with Scala Option Boolean" in {
val converter = ModelConverters.getInstance()
val schemas = converter.readAll(classOf[ModelWOptionBoolean]).asScala.toMap
Expand Down
2 changes: 2 additions & 0 deletions src/test/scala/models/ModelWOptionLong.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ import io.swagger.v3.oas.annotations.media.Schema
case class ModelWOptionLong(optLong: Option[Long])

case class ModelWOptionLongSchemaOverride(@Schema(implementation = classOf[Long]) optLong: Option[Long])

case class ModelWOptionLongSchemaIntOverride(@Schema(implementation = classOf[Int]) optLong: Option[Long])