From 1c417b2e586d99bdf8f87b7747b6e9266d126a15 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 2 Feb 2024 15:20:24 +0100 Subject: [PATCH 1/4] check ArraySchema annotation --- .../converter/SwaggerScalaModelConverter.scala | 18 +++++++++++++++++- .../converter/ModelPropertyParserTest.scala | 6 ++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala b/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala index e07dabfe..8d8736e0 100644 --- a/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala +++ b/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.{JavaType, ObjectMapper} import com.fasterxml.jackson.module.scala.introspect.{BeanIntrospector, PropertyDescriptor} import com.fasterxml.jackson.module.scala.util.ClassW import com.fasterxml.jackson.module.scala.{DefaultScalaModule, JsonScalaEnumeration} +import com.github.swagger.scala.converter.SwaggerScalaModelConverter.nullSafeSeq import io.swagger.v3.core.converter._ import io.swagger.v3.core.jackson.ModelResolver import io.swagger.v3.core.util.{Json, PrimitiveType} @@ -233,6 +234,20 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte val classOption: Option[Class[_]] = if (s.implementation() == VoidClass) None else Option(s.implementation()) classOption } + val arraySchemaOverrideClass = if (schemaOverride.isEmpty) { + None + } else { + val arraySchemaOverride = propertyAnnotations.collectFirst { case as: ArraySchemaAnnotation => as } + arraySchemaOverride.flatMap { as => + val itemSchema = if (as.items() == null || as.items().implementation() == VoidClass) as.schema() else as.items() + val classOption = if (itemSchema == null || itemSchema.implementation() == VoidClass) { + None + } else { + Option(itemSchema.implementation()) + } + classOption + } + } val maybeDefault = property.param.flatMap(_.defaultValue) val schemaDefaultValue = schemaOverride.flatMap { s => Option(s.defaultValue()).flatMap(str => if (str.isEmpty) None else Some(str)) @@ -260,7 +275,8 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte } } - if (schemaProperties.nonEmpty && schemaOverrideClass.isEmpty) { + val overrideClass = schemaOverrideClass.orElse(arraySchemaOverrideClass) + if (schemaProperties.nonEmpty && overrideClass.isEmpty) { erasedProperties.get(propertyName).foreach { erasedType => schemaProperties.get(propertyName).foreach { property => Option(PrimitiveType.fromType(erasedType)).foreach { primitiveType => diff --git a/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala b/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala index a8bb0a64..53502dc5 100644 --- a/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala +++ b/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala @@ -182,8 +182,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with BeforeAndAfterEach with M val arraySchema = numbers.asInstanceOf[ArraySchema] arraySchema.getMinItems shouldEqual 2 arraySchema.getMaxItems shouldEqual 10 - // TODO - this should be an IntegerSchema but the @ArraySchema annotation items schema is not being picked up - // arraySchema.getItems shouldBe an[IntegerSchema] + arraySchema.getItems shouldBe an[IntegerSchema] } it should "process AddRequestOldStyleAnnotation" in new PropertiesScope[AddRequestOldStyleAnnotation] { @@ -193,8 +192,7 @@ class ModelPropertyParserTest extends AnyFlatSpec with BeforeAndAfterEach with M val arraySchema = numbers.asInstanceOf[ArraySchema] arraySchema.getMinItems shouldEqual 2 arraySchema.getMaxItems shouldEqual 10 - // TODO - this should be an IntegerSchema but the @ArraySchema annotation items schema is not being picked up - // arraySchema.getItems shouldBe an[IntegerSchema] + arraySchema.getItems shouldBe an[IntegerSchema] } it should "process Model without any properties" in new TestScope { From 235b2b659688fa48332164bcd9deddc5cc17b880 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 2 Feb 2024 15:30:47 +0100 Subject: [PATCH 2/4] scala 2.12 compile issue --- .../swagger/scala/converter/SwaggerScalaModelConverter.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala b/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala index 8d8736e0..eaebf5d5 100644 --- a/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala +++ b/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala @@ -240,7 +240,7 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte val arraySchemaOverride = propertyAnnotations.collectFirst { case as: ArraySchemaAnnotation => as } arraySchemaOverride.flatMap { as => val itemSchema = if (as.items() == null || as.items().implementation() == VoidClass) as.schema() else as.items() - val classOption = if (itemSchema == null || itemSchema.implementation() == VoidClass) { + val classOption: Option[Class[_]] = if (itemSchema == null || itemSchema.implementation() == VoidClass) { None } else { Option(itemSchema.implementation()) From f17cb15c742e3b34dd2175945ec513ab77f88951 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 2 Feb 2024 15:35:08 +0100 Subject: [PATCH 3/4] Update SwaggerScalaModelConverter.scala --- .../swagger/scala/converter/SwaggerScalaModelConverter.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala b/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala index eaebf5d5..ab1e20d0 100644 --- a/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala +++ b/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala @@ -234,7 +234,7 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte val classOption: Option[Class[_]] = if (s.implementation() == VoidClass) None else Option(s.implementation()) classOption } - val arraySchemaOverrideClass = if (schemaOverride.isEmpty) { + val arraySchemaOverrideClass = if (schemaOverride.nonEmpty) { None } else { val arraySchemaOverride = propertyAnnotations.collectFirst { case as: ArraySchemaAnnotation => as } From 9ae8830866f55a2e6abe2a776988616c878c04dc Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 2 Feb 2024 18:16:43 +0100 Subject: [PATCH 4/4] Update ModelPropertyParserTest.scala --- .../swagger/scala/converter/ModelPropertyParserTest.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala b/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala index 53502dc5..4a8113cf 100644 --- a/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala +++ b/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala @@ -182,7 +182,8 @@ class ModelPropertyParserTest extends AnyFlatSpec with BeforeAndAfterEach with M val arraySchema = numbers.asInstanceOf[ArraySchema] arraySchema.getMinItems shouldEqual 2 arraySchema.getMaxItems shouldEqual 10 - arraySchema.getItems shouldBe an[IntegerSchema] + // assertion disabled due to https://github.com/swagger-api/swagger-core/issues/4610 + // arraySchema.getItems shouldBe an[IntegerSchema] } it should "process AddRequestOldStyleAnnotation" in new PropertiesScope[AddRequestOldStyleAnnotation] {