Skip to content

Commit abeb4de

Browse files
authored
Allow overriding required-value in ArraySchema (#56)
Co-authored-by: Nicolai Holm <[email protected]>
1 parent 287273d commit abeb4de

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import io.swagger.v3.core.converter._
1010
import io.swagger.v3.core.jackson.ModelResolver
1111
import io.swagger.v3.core.util.{Json, PrimitiveType}
1212
import io.swagger.v3.oas.annotations.Parameter
13-
import io.swagger.v3.oas.annotations.media.{Schema => SchemaAnnotation}
13+
import io.swagger.v3.oas.annotations.media.{ArraySchema, Schema => SchemaAnnotation}
1414
import io.swagger.v3.oas.models.media.Schema
1515

1616
class AnnotatedTypeForOption extends AnnotatedType
@@ -74,6 +74,7 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
7474
nullSafeList(`type`.getCtxAnnotations).collect {
7575
case p: Parameter => p.required()
7676
case s: SchemaAnnotation => s.required()
77+
case a: ArraySchema => a.arraySchema().required()
7778
}
7879
}
7980
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,22 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
348348
}
349349
}
350350

351+
it should "process Array-Model with Scala nonOption Seq" in {
352+
val converter = ModelConverters.getInstance()
353+
val schemas = converter.readAll(classOf[ModelWStringSeq]).asScala.toMap
354+
val model = findModel(schemas, "ModelWStringSeq")
355+
model should be(defined)
356+
nullSafeList(model.value.getRequired) shouldEqual Seq()
357+
}
358+
359+
it should "process Array-Model with forced required Scala Option Seq" in {
360+
val converter = ModelConverters.getInstance()
361+
val schemas = converter.readAll(classOf[ModelWOptionStringSeq]).asScala.toMap
362+
val model = findModel(schemas, "ModelWOptionStringSeq")
363+
model should be(defined)
364+
nullSafeList(model.value.getRequired) shouldEqual Seq("listOfStrings")
365+
}
366+
351367
private def nullSafeList[T](list: java.util.List[T]): List[T] = Option(list) match {
352368
case None => List[T]()
353369
case Some(l) => l.asScala.toList
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package models
2+
3+
import io.swagger.v3.oas.annotations.media.{ArraySchema, Schema}
4+
5+
case class ModelWStringSeq(@ArraySchema(arraySchema = new Schema(required = false), schema = new Schema(implementation = classOf[String])) listOfStrings: Seq[String] = Seq.empty[String])
6+
7+
case class ModelWOptionStringSeq(@ArraySchema(arraySchema = new Schema(required = true), schema = new Schema(implementation = classOf[String])) listOfStrings: Option[Seq[String]] = Some(Seq.empty[String]))

0 commit comments

Comments
 (0)