Skip to content

Commit 78d96fc

Browse files
committed
#13 - OpenAPI requestBody for @Form or form parameters
1 parent 10692b1 commit 78d96fc

File tree

3 files changed

+96
-7
lines changed

3 files changed

+96
-7
lines changed

src/main/java/io/dinject/javalin/generator/ElementReader.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.dinject.controller.QueryParam;
1010
import io.dinject.javalin.generator.javadoc.Javadoc;
1111
import io.swagger.v3.oas.models.Operation;
12+
import io.swagger.v3.oas.models.media.Schema;
1213
import io.swagger.v3.oas.models.parameters.Parameter;
1314

1415
import javax.lang.model.element.Element;
@@ -147,17 +148,36 @@ void writeParamName(Append writer) {
147148

148149
void addMeta(ProcessingContext ctx, Javadoc javadoc, Operation operation) {
149150
if (!isJavalinContext()) {
151+
if (paramType == ParamType.FORM || paramType == ParamType.BODY) {
152+
addMetaRequestBody(ctx, javadoc, operation);
150153

151-
Parameter param = new Parameter();
152-
param.setName(varName);
153-
param.setDescription(javadoc.getParams().get(paramName));
154-
param.setIn(paramType.getType());
155-
param.setSchema(ctx.toSchema(rawType, element));
156-
157-
operation.addParametersItem(param);
154+
} else {
155+
Parameter param = new Parameter();
156+
param.setName(varName);
157+
param.setDescription(javadoc.getParams().get(paramName));
158+
159+
Schema schema = ctx.toSchema(rawType, element);
160+
if (paramType == ParamType.FORMPARAM) {
161+
ctx.addFormParam(operation, varName, schema);
162+
163+
} else {
164+
param.setSchema(schema);
165+
param.setIn(paramType.getType());
166+
operation.addParametersItem(param);
167+
}
168+
}
158169
}
159170
}
160171

172+
private void addMetaRequestBody(ProcessingContext ctx, Javadoc javadoc, Operation operation) {
173+
174+
Schema schema = ctx.toSchema(rawType, element);
175+
String description = javadoc.getParams().get(paramName);
176+
177+
boolean asForm = (paramType == ParamType.FORM);
178+
ctx.addRequestBody(operation, schema, asForm, description);
179+
}
180+
161181
void writeCtxGet(Append writer, PathSegments segments) {
162182

163183
if (isJavalinContext()) {

src/main/java/io/dinject/javalin/generator/ProcessingContext.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.dinject.javalin.generator.openapi.SchemaBuilder;
44
import io.swagger.v3.oas.models.OpenAPI;
5+
import io.swagger.v3.oas.models.Operation;
56
import io.swagger.v3.oas.models.PathItem;
67
import io.swagger.v3.oas.models.Paths;
78
import io.swagger.v3.oas.models.media.Content;
@@ -141,4 +142,12 @@ TypeMirror asMemberOf(DeclaredType declaredType, Element element) {
141142
PathItem pathItem(String fullPath) {
142143
return pathMap.computeIfAbsent(fullPath, s -> new PathItem());
143144
}
145+
146+
void addFormParam(Operation operation, String varName, Schema schema) {
147+
schemaBuilder.addFormParam(operation, varName, schema);
148+
}
149+
150+
void addRequestBody(Operation operation, Schema schema, boolean asForm, String description) {
151+
schemaBuilder.addRequestBody(operation, schema, asForm, description);
152+
}
144153
}

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import io.swagger.v3.oas.annotations.Hidden;
44
import io.swagger.v3.oas.models.Components;
55
import io.swagger.v3.oas.models.OpenAPI;
6+
import io.swagger.v3.oas.models.Operation;
67
import io.swagger.v3.oas.models.media.ArraySchema;
78
import io.swagger.v3.oas.models.media.Content;
89
import io.swagger.v3.oas.models.media.MapSchema;
910
import io.swagger.v3.oas.models.media.MediaType;
1011
import io.swagger.v3.oas.models.media.ObjectSchema;
1112
import io.swagger.v3.oas.models.media.Schema;
13+
import io.swagger.v3.oas.models.parameters.RequestBody;
1214

1315
import javax.lang.model.element.AnnotationMirror;
1416
import javax.lang.model.element.Element;
@@ -33,6 +35,9 @@
3335
*/
3436
public class SchemaBuilder {
3537

38+
private static final String APP_FORM = "application/x-www-form-urlencoded";
39+
private static final String APP_JSON = "application/json";
40+
3641
private final Types types;
3742
private final KnownTypes knownTypes;
3843
private final TypeMirror iterableType;
@@ -59,6 +64,61 @@ public Content createContent(TypeMirror returnType, String mediaType) {
5964
return content;
6065
}
6166

67+
/**
68+
* Add parameter as a form parameter.
69+
*/
70+
public void addFormParam(Operation operation, String varName, Schema schema) {
71+
RequestBody body = requestBody(operation);
72+
Schema formSchema = requestFormParamSchema(body);
73+
formSchema.addProperties(varName, schema);
74+
}
75+
76+
private Schema requestFormParamSchema(RequestBody body) {
77+
78+
final Content content = body.getContent();
79+
MediaType mediaType = content.get(APP_FORM);
80+
81+
Schema schema;
82+
if (mediaType != null) {
83+
schema = mediaType.getSchema();
84+
} else {
85+
schema = new Schema();
86+
schema.setType("object");
87+
mediaType = new MediaType();
88+
mediaType.schema(schema);
89+
content.addMediaType(APP_FORM, mediaType);
90+
}
91+
return schema;
92+
}
93+
94+
/**
95+
* Add as request body.
96+
*/
97+
public void addRequestBody(Operation operation, Schema schema, boolean asForm, String description) {
98+
99+
RequestBody body = requestBody(operation);
100+
body.setDescription(description);
101+
102+
MediaType mt = new MediaType();
103+
mt.schema(schema);
104+
105+
String mime = asForm ? APP_FORM : APP_JSON;
106+
body.getContent().addMediaType(mime, mt);
107+
}
108+
109+
private RequestBody requestBody(Operation operation) {
110+
111+
RequestBody body = operation.getRequestBody();
112+
if (body == null) {
113+
body = new RequestBody();
114+
body.setRequired(true);
115+
Content content = new Content();
116+
body.setContent(content);
117+
operation.setRequestBody(body);
118+
}
119+
return body;
120+
}
121+
62122
public Schema<?> toSchema(TypeMirror type) {
63123

64124
Schema<?> schema = knownTypes.createSchema(type.toString());

0 commit comments

Comments
 (0)