Skip to content

Commit 5a1d8d4

Browse files
committed
use swagger APIResponse for more openAPI generation
1 parent 81fe165 commit 5a1d8d4

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/MethodReader.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package io.avaje.http.generator.core;
22

3+
import java.lang.annotation.Annotation;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import javax.lang.model.element.Element;
8+
import javax.lang.model.element.ExecutableElement;
9+
import javax.lang.model.element.VariableElement;
10+
import javax.lang.model.type.ExecutableType;
11+
import javax.lang.model.type.TypeKind;
12+
import javax.lang.model.type.TypeMirror;
13+
import javax.validation.Valid;
14+
315
import io.avaje.http.api.Delete;
416
import io.avaje.http.api.Form;
517
import io.avaje.http.api.Get;
@@ -9,20 +21,10 @@
921
import io.avaje.http.api.Put;
1022
import io.avaje.http.generator.core.javadoc.Javadoc;
1123
import io.avaje.http.generator.core.openapi.MethodDocBuilder;
24+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
1225
import io.swagger.v3.oas.annotations.tags.Tag;
1326
import io.swagger.v3.oas.annotations.tags.Tags;
1427

15-
import javax.lang.model.element.Element;
16-
import javax.lang.model.element.ExecutableElement;
17-
import javax.lang.model.element.VariableElement;
18-
import javax.lang.model.type.ExecutableType;
19-
import javax.lang.model.type.TypeKind;
20-
import javax.lang.model.type.TypeMirror;
21-
import javax.validation.Valid;
22-
import java.lang.annotation.Annotation;
23-
import java.util.ArrayList;
24-
import java.util.List;
25-
2628
public class MethodReader {
2729

2830
private final ProcessingContext ctx;
@@ -46,13 +48,19 @@ public class MethodReader {
4648

4749
private final String produces;
4850

51+
private final ApiResponse[] apiResponses;
52+
4953
private final ExecutableType actualExecutable;
5054
private final List<? extends TypeMirror> actualParams;
5155

5256
private final PathSegments pathSegments;
5357
private final boolean hasValid;
5458

55-
MethodReader(ControllerReader bean, ExecutableElement element, ExecutableType actualExecutable, ProcessingContext ctx) {
59+
MethodReader(
60+
ControllerReader bean,
61+
ExecutableElement element,
62+
ExecutableType actualExecutable,
63+
ProcessingContext ctx) {
5664
this.ctx = ctx;
5765
this.bean = bean;
5866
this.element = element;
@@ -62,6 +70,7 @@ public class MethodReader {
6270
this.methodRoles = Util.findRoles(element);
6371
this.javadoc = Javadoc.parse(ctx.getDocComment(element));
6472
this.produces = produces(bean);
73+
this.apiResponses = getApiResponses();
6574
initWebMethodViaAnnotation();
6675
if (isWebMethod()) {
6776
this.hasValid = findAnnotation(Valid.class) != null;
@@ -122,6 +131,10 @@ private String produces(ControllerReader bean) {
122131
return (produces != null) ? produces.value() : bean.produces();
123132
}
124133

134+
private ApiResponse[] getApiResponses() {
135+
return element.getAnnotationsByType(ApiResponse.class);
136+
}
137+
125138
public <A extends Annotation> A findAnnotation(Class<A> type) {
126139
A annotation = element.getAnnotation(type);
127140
if (annotation != null) {
@@ -216,6 +229,10 @@ public String produces() {
216229
return produces;
217230
}
218231

232+
public ApiResponse[] apiResponses() {
233+
return apiResponses;
234+
}
235+
219236
public TypeMirror returnType() {
220237
if (actualExecutable != null) {
221238
return actualExecutable.getReturnType();

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,22 @@ public void build() {
8282
String contentMediaType = (produces == null) ? MediaType.APPLICATION_JSON : produces;
8383
response.setContent(ctx.createContent(methodReader.returnType(), contentMediaType));
8484
}
85-
responses.addApiResponse(methodReader.statusCode(), response);
85+
var override2xx = false;
86+
for (final var responseAnnotation : methodReader.apiResponses()) {
87+
final var newResponse = new ApiResponse();
88+
89+
if (responseAnnotation.description().isEmpty())
90+
newResponse.setDescription(response.getDescription());
91+
else newResponse.setDescription(responseAnnotation.description());
92+
93+
// if user wants to define their own 2xx status code
94+
if (responseAnnotation.responseCode().startsWith("2")) {
95+
newResponse.setContent(response.getContent());
96+
override2xx = true;
97+
}
98+
responses.addApiResponse(responseAnnotation.responseCode(), newResponse);
99+
}
100+
if (!override2xx) responses.addApiResponse(methodReader.statusCode(), response);
86101
}
87102

88103
DocContext getContext() {

0 commit comments

Comments
 (0)