Skip to content

Commit 2e7c1bf

Browse files
author
PJ Fanning
committed
support JsonScalaEnumeration
1 parent 1c4ec68 commit 2e7c1bf

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.github.swagger.scala.converter
22

3+
import java.lang.reflect.ParameterizedType
34
import java.util.Iterator
45

56
import com.fasterxml.jackson.databind.JavaType
67
import com.fasterxml.jackson.databind.`type`.ReferenceType
7-
import com.fasterxml.jackson.module.scala.DefaultScalaModule
8+
import com.fasterxml.jackson.module.scala.{DefaultScalaModule, JsonScalaEnumeration}
89
import io.swagger.v3.core.converter._
910
import io.swagger.v3.core.jackson.ModelResolver
1011
import io.swagger.v3.core.util.{Json, PrimitiveType}
1112
import io.swagger.v3.oas.annotations.Parameter
1213
import io.swagger.v3.oas.annotations.media.{Schema => SchemaAnnotation}
13-
import io.swagger.v3.oas.models.media.{Schema, StringSchema}
14+
import io.swagger.v3.oas.models.media.Schema
1415

1516
class AnnotatedTypeForOption extends AnnotatedType
1617

@@ -59,21 +60,23 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
5960
}
6061

6162
private def matchScalaPrimitives(`type`: AnnotatedType, nullableClass: Class[_]): Option[Schema[_]] = {
62-
Option(nullableClass).flatMap { cls =>
63-
// handle scala enums
64-
getEnumerationInstance(cls) match {
65-
case Some(enumInstance) => {
66-
if (enumInstance.values != null) {
67-
val sp = new StringSchema()
68-
for (v <- enumInstance.values)
69-
sp.addEnumItem(v.toString)
70-
setRequired(`type`)
71-
Some(sp)
72-
} else {
73-
None
63+
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)
7474
}
75+
sp
7576
}
76-
case _ => {
77+
}
78+
case _ => {
79+
Option(nullableClass).flatMap { cls =>
7780
if (cls == classOf[BigDecimal]) {
7881
val dp = PrimitiveType.DECIMAL.createProperty()
7982
setRequired(`type`)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ class ScalaModelTest extends FlatSpec with Matchers {
2323
// (sp.getEnum().asScala.toSet & Set("TALL", "GRANDE", "VENTI")) should have size 3
2424
}
2525

26+
it should "extract a scala enum (jackson annotated)" in {
27+
val schemas = ModelConverters.getInstance().readAll(classOf[SModelWithEnumJacksonAnnotated]).asScala
28+
val userSchema = schemas("SModelWithEnumJacksonAnnotated")
29+
30+
val orderSize = userSchema.getProperties().get("orderSize")
31+
orderSize should not be null
32+
orderSize shouldBe a [StringSchema]
33+
val sp = orderSize.asInstanceOf[StringSchema]
34+
Option(sp.getEnum) shouldBe defined
35+
sp.getEnum().asScala.toSet shouldEqual Set("TALL", "GRANDE", "VENTI")
36+
}
37+
2638
it should "read a scala case class with properties" in {
2739
val schemas = ModelConverters.getInstance().readAll(classOf[SimpleUser]).asScala
2840
val userSchema = schemas("SimpleUser")

src/test/scala/models/EnumModels.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package models
22

3+
import com.fasterxml.jackson.core.`type`.TypeReference
4+
import com.fasterxml.jackson.module.scala.JsonScalaEnumeration
35
import io.swagger.v3.oas.annotations.media.Schema
46
import models.OrderSize.OrderSize
57

@@ -9,7 +11,11 @@ import scala.annotation.meta.field
911
case class SModelWithEnum(
1012
@(Schema @field)(name = "Order Size", implementation = classOf[OrderSize]) orderSize: OrderSize = OrderSize.TALL)
1113

12-
case object OrderSize extends Enumeration(0) {
14+
class OrderSizeTypeClass extends TypeReference[OrderSize.type]
15+
case class SModelWithEnumJacksonAnnotated(
16+
@JsonScalaEnumeration(classOf[OrderSizeTypeClass]) orderSize: OrderSize = OrderSize.TALL)
17+
18+
case object OrderSize extends Enumeration {
1319
type OrderSize = Value
1420
val TALL = Value("TALL")
1521
val GRANDE = Value("GRANDE")

0 commit comments

Comments
 (0)