Skip to content

Commit ee5aa7a

Browse files
authored
feat: validate operation params (#232)
1 parent 92e758e commit ee5aa7a

File tree

3 files changed

+38
-45
lines changed

3 files changed

+38
-45
lines changed

customizations/generator/openapi/src/main/kotlin/com/expediagroup/sdk/generators/openapi/OpenApiSdkGenerator.kt

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ class OpenApiSdkGenerator {
3939
"pom.xml",
4040
"README.md",
4141
"ApiException.kt",
42-
"LinkableOperation.kt",
43-
"PropertyConstraintViolation.kt",
44-
"PropertyConstraintsValidator.kt",
45-
"PropertyConstraintViolationException.kt"
42+
"LinkableOperation.kt"
4643
)
4744

4845
companion object {
@@ -145,27 +142,6 @@ class OpenApiSdkGenerator {
145142
"ApiException.kt"
146143
)
147144
)
148-
add(
149-
SupportingFile(
150-
"validation/propertyConstraintViolationException.mustache",
151-
"$packagePath/models/exception/",
152-
"PropertyConstraintViolationException.kt"
153-
)
154-
)
155-
add(
156-
SupportingFile(
157-
"validation/propertyConstraintViolation.mustache",
158-
"$packagePath/models/exception/",
159-
"PropertyConstraintViolation.kt"
160-
)
161-
)
162-
add(
163-
SupportingFile(
164-
"validation/propertyConstraintsValidator.mustache",
165-
"$packagePath/validation/",
166-
"PropertyConstraintsValidator.kt"
167-
)
168-
)
169145

170146
add(
171147
TemplateDefinition(

customizations/generator/openapi/src/main/resources/templates/expediagroup-sdk/client.mustache

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class {{clientClassname}}Client private constructor(clientConfiguration: RapidCl
5656
}
5757

5858
appendHeaders(extraHeaders)
59-
validateConstraints(operation.requestBody)
6059
contentType(ContentType.Application.Json)
6160
setBody(operation.requestBody)
6261
}
@@ -233,4 +232,4 @@ class {{clientClassname}}Client private constructor(clientConfiguration: RapidCl
233232
{{>client/paginatorMethods}}
234233
{{/isPaginatable}}
235234
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
236-
}
235+
}

customizations/generator/openapi/src/main/resources/templates/expediagroup-sdk/operation_params.mustache

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
{{#hasNonBodyParams}}
44
package com.expediagroup.sdk.{{namespace}}.operations
55

6+
import com.expediagroup.sdk.core.model.exception.client.PropertyConstraintViolationException
67
import com.expediagroup.sdk.core.model.OperationParams
8+
79
import com.fasterxml.jackson.annotation.JsonProperty
810
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
911
import io.ktor.http.Headers
1012
import io.ktor.http.Parameters
1113

14+
import javax.validation.constraints.Max
15+
import javax.validation.constraints.Min
16+
import javax.validation.constraints.NotNull
17+
import javax.validation.constraints.Pattern
18+
import javax.validation.constraints.Size
19+
import javax.validation.Valid
20+
import javax.validation.Validation
21+
22+
import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator
23+
1224
/**
1325
{{#nonBodyParams}}
1426
{{#params}}
@@ -22,6 +34,7 @@
2234
(
2335
{{#nonBodyParams}}
2436
{{#params}}
37+
{{>models/constraints}}
2538
{{>modelMutable}} {{>client/apiParam}}{{^-last}}, {{/-last}}
2639
{{/params}}
2740
{{/nonBodyParams}}
@@ -31,6 +44,7 @@
3144
internal constructor(
3245
{{#nonBodyParams}}
3346
{{#params}}
47+
{{>models/constraints}}
3448
{{>modelMutable}} {{{paramName}}}: {{>partials/datatype}}? = null,
3549
{{/params}}
3650
{{/nonBodyParams}}
@@ -109,32 +123,36 @@
109123
{{/nonBodyParams}}
110124

111125
fun build(): {{classname}}Params {
112-
{{#hasRequiredParams}}
113-
validateNullity()
114-
{{/hasRequiredParams}}
115-
116-
return {{classname}}Params(
126+
val params = {{classname}}Params(
117127
{{#nonBodyParams}}
118128
{{#params}}
119129
{{{paramName}}} = {{{paramName}}}{{#required}}!!{{/required}}{{^-last}},{{/-last}}
120130
{{/params}}
121131
{{/nonBodyParams}}
122132
)
133+
134+
validate(params)
135+
136+
return params
123137
}
124138

125-
{{#hasRequiredParams}}
126-
private fun validateNullity() {
127-
{{#nonBodyParams}}
128-
{{#params}}
129-
{{#required}}
130-
if ({{{paramName}}} == null) {
131-
throw NullPointerException("Required parameter {{{paramName}}} is missing")
132-
}
133-
{{/required}}
134-
{{/params}}
135-
{{/nonBodyParams}}
139+
private fun validate(params: {{classname}}Params) {
140+
val validator =
141+
Validation
142+
.byDefaultProvider()
143+
.configure()
144+
.messageInterpolator(ParameterMessageInterpolator())
145+
.buildValidatorFactory()
146+
.validator
147+
148+
val violations = validator.validate(params)
149+
150+
if (violations.isNotEmpty()) {
151+
throw PropertyConstraintViolationException(
152+
constraintViolations = violations.map { "${it.propertyPath}: ${it.message}" }
153+
)
136154
}
137-
{{/hasRequiredParams}}
155+
}
138156
}
139157

140158
fun toBuilder() = Builder(
@@ -187,4 +205,4 @@
187205
}
188206
{{/hasNonBodyParams}}
189207
{{/operation}}
190-
{{/operations}}
208+
{{/operations}}

0 commit comments

Comments
 (0)