Skip to content

Commit fb79dff

Browse files
committed
extend test
1 parent 6b6ddc2 commit fb79dff

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
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
@@ -9,6 +9,7 @@ import io.swagger.v3.core.converter._
99
import io.swagger.v3.core.jackson.ModelResolver
1010
import io.swagger.v3.core.util.{Json, PrimitiveType}
1111
import io.swagger.v3.oas.annotations.Parameter
12+
import io.swagger.v3.oas.annotations.media.{Schema => SchemaAnnotation}
1213
import io.swagger.v3.oas.models.media.{Schema, StringSchema}
1314

1415
class AnnotatedTypeForOption extends AnnotatedType
@@ -26,14 +27,7 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
2627

2728
matchScalaPrimitives(`type`, cls).getOrElse {
2829
// Unbox scala options
29-
val annotatedOverrides = `type` match {
30-
case _: AnnotatedTypeForOption => Seq.empty
31-
case _ => {
32-
nullSafeList(`type`.getCtxAnnotations).collect {
33-
case p: Parameter => p.required()
34-
}
35-
}
36-
}
30+
val annotatedOverrides = getRequiredSettings(`type`)
3731
if (_isOptional(`type`, cls)) {
3832
val baseType = if (annotatedOverrides.headOption.getOrElse(false)) new AnnotatedType() else new AnnotatedTypeForOption()
3933
resolve(nextType(baseType, `type`, cls, javaType), context, chain)
@@ -54,6 +48,16 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
5448
}
5549
}
5650

51+
private def getRequiredSettings(`type`: AnnotatedType): Seq[Boolean] = `type` match {
52+
case _: AnnotatedTypeForOption => Seq.empty
53+
case _ => {
54+
nullSafeList(`type`.getCtxAnnotations).collect {
55+
case p: Parameter => p.required()
56+
case s: SchemaAnnotation => s.required()
57+
}
58+
}
59+
}
60+
5761
private def matchScalaPrimitives(`type`: AnnotatedType, nullableClass: Class[_]): Option[Schema[_]] = {
5862
Option(nullableClass).flatMap { cls =>
5963
// handle scala enums
@@ -126,9 +130,12 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
126130
private def setRequired(annotatedType: AnnotatedType): Unit = annotatedType match {
127131
case _: AnnotatedTypeForOption => // not required
128132
case _ => {
129-
Option(annotatedType.getParent).foreach { parent =>
130-
Option(annotatedType.getPropertyName).foreach { n =>
131-
addRequiredItem(parent, n)
133+
val required = getRequiredSettings(annotatedType).headOption.getOrElse(true)
134+
if (required) {
135+
Option(annotatedType.getParent).foreach { parent =>
136+
Option(annotatedType.getPropertyName).foreach { n =>
137+
addRequiredItem(parent, n)
138+
}
132139
}
133140
}
134141
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ class ModelPropertyParserTest extends FlatSpec with Matchers {
119119
val optInt = model.get.getProperties().get("optInt")
120120
optInt should not be (null)
121121
optInt shouldBe a [IntegerSchema]
122-
//there is a bug that the override causes the field to be marked as required
123-
//nullSafeList(model.get.getRequired) shouldBe empty
122+
nullSafeList(model.get.getRequired) shouldBe empty
124123
}
125124

126125
it should "process Model with Scala Option Boolean" in {

0 commit comments

Comments
 (0)