Skip to content

Commit 126ae31

Browse files
pjfanningarcan1s
andauthored
do not override annotations for bigdecimals and bigints … (#40)
* do not override annotations for bigdecimals and bigints In case if annotation for bigdecimal and bigint already set this module will still override annotation to default `number` tyoe * add test cases for annotated bigint, bigdecimal and enum * change impl Co-authored-by: Evgeniy Alekseev <[email protected]>
1 parent 7a22f95 commit 126ae31

File tree

5 files changed

+78
-25
lines changed

5 files changed

+78
-25
lines changed

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,37 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
6161

6262
private def matchScalaPrimitives(`type`: AnnotatedType, nullableClass: Class[_]): Option[Schema[_]] = {
6363
val annotations = Option(`type`.getCtxAnnotations).map(_.toSeq).getOrElse(Seq.empty)
64-
annotations.collectFirst { case ann: JsonScalaEnumeration => ann } match {
65-
case Some(enumAnnotation) => {
66-
val pt = enumAnnotation.value().getGenericSuperclass.asInstanceOf[ParameterizedType]
67-
val args = pt.getActualTypeArguments
68-
val cls = args(0).asInstanceOf[Class[Enumeration]]
69-
getEnumerationInstance(cls).map { enum =>
70-
val sp: Schema[String] = PrimitiveType.STRING.createProperty().asInstanceOf[Schema[String]]
71-
setRequired(`type`)
72-
enum.values.iterator.foreach { v =>
73-
sp.addEnumItemObject(v.toString)
74-
}
75-
sp
76-
}
77-
}
64+
annotations.collectFirst { case ann: SchemaAnnotation => ann } match {
65+
case Some(_) => None
7866
case _ => {
79-
Option(nullableClass).flatMap { cls =>
80-
if (cls == classOf[BigDecimal]) {
81-
val dp = PrimitiveType.DECIMAL.createProperty()
82-
setRequired(`type`)
83-
Some(dp)
84-
} else if (cls == classOf[BigInt]) {
85-
val ip = PrimitiveType.INT.createProperty()
86-
setRequired(`type`)
87-
Some(ip)
88-
} else {
89-
None
67+
annotations.collectFirst { case ann: JsonScalaEnumeration => ann } match {
68+
case Some(enumAnnotation: JsonScalaEnumeration) => {
69+
val pt = enumAnnotation.value().getGenericSuperclass.asInstanceOf[ParameterizedType]
70+
val args = pt.getActualTypeArguments
71+
val cls = args(0).asInstanceOf[Class[Enumeration]]
72+
getEnumerationInstance(cls).map { enum =>
73+
val sp: Schema[String] = PrimitiveType.STRING.createProperty().asInstanceOf[Schema[String]]
74+
setRequired(`type`)
75+
enum.values.iterator.foreach { v =>
76+
sp.addEnumItemObject(v.toString)
77+
}
78+
sp
79+
}
80+
}
81+
case _ => {
82+
Option(nullableClass).flatMap { cls =>
83+
if (cls == classOf[BigDecimal]) {
84+
val dp = PrimitiveType.DECIMAL.createProperty()
85+
setRequired(`type`)
86+
Some(dp)
87+
} else if (cls == classOf[BigInt]) {
88+
val ip = PrimitiveType.INT.createProperty()
89+
setRequired(`type`)
90+
Some(ip)
91+
} else {
92+
None
93+
}
94+
}
9095
}
9196
}
9297
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,39 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers {
212212
converter.readAll(classOf[Option[Int]])
213213
}
214214

215+
it should "process Model with Scala BigDecimal with annotation" in {
216+
val converter = ModelConverters.getInstance()
217+
val schemas = converter.readAll(classOf[ModelWBigDecimalAnnotated]).asScala.toMap
218+
val model = findModel(schemas, "ModelWBigDecimalAnnotated")
219+
model should be (defined)
220+
model.get.getProperties should not be (null)
221+
val field = model.get.getProperties.get("field")
222+
field shouldBe a [StringSchema]
223+
nullSafeList(model.get.getRequired) should not be empty
224+
}
225+
226+
it should "process Model with Scala BigInt with annotation" in {
227+
val converter = ModelConverters.getInstance()
228+
val schemas = converter.readAll(classOf[ModelWBigIntAnnotated]).asScala.toMap
229+
val model = findModel(schemas, "ModelWBigIntAnnotated")
230+
model should be (defined)
231+
model.get.getProperties should not be (null)
232+
val field = model.get.getProperties.get("field")
233+
field shouldBe a [StringSchema]
234+
nullSafeList(model.get.getRequired) should not be empty
235+
}
236+
237+
it should "process Model with Scala Enum with annotation" in {
238+
val converter = ModelConverters.getInstance()
239+
val schemas = converter.readAll(classOf[ModelWEnumAnnotated]).asScala.toMap
240+
val model = findModel(schemas, "ModelWEnumAnnotated")
241+
model should be (defined)
242+
model.get.getProperties should not be (null)
243+
val field = model.get.getProperties.get("field")
244+
field shouldBe a [StringSchema]
245+
nullSafeList(model.get.getRequired) should not be empty
246+
}
247+
215248
def findModel(schemas: Map[String, Schema[_]], name: String): Option[Schema[_]] = {
216249
schemas.get(name) match {
217250
case Some(m) => Some(m)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package models
2+
3+
import io.swagger.v3.oas.annotations.media.Schema
4+
5+
case class ModelWBigDecimalAnnotated(@Schema(description = "bigdecimal value", `type` = "string", example = "42.0", required = true) field: BigDecimal)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package models
2+
3+
import io.swagger.v3.oas.annotations.media.Schema
4+
5+
case class ModelWBigIntAnnotated(@Schema(description = "bigint value", `type` = "string", example = "42", required = true) field: BigInt)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package models
2+
3+
import io.swagger.v3.oas.annotations.media.Schema
4+
5+
case class ModelWEnumAnnotated(@Schema(description = "enum value", `type` = "string", required = true) field: OrderSize.Value)

0 commit comments

Comments
 (0)