Skip to content

Commit 4e7ea51

Browse files
committed
1 parent 211e824 commit 4e7ea51

2 files changed

Lines changed: 50 additions & 13 deletions

File tree

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/BeanValidationFactory.kt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,22 @@ open class BeanValidationFactory(
3030
}
3131

3232
fun validate(dataType: DataType, required: Boolean = false, parentHasValid: Boolean = false): BeanValidationInfo {
33-
return if (dataType is CollectionDataType) {
34-
val (annotations, valid) = collectAnnotationsWithValid(dataType, required, parentHasValid)
35-
36-
BeanValidationInfoCollection(
37-
dataType,
38-
annotations,
39-
validate(dataType.item, parentHasValid = valid)
40-
)
41-
} else {
42-
BeanValidationInfoSimple(
43-
dataType,
44-
collectAnnotations(dataType, required, parentHasValid)
45-
)
33+
return when (dataType) {
34+
is MappedCollectionDataTypePrimitive -> {
35+
// should not be handled as collection, we can't annotate the primitive type only the primitve array
36+
BeanValidationInfoSimple(dataType, collectAnnotations(dataType, required, parentHasValid))
37+
}
38+
is CollectionDataType -> {
39+
val (annotations, valid) = collectAnnotationsWithValid(dataType, required, parentHasValid)
40+
41+
BeanValidationInfoCollection(
42+
dataType,
43+
annotations,
44+
validate(dataType.item, parentHasValid = valid))
45+
}
46+
else -> {
47+
BeanValidationInfoSimple(dataType, collectAnnotations(dataType, required, parentHasValid))
48+
}
4649
}
4750
}
4851

openapi-processor-core/src/test/kotlin/io/openapiprocessor/core/writer/java/BeanValidationFactorySpec.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.kotest.core.spec.style.StringSpec
1010
import io.kotest.matchers.collections.shouldBeEmpty
1111
import io.kotest.matchers.shouldBe
1212
import io.openapiprocessor.core.converter.ApiOptions
13+
import io.openapiprocessor.core.converter.mapping.TypeMapping
1314
import io.openapiprocessor.core.model.datatypes.*
1415
import io.openapiprocessor.core.support.datatypes.ListDataType
1516
import io.openapiprocessor.core.support.datatypes.ObjectDataType
@@ -236,4 +237,37 @@ class BeanValidationFactorySpec : StringSpec({
236237
io.imports shouldBe setOf(validations.NOT_NULL, validations.VALID)
237238
io.annotations.shouldBeEmpty()
238239
}
240+
241+
"does apply validation annotations to mapped primitive collection" {
242+
val sourceDataType = StringDataType(
243+
constraints = DataTypeConstraints(format = "byte", minLength = 1, maxLength = 10), typeFormat = "string:byte")
244+
245+
val typeMapping = TypeMapping (
246+
"byte",
247+
null,
248+
"byte",
249+
emptyList(),
250+
primitive = true,
251+
primitiveArray = true)
252+
253+
val targetType = typeMapping.getTargetType()
254+
255+
val mappedDataType = MappedCollectionDataTypePrimitive(
256+
targetType.getName(),
257+
null,
258+
false,
259+
sourceDataType)
260+
261+
val info = validation.validate(mappedDataType, false)
262+
263+
val prop = info.prop
264+
prop.dataTypeValue shouldBe "byte[]"
265+
prop.imports shouldBe setOf(validations.SIZE)
266+
prop.annotations shouldBe setOf("@Size(min = 1, max = 10)")
267+
268+
val io = info.inout
269+
io.dataTypeValue shouldBe "@Size(min = 1, max = 10) byte[]"
270+
io.imports shouldBe setOf(validations.SIZE)
271+
io.annotations.shouldBeEmpty()
272+
}
239273
})

0 commit comments

Comments
 (0)