Skip to content

Commit

Permalink
try to fix issue with Option[Iterable] type (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Feb 1, 2024
1 parent 74e1a49 commit ff3a38d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import io.swagger.v3.core.jackson.ModelResolver
import io.swagger.v3.core.util.{Json, PrimitiveType}
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode
import io.swagger.v3.oas.annotations.media.{ArraySchema, Schema => SchemaAnnotation}
import io.swagger.v3.oas.models.media.{MapSchema, ObjectSchema, Schema}
import io.swagger.v3.oas.annotations.media.{ArraySchema => ArraySchemaAnnotation, Schema => SchemaAnnotation}
import io.swagger.v3.oas.models.media.{ArraySchema, MapSchema, ObjectSchema, Schema}
import org.slf4j.LoggerFactory

import java.lang.annotation.Annotation
import java.lang.reflect.ParameterizedType
import java.util
import java.util.List
import scala.collection.JavaConverters._
import scala.util.Try
import scala.util.control.NonFatal
Expand Down Expand Up @@ -109,7 +108,7 @@ object SwaggerScalaModelConverter {
s.requiredMode()
}
}
case a: ArraySchema => {
case a: ArraySchemaAnnotation => {
if (a.arraySchema().requiredMode() == RequiredMode.AUTO) {
if (a.arraySchema().required()) {
RequiredMode.REQUIRED
Expand Down Expand Up @@ -350,6 +349,11 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
private[converter] def tryCorrectSchema(itemSchema: Schema[_], primitiveType: PrimitiveType): Schema[_] = {
itemSchema match {
case ms: MapSchema => ms
case as: ArraySchema => {
val correctedSchema = tryCorrectSchema(as.getItems, primitiveType)
as.setItems(correctedSchema)
as
}
case _ => {
Try {
val primitiveProperty = primitiveType.createProperty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,19 @@ class ModelPropertyParserTest extends AnyFlatSpec with BeforeAndAfterEach with M
it should "process Option[Set[String]] as string" in new PropertiesScope[OptionSetString] {
val values = model.value.getProperties().get("values")
values should not be (null)
// TODO fix broken assertion
//values shouldBe an[ArraySchema]
values shouldBe an[ArraySchema]
values.getRequired shouldBe null
values.asInstanceOf[ArraySchema].getItems shouldBe a[StringSchema]
}

it should "process Option[Seq[Long]] as string" in new PropertiesScope[OptionSeqLong] {
val values = model.value.getProperties().get("values")
values should not be (null)
// TODO fix broken assertion
// values shouldBe an[ArraySchema]
values shouldBe an[ArraySchema]
values.getRequired shouldBe null
val itemSchema = values.asInstanceOf[ArraySchema].getItems
itemSchema shouldBe an[IntegerSchema]
itemSchema.asInstanceOf[IntegerSchema].getFormat shouldBe "int64"
}

it should "process Option[Model] as Model" in new PropertiesScope[ModelWOptionModel] {
Expand Down

0 comments on commit ff3a38d

Please sign in to comment.