Skip to content

Commit 271d1d0

Browse files
authored
Merge pull request #177 from kevin70/master
OpenAPI documents support enumeration types
2 parents fa4c432 + 4865902 commit 271d1d0

File tree

5 files changed

+81
-18
lines changed

5 files changed

+81
-18
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/openapi/OpenAPISerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static String serialize(Object obj) throws IllegalAccessException {
8181
sb.append(",");
8282
}
8383
sb.append("\"");
84-
sb.append(field.getName());
84+
sb.append(field.getName().replace("_enum", "enum"));
8585
sb.append("\" : ");
8686
write(sb, value);
8787
firstField = false;

http-generator-core/src/main/java/io/avaje/http/generator/core/openapi/SchemaDocBuilder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static io.avaje.http.generator.core.Util.typeDef;
44

55
import io.avaje.http.generator.core.javadoc.Javadoc;
6+
import io.swagger.v3.oas.models.media.StringSchema;
67
import java.util.ArrayList;
78
import java.util.List;
89
import java.util.Map;
@@ -11,6 +12,7 @@
1112

1213
import javax.lang.model.element.AnnotationMirror;
1314
import javax.lang.model.element.Element;
15+
import javax.lang.model.element.ElementKind;
1416
import javax.lang.model.element.Modifier;
1517
import javax.lang.model.element.TypeElement;
1618
import javax.lang.model.element.VariableElement;
@@ -149,9 +151,27 @@ Schema<?> toSchema(TypeMirror type) {
149151
if (types.isAssignable(type, iterableType)) {
150152
return buildIterableSchema(type);
151153
}
154+
Element e = types.asElement(type);
155+
if (e != null && e.getKind() == ElementKind.ENUM) {
156+
return buildEnumSchema(e);
157+
}
152158
return buildObjectSchema(type);
153159
}
154160

161+
private Schema<?> buildEnumSchema(Element e) {
162+
var schema = new StringSchema();
163+
e.getEnclosedElements().stream()
164+
.filter(ec -> ec.getKind().equals(ElementKind.ENUM_CONSTANT))
165+
.forEach(ec -> schema.addEnumItem(ec.getSimpleName().toString()));
166+
167+
var doc = Javadoc.parse(elements.getDocComment(e));
168+
var desc = doc.getDescription();
169+
if (desc != null && !desc.isEmpty()) {
170+
schema.setDescription(desc);
171+
}
172+
return schema;
173+
}
174+
155175
private Schema<?> buildObjectSchema(TypeMirror type) {
156176
String objectSchemaKey = getObjectSchemaName(type);
157177

tests/test-javalin-jsonb/src/main/resources/public/openapi.json

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"openapi" : "3.0.1",
33
"info" : {
4-
"title" : "Example service showing off the Path extension method of controller",
4+
"title" : "Example service",
55
"description" : "Example Javalin controllers with Java and Maven",
66
"version" : ""
77
},
88
"tags" : [
99
{
1010
"name" : "tag1",
11-
"description" : "this is added to openapi tags"
11+
"description" : "it's somethin"
1212
},
1313
{
1414
"name" : "tag1",
15-
"description" : "it's somethin"
15+
"description" : "this is added to openapi tags"
1616
}
1717
],
1818
"paths" : {
@@ -1156,7 +1156,12 @@
11561156
"type" : "string"
11571157
},
11581158
"type" : {
1159-
"$ref" : "#/components/schemas/ServerType"
1159+
"type" : "string",
1160+
"enum" : [
1161+
"PROXY",
1162+
"HIDE_N_SEEK",
1163+
"FFA"
1164+
]
11601165
}
11611166
}
11621167
}
@@ -1195,7 +1200,12 @@
11951200
"type" : "string"
11961201
},
11971202
"type" : {
1198-
"$ref" : "#/components/schemas/ServerType"
1203+
"type" : "string",
1204+
"enum" : [
1205+
"PROXY",
1206+
"HIDE_N_SEEK",
1207+
"FFA"
1208+
]
11991209
}
12001210
}
12011211
}
@@ -1229,7 +1239,12 @@
12291239
"name" : "type",
12301240
"in" : "query",
12311241
"schema" : {
1232-
"$ref" : "#/components/schemas/ServerType"
1242+
"type" : "string",
1243+
"enum" : [
1244+
"PROXY",
1245+
"HIDE_N_SEEK",
1246+
"FFA"
1247+
]
12331248
}
12341249
}
12351250
],
@@ -1261,7 +1276,12 @@
12611276
"schema" : {
12621277
"type" : "array",
12631278
"items" : {
1264-
"$ref" : "#/components/schemas/ServerType"
1279+
"type" : "string",
1280+
"enum" : [
1281+
"PROXY",
1282+
"HIDE_N_SEEK",
1283+
"FFA"
1284+
]
12651285
}
12661286
}
12671287
}
@@ -1299,7 +1319,12 @@
12991319
"name" : "type",
13001320
"in" : "query",
13011321
"schema" : {
1302-
"$ref" : "#/components/schemas/ServerType"
1322+
"type" : "string",
1323+
"enum" : [
1324+
"PROXY",
1325+
"HIDE_N_SEEK",
1326+
"FFA"
1327+
]
13031328
}
13041329
}
13051330
],
@@ -1903,9 +1928,6 @@
19031928
}
19041929
}
19051930
},
1906-
"ServerType" : {
1907-
"type" : "object"
1908-
},
19091931
"byte" : {
19101932
"type" : "object"
19111933
}

tests/test-jex/src/main/java/org/example/web/HelloDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public class HelloDto {
88
public int id;
99
@NotNull
1010
public String name;
11+
public ServerType serverType;
1112
}

tests/test-jex/src/main/resources/public/openapi.json

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@
190190
"name" : "type",
191191
"in" : "query",
192192
"schema" : {
193-
"$ref" : "#/components/schemas/ServerType"
193+
"type" : "string",
194+
"enum" : [
195+
"PROXY",
196+
"HIDE_N_SEEK",
197+
"FFA"
198+
]
194199
}
195200
}
196201
],
@@ -222,7 +227,12 @@
222227
"schema" : {
223228
"type" : "array",
224229
"items" : {
225-
"$ref" : "#/components/schemas/ServerType"
230+
"type" : "string",
231+
"enum" : [
232+
"PROXY",
233+
"HIDE_N_SEEK",
234+
"FFA"
235+
]
226236
}
227237
}
228238
}
@@ -260,7 +270,12 @@
260270
"name" : "type",
261271
"in" : "query",
262272
"schema" : {
263-
"$ref" : "#/components/schemas/ServerType"
273+
"type" : "string",
274+
"enum" : [
275+
"PROXY",
276+
"HIDE_N_SEEK",
277+
"FFA"
278+
]
264279
}
265280
}
266281
],
@@ -363,11 +378,16 @@
363378
"name" : {
364379
"type" : "string",
365380
"nullable" : false
381+
},
382+
"serverType" : {
383+
"type" : "string",
384+
"enum" : [
385+
"PROXY",
386+
"HIDE_N_SEEK",
387+
"FFA"
388+
]
366389
}
367390
}
368-
},
369-
"ServerType" : {
370-
"type" : "object"
371391
}
372392
}
373393
}

0 commit comments

Comments
 (0)