|
1 | 1 | package com.github.swagger.scala.converter |
2 | 2 |
|
| 3 | +import java.lang.reflect.ParameterizedType |
3 | 4 | import java.util.Iterator |
4 | 5 |
|
5 | 6 | import com.fasterxml.jackson.databind.JavaType |
6 | 7 | import com.fasterxml.jackson.databind.`type`.ReferenceType |
7 | | -import com.fasterxml.jackson.module.scala.DefaultScalaModule |
| 8 | +import com.fasterxml.jackson.module.scala.{DefaultScalaModule, JsonScalaEnumeration} |
8 | 9 | import io.swagger.v3.core.converter._ |
9 | 10 | import io.swagger.v3.core.jackson.ModelResolver |
10 | 11 | import io.swagger.v3.core.util.{Json, PrimitiveType} |
11 | 12 | import io.swagger.v3.oas.annotations.Parameter |
12 | 13 | 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 |
14 | 15 |
|
15 | 16 | class AnnotatedTypeForOption extends AnnotatedType |
16 | 17 |
|
@@ -59,21 +60,23 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) { |
59 | 60 | } |
60 | 61 |
|
61 | 62 | 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) |
74 | 74 | } |
| 75 | + sp |
75 | 76 | } |
76 | | - case _ => { |
| 77 | + } |
| 78 | + case _ => { |
| 79 | + Option(nullableClass).flatMap { cls => |
77 | 80 | if (cls == classOf[BigDecimal]) { |
78 | 81 | val dp = PrimitiveType.DECIMAL.createProperty() |
79 | 82 | setRequired(`type`) |
|
0 commit comments