Skip to content

OpenAPI documents support enumeration types #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static String serialize(Object obj) throws IllegalAccessException {
sb.append(",");
}
sb.append("\"");
sb.append(field.getName());
sb.append(field.getName().replace("_enum", "enum"));
sb.append("\" : ");
write(sb, value);
firstField = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.avaje.http.generator.core.Util.typeDef;

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

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
Expand Down Expand Up @@ -149,9 +151,27 @@ Schema<?> toSchema(TypeMirror type) {
if (types.isAssignable(type, iterableType)) {
return buildIterableSchema(type);
}
Element e = types.asElement(type);
if (e != null && e.getKind() == ElementKind.ENUM) {
return buildEnumSchema(e);
}
return buildObjectSchema(type);
}

private Schema<?> buildEnumSchema(Element e) {
var schema = new StringSchema();
e.getEnclosedElements().stream()
.filter(ec -> ec.getKind().equals(ElementKind.ENUM_CONSTANT))
.forEach(ec -> schema.addEnumItem(ec.getSimpleName().toString()));

var doc = Javadoc.parse(elements.getDocComment(e));
var desc = doc.getDescription();
if (desc != null && !desc.isEmpty()) {
schema.setDescription(desc);
}
return schema;
}

private Schema<?> buildObjectSchema(TypeMirror type) {
String objectSchemaKey = getObjectSchemaName(type);

Expand Down
44 changes: 33 additions & 11 deletions tests/test-javalin-jsonb/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"openapi" : "3.0.1",
"info" : {
"title" : "Example service showing off the Path extension method of controller",
"title" : "Example service",
"description" : "Example Javalin controllers with Java and Maven",
"version" : ""
},
"tags" : [
{
"name" : "tag1",
"description" : "this is added to openapi tags"
"description" : "it's somethin"
},
{
"name" : "tag1",
"description" : "it's somethin"
"description" : "this is added to openapi tags"
}
],
"paths" : {
Expand Down Expand Up @@ -1156,7 +1156,12 @@
"type" : "string"
},
"type" : {
"$ref" : "#/components/schemas/ServerType"
"type" : "string",
"enum" : [
"PROXY",
"HIDE_N_SEEK",
"FFA"
]
}
}
}
Expand Down Expand Up @@ -1195,7 +1200,12 @@
"type" : "string"
},
"type" : {
"$ref" : "#/components/schemas/ServerType"
"type" : "string",
"enum" : [
"PROXY",
"HIDE_N_SEEK",
"FFA"
]
}
}
}
Expand Down Expand Up @@ -1229,7 +1239,12 @@
"name" : "type",
"in" : "query",
"schema" : {
"$ref" : "#/components/schemas/ServerType"
"type" : "string",
"enum" : [
"PROXY",
"HIDE_N_SEEK",
"FFA"
]
}
}
],
Expand Down Expand Up @@ -1261,7 +1276,12 @@
"schema" : {
"type" : "array",
"items" : {
"$ref" : "#/components/schemas/ServerType"
"type" : "string",
"enum" : [
"PROXY",
"HIDE_N_SEEK",
"FFA"
]
}
}
}
Expand Down Expand Up @@ -1299,7 +1319,12 @@
"name" : "type",
"in" : "query",
"schema" : {
"$ref" : "#/components/schemas/ServerType"
"type" : "string",
"enum" : [
"PROXY",
"HIDE_N_SEEK",
"FFA"
]
}
}
],
Expand Down Expand Up @@ -1903,9 +1928,6 @@
}
}
},
"ServerType" : {
"type" : "object"
},
"byte" : {
"type" : "object"
}
Expand Down
1 change: 1 addition & 0 deletions tests/test-jex/src/main/java/org/example/web/HelloDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public class HelloDto {
public int id;
@NotNull
public String name;
public ServerType serverType;
}
32 changes: 26 additions & 6 deletions tests/test-jex/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@
"name" : "type",
"in" : "query",
"schema" : {
"$ref" : "#/components/schemas/ServerType"
"type" : "string",
"enum" : [
"PROXY",
"HIDE_N_SEEK",
"FFA"
]
}
}
],
Expand Down Expand Up @@ -222,7 +227,12 @@
"schema" : {
"type" : "array",
"items" : {
"$ref" : "#/components/schemas/ServerType"
"type" : "string",
"enum" : [
"PROXY",
"HIDE_N_SEEK",
"FFA"
]
}
}
}
Expand Down Expand Up @@ -260,7 +270,12 @@
"name" : "type",
"in" : "query",
"schema" : {
"$ref" : "#/components/schemas/ServerType"
"type" : "string",
"enum" : [
"PROXY",
"HIDE_N_SEEK",
"FFA"
]
}
}
],
Expand Down Expand Up @@ -363,11 +378,16 @@
"name" : {
"type" : "string",
"nullable" : false
},
"serverType" : {
"type" : "string",
"enum" : [
"PROXY",
"HIDE_N_SEEK",
"FFA"
]
}
}
},
"ServerType" : {
"type" : "object"
}
}
}
Expand Down