Skip to content

Commit ef652c6

Browse files
committed
rename annotation
1 parent fdf5b58 commit ef652c6

File tree

5 files changed

+55
-18
lines changed

5 files changed

+55
-18
lines changed

http-api/src/main/java/io/avaje/http/api/OpenAPIReturns.java renamed to http-api/src/main/java/io/avaje/http/api/OpenAPIResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
@Target(value = METHOD)
2525
@Retention(value = RUNTIME)
26-
@Repeatable(OpenAPIReturnsContainer.class)
27-
public @interface OpenAPIReturns {
26+
@Repeatable(OpenAPIResponses.class)
27+
public @interface OpenAPIResponse {
2828

2929
/** the http status code of this response */
3030
String responseCode();

http-api/src/main/java/io/avaje/http/api/OpenAPIReturnsContainer.java renamed to http-api/src/main/java/io/avaje/http/api/OpenAPIResponses.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import java.lang.annotation.Retention;
77
import java.lang.annotation.Target;
88

9-
/** a container for the OpenAPIReturns annotation to make it repeatable */
9+
/**
10+
* Container for repeatable {@link OpenAPIResponse} annotation
11+
*
12+
* @see OpenAPIResponse
13+
*/
1014
@Target(value = METHOD)
1115
@Retention(value = RUNTIME)
12-
public @interface OpenAPIReturnsContainer {
13-
OpenAPIReturns[] value();
16+
public @interface OpenAPIResponses {
17+
OpenAPIResponse[] value();
1418
}

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import java.lang.annotation.Annotation;
44
import java.util.ArrayList;
5+
import java.util.Arrays;
56
import java.util.List;
7+
import java.util.Optional;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
610

711
import javax.lang.model.element.Element;
812
import javax.lang.model.element.ExecutableElement;
@@ -15,7 +19,8 @@
1519
import io.avaje.http.api.Delete;
1620
import io.avaje.http.api.Form;
1721
import io.avaje.http.api.Get;
18-
import io.avaje.http.api.OpenAPIReturns;
22+
import io.avaje.http.api.OpenAPIResponse;
23+
import io.avaje.http.api.OpenAPIResponses;
1924
import io.avaje.http.api.Patch;
2025
import io.avaje.http.api.Post;
2126
import io.avaje.http.api.Produces;
@@ -49,7 +54,7 @@ public class MethodReader {
4954

5055
private final String produces;
5156

52-
private final OpenAPIReturns[] apiResponses;
57+
private final List<OpenAPIResponse> apiResponses;
5358

5459
private final ExecutableType actualExecutable;
5560
private final List<? extends TypeMirror> actualParams;
@@ -128,12 +133,20 @@ public Javadoc javadoc() {
128133
}
129134

130135
private String produces(ControllerReader bean) {
131-
final Produces produces = findAnnotation(Produces.class);
136+
final var produces = findAnnotation(Produces.class);
132137
return (produces != null) ? produces.value() : bean.produces();
133138
}
134139

135-
private OpenAPIReturns[] getApiResponses() {
136-
return element.getAnnotationsByType(OpenAPIReturns.class);
140+
private List<OpenAPIResponse> getApiResponses() {
141+
final var container =
142+
Optional.ofNullable(findAnnotation(OpenAPIResponses.class)).stream()
143+
.map(OpenAPIResponses::value)
144+
.flatMap(Arrays::stream);
145+
146+
return Stream.concat(container, Arrays.stream(element.getAnnotationsByType(OpenAPIResponse.class)))
147+
.collect(Collectors.toList());
148+
149+
137150
}
138151

139152
public <A extends Annotation> A findAnnotation(Class<A> type) {
@@ -230,7 +243,7 @@ public String produces() {
230243
return produces;
231244
}
232245

233-
public OpenAPIReturns[] apiResponses() {
246+
public List<OpenAPIResponse> apiResponses() {
234247
return apiResponses;
235248
}
236249

@@ -291,5 +304,4 @@ public String bodyName() {
291304
}
292305
return "body";
293306
}
294-
295307
}

tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/test/OpenAPIController.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import io.avaje.http.api.Controller;
66
import io.avaje.http.api.Get;
77
import io.avaje.http.api.MediaType;
8-
import io.avaje.http.api.OpenAPIReturns;
8+
import io.avaje.http.api.OpenAPIResponse;
9+
import io.avaje.http.api.OpenAPIResponses;
910
import io.avaje.http.api.Path;
1011
import io.avaje.http.api.Post;
1112
import io.avaje.http.api.Produces;
@@ -32,7 +33,7 @@ public class OpenAPIController {
3233
*/
3334
@Get("/get")
3435
@Produces(MediaType.TEXT_PLAIN)
35-
@OpenAPIReturns(responseCode = "200", type = String.class)
36+
@OpenAPIResponse(responseCode = "200", type = String.class)
3637
void ctxEndpoint(Context ctx) {
3738
ctx.contentType(MediaType.TEXT_PLAIN).result("healthlmao");
3839
}
@@ -45,12 +46,12 @@ void ctxEndpoint(Context ctx) {
4546
*/
4647
@Post("/post")
4748
@Tag(name = "tag1", description = "this is added to openapi tags")
48-
@OpenAPIReturns(responseCode = "200", description = "overrides @return javadoc description")
49-
@OpenAPIReturns(responseCode = "201")
50-
@OpenAPIReturns(
49+
@OpenAPIResponse(responseCode = "200", description = "overrides @return javadoc description")
50+
@OpenAPIResponse(responseCode = "201")
51+
@OpenAPIResponse(
5152
responseCode = "400",
5253
description = "User not found (Will not have an associated response schema)")
53-
@OpenAPIReturns(
54+
@OpenAPIResponse(
5455
responseCode = "500",
5556
description = "Some other Error (Will have this error class as the response class)",
5657
type = ErrorResponse.class)
@@ -66,6 +67,13 @@ Person testPost(Person b) {
6667
*/
6768
@Deprecated
6869
@Post("/post1")
70+
@OpenAPIResponses({
71+
@OpenAPIResponse(responseCode = "400", description = "User not found"),
72+
@OpenAPIResponse(
73+
responseCode = "500",
74+
description = "Some other Error",
75+
type = ErrorResponse.class)
76+
})
6977
Person testPostl(List<Person> m) {
7078

7179
return new Person(0, "baby");

tests/test-javalin-jsonb/src/test/java/io/avaje/http/generator/expectedOpenApi.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@
106106
"required": true
107107
},
108108
"responses": {
109+
"400": {
110+
"description": "User not found"
111+
},
112+
"500": {
113+
"description": "Some other Error",
114+
"content": {
115+
"application/json": {
116+
"schema": {
117+
"$ref": "#/components/schemas/ErrorResponse"
118+
}
119+
}
120+
}
121+
},
109122
"201": {
110123
"description": "the response body (from javadoc)",
111124
"content": {

0 commit comments

Comments
 (0)