Skip to content

Commit ecc0065

Browse files
committed
🎨 A "NullPointerException" could be thrown;
fix #54 sonar violation
1 parent dee0666 commit ecc0065

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

src/main/java/io/asfjava/ui/core/schema/decorators/PasswordSchemaDecorator.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.asfjava.ui.core.schema.decorators;
22

3+
import java.util.Optional;
4+
35
import com.fasterxml.jackson.databind.BeanProperty;
46
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
57
import com.fasterxml.jackson.module.jsonSchema.types.StringSchema;
@@ -10,19 +12,20 @@ public class PasswordSchemaDecorator implements SchemaDecorator {
1012

1113
@Override
1214
public void customizeSchema(BeanProperty property, JsonSchema jsonschema) {
13-
Password annotation = property.getAnnotation(Password.class);
14-
if (annotation != null && annotation.title() != null) {
15-
((StringSchema) jsonschema).setTitle(annotation.title());
16-
}
17-
if (annotation.pattern() != null) {
18-
((StringSchema) jsonschema).setPattern(annotation.pattern());
19-
}
20-
if (annotation.minLenght() != 0) {
21-
((StringSchema) jsonschema).setMinLength(annotation.minLenght());
22-
}
23-
if (annotation.maxLenght() != Integer.MAX_VALUE) {
24-
((StringSchema) jsonschema).setMaxLength(annotation.maxLenght());
25-
}
15+
Optional.ofNullable(property.getAnnotation(Password.class)).ifPresent(annotation -> {
16+
if (annotation.title() != null) {
17+
((StringSchema) jsonschema).setTitle(annotation.title());
18+
}
19+
if (annotation.pattern() != null) {
20+
((StringSchema) jsonschema).setPattern(annotation.pattern());
21+
}
22+
if (annotation.minLenght() != 0) {
23+
((StringSchema) jsonschema).setMinLength(annotation.minLenght());
24+
}
25+
if (annotation.maxLenght() != Integer.MAX_VALUE) {
26+
((StringSchema) jsonschema).setMaxLength(annotation.maxLenght());
27+
}
28+
});
2629
}
2730

2831
@Override

src/main/java/io/asfjava/ui/core/schema/decorators/TextAreaSchemaDecorator.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.asfjava.ui.core.schema.decorators;
22

3+
import java.util.Optional;
4+
35
import com.fasterxml.jackson.databind.BeanProperty;
46
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
57
import com.fasterxml.jackson.module.jsonSchema.types.StringSchema;
@@ -10,17 +12,17 @@ public class TextAreaSchemaDecorator implements SchemaDecorator {
1012

1113
@Override
1214
public void customizeSchema(BeanProperty property, JsonSchema jsonschema) {
13-
TextArea annotation = property.getAnnotation(TextArea.class);
14-
if (annotation != null && annotation.title() != null) {
15-
((StringSchema) jsonschema).setTitle(annotation.title());
16-
}
17-
18-
if (annotation.minLenght() != 0) {
19-
((StringSchema) jsonschema).setMinLength(annotation.minLenght());
20-
}
21-
if (annotation.maxLenght() != Integer.MAX_VALUE) {
22-
((StringSchema) jsonschema).setMaxLength(annotation.maxLenght());
23-
}
15+
Optional.ofNullable(property.getAnnotation(TextArea.class)).ifPresent(annotation -> {
16+
if (annotation.title() != null) {
17+
((StringSchema) jsonschema).setTitle(annotation.title());
18+
}
19+
if (annotation.minLenght() != 0) {
20+
((StringSchema) jsonschema).setMinLength(annotation.minLenght());
21+
}
22+
if (annotation.maxLenght() != Integer.MAX_VALUE) {
23+
((StringSchema) jsonschema).setMaxLength(annotation.maxLenght());
24+
}
25+
});
2426
}
2527

2628
@Override

0 commit comments

Comments
 (0)