Skip to content

Commit d93a4fc

Browse files
committed
resolves #82
1 parent 9fdaf27 commit d93a4fc

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

src/main/groovy/com/github/hauner/openapi/spring/writer/BeanValidationFactory.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ class BeanValidationFactory {
7676
|| dataType instanceof ArrayDataType
7777
|| dataType instanceof MappedMapDataType
7878
) && (
79-
dataType.constraints?.maxItems
79+
dataType.constraints?.maxItems != null
8080
|| dataType.constraints?.minItems
8181
)
8282
) || ( // String
8383
(
8484
dataType instanceof StringDataType
8585
) && (
86-
dataType.constraints?.maxLength
86+
dataType.constraints?.maxLength != null
8787
|| dataType.constraints?.minLength
8888
)
8989
)
@@ -96,7 +96,7 @@ class BeanValidationFactory {
9696
|| dataType instanceof IntegerDataType
9797
|| dataType instanceof LongDataType
9898
) && (
99-
dataType.constraints?.maximum
99+
dataType.constraints?.maximum != null
100100
)
101101
}
102102

@@ -107,7 +107,7 @@ class BeanValidationFactory {
107107
|| dataType instanceof IntegerDataType
108108
|| dataType instanceof LongDataType
109109
) && (
110-
dataType.constraints?.minimum
110+
dataType.constraints?.minimum != null
111111
)
112112
}
113113

src/test/groovy/com/github/hauner/openapi/spring/writer/BeanValidationFactorySpec.groovy

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ import spock.lang.Unroll
3434

3535
class BeanValidationFactorySpec extends Specification {
3636

37-
BeanValidationFactory beanValidationFactory = new BeanValidationFactory()
37+
BeanValidationFactory beanValidationFactory = new BeanValidationFactory ()
3838

3939
@Unroll
40-
void "check @Valid for Object (type: #type)"() {
40+
void "check @Valid for Object (type: #type)" () {
4141
setup:
42-
DataType dataType = type.getDeclaredConstructor().newInstance()
42+
DataType dataType = type.getDeclaredConstructor ().newInstance ()
4343
4444
when:
45-
def imports = beanValidationFactory.collectImports(dataType)
46-
def annotations = beanValidationFactory.createAnnotations(dataType)
45+
def imports = beanValidationFactory.collectImports (dataType)
46+
def annotations = beanValidationFactory.createAnnotations (dataType)
4747
4848
then:
4949
imports == resultImports as Set<String>
@@ -62,16 +62,16 @@ class BeanValidationFactorySpec extends Specification {
6262
}
6363

6464
@Unroll
65-
void "check import @Size for String (minLength: #minLength, maxLength: #maxLength, type: #type)"() {
65+
void "check import @Size for String (minLength: #minLength, maxLength: #maxLength, type: #type)" () {
6666
setup:
67-
DataType dataType = type.getDeclaredConstructor().newInstance()
68-
dataType.constraints = new DataTypeConstraints()
67+
DataType dataType = type.getDeclaredConstructor ().newInstance ()
68+
dataType.constraints = new DataTypeConstraints ()
6969
dataType.constraints.minLength = minLength
7070
dataType.constraints.maxLength = maxLength
7171
7272
when:
73-
def imports = beanValidationFactory.collectImports(dataType)
74-
def annotations = beanValidationFactory.createAnnotations(dataType)
73+
def imports = beanValidationFactory.collectImports (dataType)
74+
def annotations = beanValidationFactory.createAnnotations (dataType)
7575
7676
then:
7777
imports == resultImports as Set<String>
@@ -80,7 +80,9 @@ class BeanValidationFactorySpec extends Specification {
8080
where:
8181
type | minLength | maxLength || resultImports | resultAnnotations
8282
StringDataType | null | null || [] | ""
83+
StringDataType | 0 | null || [] | ""
8384
StringDataType | 1 | null || ["javax.validation.constraints.Size"] | "@Size(min = 1)"
85+
StringDataType | null | 0 || ["javax.validation.constraints.Size"] | "@Size(max = 0)"
8486
StringDataType | null | 2 || ["javax.validation.constraints.Size"] | "@Size(max = 2)"
8587
StringDataType | 1 | 2 || ["javax.validation.constraints.Size"] | "@Size(min = 1, max = 2)"
8688
IntegerDataType | 1 | null || [] | ""
@@ -90,16 +92,16 @@ class BeanValidationFactorySpec extends Specification {
9092
}
9193
9294
@Unroll
93-
void "check import @Size for Collections (minItems: #minItems, maxItems: #maxItems, type: #type)"() {
95+
void "check import @Size for Collections (minItems: #minItems, maxItems: #maxItems, type: #type)" () {
9496
setup:
95-
DataType dataType = type.getDeclaredConstructor().newInstance()
96-
dataType.constraints = new DataTypeConstraints()
97+
DataType dataType = type.getDeclaredConstructor ().newInstance ()
98+
dataType.constraints = new DataTypeConstraints ()
9799
dataType.constraints.minItems = minItems
98100
dataType.constraints.maxItems = maxItems
99101

100102
when:
101-
def imports = beanValidationFactory.collectImports(dataType)
102-
def annotations = beanValidationFactory.createAnnotations(dataType)
103+
def imports = beanValidationFactory.collectImports (dataType)
104+
def annotations = beanValidationFactory.createAnnotations (dataType)
103105

104106
then:
105107
imports == resultImports as Set<String>
@@ -108,26 +110,29 @@ class BeanValidationFactorySpec extends Specification {
108110
where:
109111
type | minItems | maxItems || resultImports | resultAnnotations
110112
ArrayDataType | null | null || [] | ""
113+
ArrayDataType | 0 | null || [] | ""
111114
ArrayDataType | 1 | null || ["javax.validation.constraints.Size"] | "@Size(min = 1)"
115+
ArrayDataType | null | 0 || ["javax.validation.constraints.Size"] | "@Size(max = 0)"
112116
ArrayDataType | null | 2 || ["javax.validation.constraints.Size"] | "@Size(max = 2)"
113117
ArrayDataType | 1 | 2 || ["javax.validation.constraints.Size"] | "@Size(min = 1, max = 2)"
114118
ListDataType | null | 2 || ["javax.validation.constraints.Size"] | "@Size(max = 2)"
115119
SetDataType | 1 | 2 || ["javax.validation.constraints.Size"] | "@Size(min = 1, max = 2)"
120+
StringDataType | 0 | null || [] | ""
116121
StringDataType | 1 | null || [] | ""
117122
StringDataType | null | 2 || [] | ""
118123
LongDataType | 1 | 2 || [] | ""
119124
}
120125

121126
@Unroll
122-
void "check import @NotNull (nullable: #nullable, type: #type)"() {
127+
void "check import @NotNull (nullable: #nullable, type: #type)" () {
123128
setup:
124-
DataType dataType = type.getDeclaredConstructor().newInstance()
125-
dataType.constraints = new DataTypeConstraints()
129+
DataType dataType = type.getDeclaredConstructor ().newInstance ()
130+
dataType.constraints = new DataTypeConstraints ()
126131
dataType.constraints.nullable = nullable
127132

128133
when:
129-
def imports = beanValidationFactory.collectImports(dataType)
130-
def annotations = beanValidationFactory.createAnnotations(dataType)
134+
def imports = beanValidationFactory.collectImports (dataType)
135+
def annotations = beanValidationFactory.createAnnotations (dataType)
131136

132137
then:
133138
imports == resultImports as Set<String>
@@ -147,16 +152,16 @@ class BeanValidationFactorySpec extends Specification {
147152
}
148153

149154
@Unroll
150-
void "check import @DecimalMin (minimum: #minimum, exclusiveMinimum: #exclusiveMinimum, type: #type)"() {
155+
void "check import @DecimalMin (minimum: #minimum, exclusiveMinimum: #exclusiveMinimum, type: #type)" () {
151156
setup:
152-
DataType dataType = type.getDeclaredConstructor().newInstance()
153-
dataType.constraints = new DataTypeConstraints()
157+
DataType dataType = type.getDeclaredConstructor ().newInstance ()
158+
dataType.constraints = new DataTypeConstraints ()
154159
dataType.constraints.minimum = minimum
155160
dataType.constraints.exclusiveMinimum = exclusiveMinimum
156161

157162
when:
158-
def imports = beanValidationFactory.collectImports(dataType)
159-
def annotations = beanValidationFactory.createAnnotations(dataType)
163+
def imports = beanValidationFactory.collectImports (dataType)
164+
def annotations = beanValidationFactory.createAnnotations (dataType)
160165

161166
then:
162167
imports == resultImports as Set<String>
@@ -170,6 +175,7 @@ class BeanValidationFactorySpec extends Specification {
170175
IntegerDataType | 1 | null || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
171176
IntegerDataType | 1 | true || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\", inclusive = false)"
172177
IntegerDataType | 1 | false || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
178+
IntegerDataType | 0 | false || ["javax.validation.constraints.DecimalMin"] | '@DecimalMin(value = "0")'
173179
LongDataType | null | null || [] | ""
174180
LongDataType | null | true || [] | ""
175181
LongDataType | null | false || [] | ""
@@ -192,16 +198,16 @@ class BeanValidationFactorySpec extends Specification {
192198
}
193199

194200
@Unroll
195-
void "check import @DecimalMax (maximum: #maximum, exclusiveMaximum: #exclusiveMaximum, type: #type)"() {
201+
void "check import @DecimalMax (maximum: #maximum, exclusiveMaximum: #exclusiveMaximum, type: #type)" () {
196202
setup:
197-
DataType dataType = type.getDeclaredConstructor().newInstance()
198-
dataType.constraints = new DataTypeConstraints()
203+
DataType dataType = type.getDeclaredConstructor ().newInstance ()
204+
dataType.constraints = new DataTypeConstraints ()
199205
dataType.constraints.maximum = maximum
200206
dataType.constraints.exclusiveMaximum = exclusiveMaximum
201207

202208
when:
203-
def imports = beanValidationFactory.collectImports(dataType)
204-
def annotations = beanValidationFactory.createAnnotations(dataType)
209+
def imports = beanValidationFactory.collectImports (dataType)
210+
def annotations = beanValidationFactory.createAnnotations (dataType)
205211

206212
then:
207213
imports == resultImports as Set<String>
@@ -215,6 +221,7 @@ class BeanValidationFactorySpec extends Specification {
215221
IntegerDataType | 1 | null || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
216222
IntegerDataType | 1 | true || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\", inclusive = false)"
217223
IntegerDataType | 1 | false || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
224+
IntegerDataType | 0 | false || ["javax.validation.constraints.DecimalMax"] | '@DecimalMax(value = "0")'
218225
LongDataType | null | null || [] | ""
219226
LongDataType | null | true || [] | ""
220227
LongDataType | null | false || [] | ""
@@ -237,18 +244,18 @@ class BeanValidationFactorySpec extends Specification {
237244
}
238245

239246
@Unroll
240-
void "check import @DecimalMin and @DecimalMax (minimum: #minimum, exclusiveMinimum: #exclusiveMinimum maximum: #maximum, exclusiveMaximum: #exclusiveMaximum)"() {
247+
void "check import @DecimalMin and @DecimalMax (minimum: #minimum, exclusiveMinimum: #exclusiveMinimum maximum: #maximum, exclusiveMaximum: #exclusiveMaximum)" () {
241248
setup:
242-
DataType dataType = new DoubleDataType()
243-
dataType.constraints = new DataTypeConstraints()
249+
DataType dataType = new DoubleDataType ()
250+
dataType.constraints = new DataTypeConstraints ()
244251
dataType.constraints.minimum = minimum
245252
dataType.constraints.exclusiveMinimum = exclusiveMinimum
246253
dataType.constraints.maximum = maximum
247254
dataType.constraints.exclusiveMaximum = exclusiveMaximum
248255

249256
when:
250-
def imports = beanValidationFactory.collectImports(dataType)
251-
def annotations = beanValidationFactory.createAnnotations(dataType)
257+
def imports = beanValidationFactory.collectImports (dataType)
258+
def annotations = beanValidationFactory.createAnnotations (dataType)
252259

253260
then:
254261
imports == resultImports as Set<String>

0 commit comments

Comments
 (0)