This repository was archived by the owner on Mar 16, 2025. It is now read-only.
File tree 7 files changed +50
-3
lines changed
main/kotlin/io/openapiprocessor/core
test/kotlin/io/openapiprocessor/core/writer/java
testInt/resources/tests/bean-validation
7 files changed +50
-3
lines changed Original file line number Diff line number Diff line change @@ -239,7 +239,8 @@ class DataTypeConverter(
239
239
schemaInfo.getExclusiveMinimum(),
240
240
schemaInfo.getMaximum(),
241
241
schemaInfo.getExclusiveMaximum(),
242
- pattern = schemaInfo.pattern
242
+ pattern = schemaInfo.pattern,
243
+ format = schemaInfo.getFormat()
243
244
)
244
245
245
246
return when (typeFormat) {
Original file line number Diff line number Diff line change @@ -21,8 +21,8 @@ class DataTypeConstraints(
21
21
var /* val*/ minItems : Int = 0 ,
22
22
var /* val*/ maxItems : Int? = null ,
23
23
var pattern : String? = null ,
24
- var /* val*/ required : List <String > = emptyList()
25
-
24
+ var /* val*/ required : List <String > = emptyList(),
25
+ val format : String? = null
26
26
) {
27
27
28
28
fun getDefault (): Any? = defaultValue
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ package io.openapiprocessor.core.writer.java
8
8
enum class BeanValidation (val typeName : String ) {
9
9
DECIMAL_MAX (" javax.validation.constraints.DecimalMax" ),
10
10
DECIMAL_MIN (" javax.validation.constraints.DecimalMin" ),
11
+ EMAIL (" javax.validation.constraints.Email" ),
11
12
NOT_NULL (" javax.validation.constraints.NotNull" ),
12
13
PATTERN (" javax.validation.constraints.Pattern" ),
13
14
SIZE (" javax.validation.constraints.Size" ),
Original file line number Diff line number Diff line change @@ -64,6 +64,10 @@ open class BeanValidationFactory {
64
64
imports.add(BeanValidation .PATTERN .import)
65
65
}
66
66
67
+ if (dataType.emailConstraint()) {
68
+ imports.add(BeanValidation .EMAIL .import)
69
+ }
70
+
67
71
return imports
68
72
}
69
73
@@ -95,6 +99,10 @@ open class BeanValidationFactory {
95
99
annotations.add(createPatternAnnotation(dataType))
96
100
}
97
101
102
+ if (dataType.emailConstraint()) {
103
+ annotations.add(BeanValidation .EMAIL .annotation)
104
+ }
105
+
98
106
return annotations
99
107
}
100
108
@@ -195,3 +203,5 @@ private fun DataType.lengthConstraints(): SizeConstraints = constraints?.lengthC
195
203
private fun DataType.itemConstraints (): SizeConstraints = constraints?.itemConstraints!!
196
204
197
205
private fun DataType.patternConstraint (): Boolean = constraints?.pattern != null
206
+
207
+ private fun DataType.emailConstraint (): Boolean = " email" == constraints?.format
Original file line number Diff line number Diff line change @@ -220,4 +220,20 @@ class BeanValidationFactorySpec: StringSpec({
220
220
io.annotations shouldBe emptySet()
221
221
}
222
222
223
+ " applies @Email to String" {
224
+ val validation = BeanValidationFactory ()
225
+
226
+ val dataType = StringDataType (constraints = DataTypeConstraints (format = "email"))
227
+ val info = validation.validate(dataType)
228
+
229
+ val prop = info.prop
230
+ prop.dataTypeValue shouldBe " String"
231
+ info.imports shouldBe setOf(BeanValidation .EMAIL .import)
232
+ info.annotations shouldBe setOf(BeanValidation .EMAIL .annotation)
233
+
234
+ val io = info.inout
235
+ io.dataTypeValue shouldBe """ ${BeanValidation .EMAIL .annotation} String"""
236
+ io.imports shouldBe setOf(BeanValidation .EMAIL .import)
237
+ io.annotations shouldBe emptySet()
238
+ }
223
239
})
Original file line number Diff line number Diff line change 11
11
import javax .validation .Valid ;
12
12
import javax .validation .constraints .DecimalMax ;
13
13
import javax .validation .constraints .DecimalMin ;
14
+ import javax .validation .constraints .Email ;
14
15
import javax .validation .constraints .NotNull ;
15
16
import javax .validation .constraints .Pattern ;
16
17
import javax .validation .constraints .Size ;
@@ -51,4 +52,7 @@ void getEndpointItems(
51
52
@ Mapping ("/endpoint/pattern" )
52
53
void getEndpointPattern (@ Parameter @ Pattern (regexp = ".*\\ .\\ \\ " ) String anything );
53
54
55
+ @ Mapping ("/endpoint/email" )
56
+ void getEndpointEmail (@ Parameter @ Email String anything );
57
+
54
58
}
Original file line number Diff line number Diff line change @@ -158,6 +158,21 @@ paths:
158
158
' 204 ' :
159
159
description : empty
160
160
161
+ /endpoint/email :
162
+ get :
163
+ tags :
164
+ - endpoint
165
+ parameters :
166
+ - in : query
167
+ name : anything
168
+ schema :
169
+ type : string
170
+ format : email
171
+ description : email format
172
+ responses :
173
+ ' 204 ' :
174
+ description : empty
175
+
161
176
components :
162
177
schemas :
163
178
Obj1 :
You can’t perform that action at this time.
0 commit comments