Skip to content

Commit 8bdb0ce

Browse files
authored
issue where schema annotations can be ignored (after recent changes) (#168)
1 parent 6785cf6 commit 8bdb0ce

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
2929
SwaggerScalaModelConverter
3030

3131
private val logger = LoggerFactory.getLogger(classOf[SwaggerScalaModelConverter])
32+
private val VoidClass = classOf[Void]
3233
private val EnumClass = classOf[scala.Enumeration]
3334
private val OptionClass = classOf[scala.Option[_]]
3435
private val IterableClass = classOf[scala.collection.Iterable[_]]
@@ -79,20 +80,24 @@ class SwaggerScalaModelConverter extends ModelResolver(SwaggerScalaModelConverte
7980
val introspector = BeanIntrospector(cls)
8081
val erasedProperties = ErasureHelper.erasedOptionalPrimitives(cls)
8182
introspector.properties.foreach { property =>
82-
8383
val propertyClass = getPropertyClass(property)
8484
val isOptional = isOption(propertyClass)
85-
86-
erasedProperties.get(property.name).foreach { erasedType =>
87-
val primitiveType = PrimitiveType.fromType(erasedType)
88-
if (primitiveType != null && isOptional) {
89-
updateTypeOnSchema(schema, primitiveType, property.name)
90-
}
91-
if (primitiveType != null && isIterable(propertyClass) && !isMap(propertyClass)) {
92-
updateTypeOnItemsSchema(schema, primitiveType, property.name)
85+
val propertyAnnotations = getPropertyAnnotations(property)
86+
val schemaOverrideClass = propertyAnnotations.collectFirst {
87+
case s: SchemaAnnotation if s.implementation() != VoidClass => s.implementation()
88+
}
89+
if (schemaOverrideClass.isEmpty) {
90+
erasedProperties.get(property.name).foreach { erasedType =>
91+
val primitiveType = PrimitiveType.fromType(erasedType)
92+
if (primitiveType != null && isOptional) {
93+
updateTypeOnSchema(schema, primitiveType, property.name)
94+
}
95+
if (primitiveType != null && isIterable(propertyClass) && !isMap(propertyClass)) {
96+
updateTypeOnItemsSchema(schema, primitiveType, property.name)
97+
}
9398
}
9499
}
95-
getPropertyAnnotations(property) match {
100+
propertyAnnotations match {
96101
case Seq() => {
97102
if (isOptional && schema.getRequired != null && schema.getRequired.contains(property.name)) {
98103
schema.getRequired.remove(property.name)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
182182
nullSafeList(model.value.getRequired) shouldBe empty
183183
}
184184

185+
it should "process Model with Scala Option Long with Schema Int Override" in {
186+
val converter = ModelConverters.getInstance()
187+
val schemas = converter.readAll(classOf[ModelWOptionLongSchemaIntOverride]).asScala.toMap
188+
val model = schemas.get("ModelWOptionLongSchemaIntOverride")
189+
model should be(defined)
190+
model.value.getProperties should not be (null)
191+
val optLong = model.value.getProperties().get("optLong")
192+
optLong should not be (null)
193+
optLong shouldBe a[IntegerSchema]
194+
optLong.asInstanceOf[IntegerSchema].getFormat shouldEqual "int32"
195+
nullSafeList(model.value.getRequired) shouldBe empty
196+
}
197+
185198
it should "process Model with Scala Option Boolean" in {
186199
val converter = ModelConverters.getInstance()
187200
val schemas = converter.readAll(classOf[ModelWOptionBoolean]).asScala.toMap

src/test/scala/models/ModelWOptionLong.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ import io.swagger.v3.oas.annotations.media.Schema
55
case class ModelWOptionLong(optLong: Option[Long])
66

77
case class ModelWOptionLongSchemaOverride(@Schema(implementation = classOf[Long]) optLong: Option[Long])
8+
9+
case class ModelWOptionLongSchemaIntOverride(@Schema(implementation = classOf[Int]) optLong: Option[Long])

0 commit comments

Comments
 (0)