This repository was archived by the owner on Mar 16, 2025. It is now read-only.
File tree Expand file tree Collapse file tree
main/kotlin/io/openapiprocessor/core
testInt/resources/tests/bean-validation
test/kotlin/io/openapiprocessor/core/writer/java Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -239,7 +239,8 @@ class DataTypeConverter(
239239 schemaInfo.getExclusiveMinimum(),
240240 schemaInfo.getMaximum(),
241241 schemaInfo.getExclusiveMaximum(),
242- pattern = schemaInfo.pattern
242+ pattern = schemaInfo.pattern,
243+ format = schemaInfo.getFormat()
243244 )
244245
245246 return when (typeFormat) {
Original file line number Diff line number Diff line change @@ -21,8 +21,8 @@ class DataTypeConstraints(
2121 var /* val*/ minItems : Int = 0 ,
2222 var /* val*/ maxItems : Int? = null ,
2323 var pattern : String? = null ,
24- var /* val*/ required : List <String > = emptyList()
25-
24+ var /* val*/ required : List <String > = emptyList(),
25+ val format : String? = null
2626) {
2727
2828 fun getDefault (): Any? = defaultValue
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ package io.openapiprocessor.core.writer.java
88enum class BeanValidation (val typeName : String ) {
99 DECIMAL_MAX (" javax.validation.constraints.DecimalMax" ),
1010 DECIMAL_MIN (" javax.validation.constraints.DecimalMin" ),
11+ EMAIL (" javax.validation.constraints.Email" ),
1112 NOT_NULL (" javax.validation.constraints.NotNull" ),
1213 PATTERN (" javax.validation.constraints.Pattern" ),
1314 SIZE (" javax.validation.constraints.Size" ),
Original file line number Diff line number Diff line change @@ -64,6 +64,10 @@ open class BeanValidationFactory {
6464 imports.add(BeanValidation .PATTERN .import)
6565 }
6666
67+ if (dataType.emailConstraint()) {
68+ imports.add(BeanValidation .EMAIL .import)
69+ }
70+
6771 return imports
6872 }
6973
@@ -95,6 +99,10 @@ open class BeanValidationFactory {
9599 annotations.add(createPatternAnnotation(dataType))
96100 }
97101
102+ if (dataType.emailConstraint()) {
103+ annotations.add(BeanValidation .EMAIL .annotation)
104+ }
105+
98106 return annotations
99107 }
100108
@@ -195,3 +203,5 @@ private fun DataType.lengthConstraints(): SizeConstraints = constraints?.lengthC
195203private fun DataType.itemConstraints (): SizeConstraints = constraints?.itemConstraints!!
196204
197205private 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({
220220 io.annotations shouldBe emptySet()
221221 }
222222
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+ }
223239})
Original file line number Diff line number Diff line change 1111import javax .validation .Valid ;
1212import javax .validation .constraints .DecimalMax ;
1313import javax .validation .constraints .DecimalMin ;
14+ import javax .validation .constraints .Email ;
1415import javax .validation .constraints .NotNull ;
1516import javax .validation .constraints .Pattern ;
1617import javax .validation .constraints .Size ;
@@ -51,4 +52,7 @@ void getEndpointItems(
5152 @ Mapping ("/endpoint/pattern" )
5253 void getEndpointPattern (@ Parameter @ Pattern (regexp = ".*\\ .\\ \\ " ) String anything );
5354
55+ @ Mapping ("/endpoint/email" )
56+ void getEndpointEmail (@ Parameter @ Email String anything );
57+
5458}
Original file line number Diff line number Diff line change @@ -158,6 +158,21 @@ paths:
158158 ' 204 ' :
159159 description : empty
160160
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+
161176components :
162177 schemas :
163178 Obj1 :
You can’t perform that action at this time.
0 commit comments