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