Skip to content

Commit fbd8624

Browse files
committed
#23 - Fix OpenAPI (swagger) gen for @NotNull (javax and Kotlin) @SiZe and @Email
1 parent 752bf87 commit fbd8624

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/main/java/io/dinject/javalin/generator/openapi/KnownTypes.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ interface KnownType {
4040

4141
KnownTypes() {
4242
add(new StringType(), String.class, char[].class, CharSequence.class);
43-
add(new BoolType(), boolean.class, Boolean.class);
44-
add(new IntegerType(), int.class, Integer.class);
45-
add(new LongType(), long.class, Long.class);
46-
add(new NumberType(), double.class, Double.class, float.class, Float.class, BigDecimal.class, BigInteger.class);
43+
add(new BoolType(), boolean.class);
44+
add(new BooleanType(), Boolean.class);
45+
add(new IntType(), int.class);
46+
add(new IntegerType(), Integer.class);
47+
add(new PLongType(), long.class);
48+
add(new LongType(), Long.class);
49+
50+
add(new PNumberType(), double.class, float.class);
51+
add(new NumberType(), Double.class, Float.class, BigDecimal.class, BigInteger.class);
4752
add(new DateType(), LocalDate.class, java.sql.Date.class);
4853
add(new DateTimeType(), Instant.class, OffsetDateTime.class, ZonedDateTime.class, Timestamp.class, java.util.Date.class, LocalDateTime.class);
4954

@@ -82,26 +87,54 @@ public Schema<?> createSchema() {
8287
}
8388

8489
private class BoolType implements KnownType {
90+
@Override
91+
public Schema<?> createSchema() {
92+
return new BooleanSchema().nullable(Boolean.FALSE);
93+
}
94+
}
95+
96+
private class BooleanType implements KnownType {
8597
@Override
8698
public Schema<?> createSchema() {
8799
return new BooleanSchema();
88100
}
89101
}
90102

103+
private class IntType implements KnownType {
104+
@Override
105+
public Schema<?> createSchema() {
106+
return new IntegerSchema().nullable(Boolean.FALSE);
107+
}
108+
}
109+
91110
private class IntegerType implements KnownType {
92111
@Override
93112
public Schema<?> createSchema() {
94113
return new IntegerSchema();
95114
}
96115
}
97116

117+
private class PLongType implements KnownType {
118+
@Override
119+
public Schema<?> createSchema() {
120+
return new IntegerSchema().format("int64").nullable(Boolean.FALSE);
121+
}
122+
}
123+
98124
private class LongType implements KnownType {
99125
@Override
100126
public Schema<?> createSchema() {
101127
return new IntegerSchema().format("int64");
102128
}
103129
}
104130

131+
private class PNumberType implements KnownType {
132+
@Override
133+
public Schema<?> createSchema() {
134+
return new NumberSchema().nullable(Boolean.FALSE);
135+
}
136+
}
137+
105138
private class NumberType implements KnownType {
106139
@Override
107140
public Schema<?> createSchema() {

src/main/java/io/dinject/javalin/generator/openapi/SchemaDocBuilder.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import javax.lang.model.util.ElementFilter;
2323
import javax.lang.model.util.Elements;
2424
import javax.lang.model.util.Types;
25+
import javax.validation.constraints.Email;
26+
import javax.validation.constraints.Size;
2527
import java.util.ArrayList;
2628
import java.util.List;
2729
import java.util.Map;
@@ -217,12 +219,43 @@ private ObjectSchema createObjectSchema(TypeMirror objectType) {
217219
Element element = types.asElement(objectType);
218220
for (VariableElement field : allFields(element)) {
219221
Schema<?> propSchema = toSchema(field.asType());
220-
// max,min,required,deprecated ?
222+
if (isNotNullable(field)) {
223+
propSchema.setNullable(Boolean.FALSE);
224+
}
225+
setLengthMinMax(field, propSchema);
226+
setFormatFromValidation(field, propSchema);
221227
objectSchema.addProperties(field.getSimpleName().toString(), propSchema);
222228
}
223229
return objectSchema;
224230
}
225231

232+
private void setFormatFromValidation(Element element, Schema<?> propSchema) {
233+
if (element.getAnnotation(Email.class) != null) {
234+
propSchema.setFormat("email");
235+
}
236+
}
237+
238+
private void setLengthMinMax(Element element, Schema<?> propSchema) {
239+
final Size size = element.getAnnotation(Size.class);
240+
if (size != null) {
241+
if (size.min() > 0) {
242+
propSchema.setMinLength(size.min());
243+
}
244+
if (size.max() > 0) {
245+
propSchema.setMaxLength(size.max());
246+
}
247+
}
248+
}
249+
250+
private boolean isNotNullable(Element element) {
251+
if (element.getAnnotation(org.jetbrains.annotations.NotNull.class) != null) {
252+
return true;
253+
}
254+
if (element.getAnnotation(javax.validation.constraints.NotNull.class) != null) {
255+
return true;
256+
}
257+
return false;
258+
}
226259

227260
/**
228261
* Gather all the fields (properties) for the given bean element.

0 commit comments

Comments
 (0)