Skip to content

Commit 9794d4b

Browse files
committed
RTypeCache
1 parent 2064f4c commit 9794d4b

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

src/main/scala-2/com/github/swagger/scala/converter/SubtypeHelper.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ object SubtypeHelper {
1616
if (classSymbol.isJava) {
1717
Seq.empty
1818
} else if (symbol.isClass) {
19-
val clazzes = symbol.asClass.knownDirectSubclasses
19+
symbol.asClass.knownDirectSubclasses
2020
.toSeq
2121
.sortBy(_.info.toString)
2222
.flatMap(s => if (s.isClass) Some(s.asClass) else None)
23-
.map(c => mirror.runtimeClass(c).getName)
24-
clazzes.map(cn => Class.forName(cn, true, Thread.currentThread().getContextClassLoader))
23+
.map(c => mirror.runtimeClass(c))
2524
} else {
2625
Seq.empty
2726
}

src/main/scala-3/com/github/swagger/scala/converter/ErasureHelper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private[converter] object ErasureHelper {
1313

1414
def erasedOptionalPrimitives(cls: Class[_]): Map[String, Class[_]] = {
1515
try {
16-
val rType = RType.of(cls)
16+
val rType = RTypeCache.getRType(cls)
1717
rType match {
1818
case classInfo: ClassInfo => {
1919
val results = classInfo.fields.flatMap { fieldInfo =>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.swagger.scala.converter
2+
3+
import co.blocke.scala_reflection.RType
4+
5+
import scala.collection.concurrent.TrieMap
6+
7+
object RTypeCache {
8+
private val trieMap = TrieMap[Class[_], RType]()
9+
10+
def getRType(cls: Class[_]): RType = trieMap.getOrElseUpdate(cls, RType.of(cls))
11+
12+
def remove(cls: Class[_]): Option[RType] = trieMap.remove(cls)
13+
14+
def clear(): Unit = trieMap.clear()
15+
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ object SwaggerScalaModelConverter {
2929
private var requiredBasedOnAnnotation = true
3030

3131
/** If you use swagger annotations to override what is automatically derived, then be aware that
32-
* [[io.swagger.v3.oas.annotations.media.Schema]] annotation has required = [[false]], by default. You are advised to set the required
32+
* [[io.swagger.v3.oas.annotations.media.Schema]] annotation has required = false, by default. You are advised to set the required
3333
* flag on this annotation to the correct value. If you would prefer to have the Schema annotation required flag ignored and to rely on
3434
* the this module inferring the value (as ot would if you don't annotate the classes or fields), then set
35-
* [[SwaggerScalaModelConverter.setRequiredBasedOnAnnotation]] to [[true]] and the required property on the annotation will be ignored,
35+
* [[SwaggerScalaModelConverter.setRequiredBasedOnAnnotation]] to true and the required property on the annotation will be ignored,
3636
* unless the field is an [[Option]].
3737
*
3838
* @param value
@@ -43,10 +43,10 @@ object SwaggerScalaModelConverter {
4343
}
4444

4545
/** If you use swagger annotations to override what is automatically derived, then be aware that
46-
* [[io.swagger.v3.oas.annotations.media.Schema]] annotation has required = [[false]], by default. You are advised to set the required
46+
* [[io.swagger.v3.oas.annotations.media.Schema]] annotation has required = false, by default. You are advised to set the required
4747
* flag on this annotation to the correct value. If you would prefer to have the Schema annotation required flag ignored and to rely on
4848
* the this module inferring the value (as ot would if you don't annotate the classes or fields), then set
49-
* [[SwaggerScalaModelConverter.setRequiredBasedOnAnnotation]] to [[true]] and the required property on the annotation will be ignored,
49+
* [[SwaggerScalaModelConverter.setRequiredBasedOnAnnotation]] to true and the required property on the annotation will be ignored,
5050
* unless the field is an [[Option]].
5151
*
5252
* @return
@@ -215,7 +215,7 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
215215
private def filterUnwantedProperties(schema: Schema[_], propertiesToKeep: Seq[PropertyDescriptor]): Unit = {
216216
val propNamesSet = propertiesToKeep.map(getAnnotatedPropertyName).toSet
217217
val originalProps = nullSafeMap(schema.getProperties)
218-
val newProps = originalProps.filter { case (key, value) =>
218+
val newProps = originalProps.filter { case (key, _) =>
219219
propNamesSet.contains(key)
220220
}
221221
if (originalProps.size > newProps.size) {
@@ -401,7 +401,6 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
401401
property.param match {
402402
case Some(constructorParameter) =>
403403
val types = constructorParameter.constructor.getParameterTypes
404-
val annotations = constructorParameter.constructor.getParameterAnnotations
405404
val index = constructorParameter.index
406405
if (index > types.size) {
407406
AnyClass

0 commit comments

Comments
 (0)