Skip to content

Commit

Permalink
Fix under-/overflow in min/max validation for controller parameters (#…
Browse files Browse the repository at this point in the history
…324)

* test: add testscase for overflow in javax/jakarta validation

* fix: min/max annotation in controller uses long not int

* chore: move controller tests to appropriate folder
  • Loading branch information
y-marion authored Oct 9, 2024
1 parent c590d09 commit ebe07bc
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ interface ValidationAnnotations {
val nonNullAnnotation: AnnotationSpec?
fun fieldValid(): AnnotationSpec?
fun parameterValid(): AnnotationSpec?
fun min(value: Int): AnnotationSpec?
fun max(value: Int): AnnotationSpec?
fun min(value: Long): AnnotationSpec?
fun max(value: Long): AnnotationSpec?
fun regexPattern(pattern: String): AnnotationSpec?
fun lengthRestriction(min: Int?, max: Int?): AnnotationSpec?
fun minRestriction(min: Number, exclusive: Boolean): AnnotationSpec?
Expand Down Expand Up @@ -39,12 +39,12 @@ abstract class PackageValidationAnnotations(packageName: String) : ValidationAnn
.builder(validClass)
.build()

override fun min(value: Int) = AnnotationSpec
override fun min(value: Long) = AnnotationSpec
.builder(minClass)
.addMember("%L", value)
.build()

override fun max(value: Int) = AnnotationSpec
override fun max(value: Long) = AnnotationSpec
.builder(maxClass)
.addMember("%L", value)
.build()
Expand Down Expand Up @@ -91,9 +91,9 @@ object NoValidationAnnotations : ValidationAnnotations {

override fun parameterValid(): AnnotationSpec? = null

override fun min(value: Int): AnnotationSpec? = null
override fun min(value: Long): AnnotationSpec? = null

override fun max(value: Int): AnnotationSpec? = null
override fun max(value: Long): AnnotationSpec? = null

override fun regexPattern(pattern: String): AnnotationSpec? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ abstract class AnnotationBasedControllerInterfaceGenerator(
}

fun ParameterSpec.Builder.addValidationAnnotations(parameter: RequestParameter): ParameterSpec.Builder {
if (parameter.minimum != null) this.maybeAddAnnotation(validationAnnotations.min(parameter.minimum.toInt()))
if (parameter.maximum != null) this.maybeAddAnnotation(validationAnnotations.max(parameter.maximum.toInt()))
if (parameter.minimum != null) this.maybeAddAnnotation(validationAnnotations.min(parameter.minimum.toLong()))
if (parameter.maximum != null) this.maybeAddAnnotation(validationAnnotations.max(parameter.maximum.toLong()))
if (parameter.typeInfo.isComplexType) this.maybeAddAnnotation(validationAnnotations.parameterValid())
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class KtorControllerInterfaceGeneratorTest {
"pathLevelParameters",
"parameterNameClash",
"requestBodyAsArray",
"jakartaValidationAnnotations",
)

private fun setupGithubApiTestEnv() {
Expand All @@ -56,7 +57,7 @@ class KtorControllerInterfaceGeneratorTest {
}

// @Test
// fun `debug single test`() = `correct models are generated for different OpenApi Specifications`("singleAllOf")
// fun `debug single test`() = `correct controllers are generated for different OpenApi Specifications`("insert testcase here")

@ParameterizedTest
@MethodSource("testCases")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MicronautControllerGeneratorTest {
"singleAllOf",
"pathLevelParameters",
"parameterNameClash",
"jakartaValidationAnnotations",
)

private fun setupGithubApiTestEnv(validationAnnotations: ValidationAnnotations = JavaxValidationAnnotations) {
Expand All @@ -55,7 +56,7 @@ class MicronautControllerGeneratorTest {
}

// @Test
// fun `debug single test`() = `correct models are generated for different OpenApi Specifications`("singleAllOf")
// fun `debug single test`() = `correct models are generated for different OpenApi Specifications`("insert testcase here")

@ParameterizedTest
@MethodSource("testCases")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class SpringControllerGeneratorTest {
"singleAllOf",
"pathLevelParameters",
"parameterNameClash",
"jakartaValidationAnnotations",
)

private fun setupGithubApiTestEnv(annotations: ValidationAnnotations = JavaxValidationAnnotations) {
Expand Down
79 changes: 79 additions & 0 deletions src/test/resources/examples/jakartaValidationAnnotations/api.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,83 @@
openapi: 3.0.0
paths:
/maximumTest/{pathId}:
get:
parameters:
- in: path
schema:
type: integer
format: int64
minimum: 0
maximum: 4294967295
name: pathId
- in: header
name: headerid
schema:
type: integer
format: int64
minimum: 0
maximum: 4294967295
- in: query
name: queryid
schema:
type: integer
format: int64
minimum: 0
maximum: 4294967295
responses:
204:
description: "result"
/minimumTest/{pathId}:
get:
parameters:
- in: path
schema:
type: integer
format: int64
minimum: -4294967295
name: pathId
- in: header
name: headerid
schema:
type: integer
format: int64
minimum: -4294967295
- in: query
name: queryid
schema:
type: integer
format: int64
minimum: -4294967295
responses:
204:
description: "result"
/minMaxTest/{pathId}:
get:
parameters:
- in: path
schema:
type: integer
format: int64
minimum: -4294967295
maximum: 4294967296
name: pathId
- in: header
name: headerid
schema:
type: integer
format: int64
minimum: -4294967295
maximum: 4294967296
- in: query
name: queryid
schema:
type: integer
format: int64
minimum: -4294967295
maximum: 4294967296
responses:
204:
description: "result"
components:
schemas:
ValidationAnnotations:
Expand Down
Loading

0 comments on commit ebe07bc

Please sign in to comment.