Skip to content

Inherit interface roles and class openAPIResponse #152

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 2 commits into from
Feb 6, 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
21 changes: 18 additions & 3 deletions http-api/src/main/java/io/avaje/http/api/OpenAPIResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.http.api;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Repeatable;
Expand All @@ -11,7 +12,9 @@
* Specify endpoint response status code/description/type.
*
* <p>When not specified the default 2xx openAPI generation is based on the javadoc of the method.
* <p> Will not override the default 2xx generated openapi unless status code is 2xx
*
* <p>Will not override the default 2xx generated openapi unless status code is 2xx
*
* <pre>{@code
* @Post("/post")
* @OpenAPIReturns(responseCode = "200", description = "from annotaion")
Expand All @@ -20,9 +23,21 @@
* ResponseModel endpoint() {}
*
* }</pre>
*
* <p>Can also be placed on a class to add to every method in the controller.
*
* <pre>{@code
* @OpenAPIResponse(
* responseCode = "403",
* description = "Insufficient rights to this resource."
* )
* public class MyController {
* ...
* }
* }</pre>
*/
@Target(value = METHOD)
@Retention(value = RUNTIME)
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@Repeatable(OpenAPIResponses.class)
public @interface OpenAPIResponse {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.http.api;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
Expand All @@ -11,8 +12,8 @@
*
* @see OpenAPIResponse
*/
@Target(value = METHOD)
@Retention(value = RUNTIME)
@Target({TYPE, METHOD})
@Retention(RUNTIME)
public @interface OpenAPIResponses {
OpenAPIResponse[] value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -21,6 +22,8 @@
import javax.validation.Valid;

import io.avaje.http.api.Controller;
import io.avaje.http.api.OpenAPIResponse;
import io.avaje.http.api.OpenAPIResponses;
import io.avaje.http.api.Path;
import io.avaje.http.api.Produces;
import io.swagger.v3.oas.annotations.Hidden;
Expand All @@ -38,6 +41,7 @@ public class ControllerReader {
private final List<MethodReader> methods = new ArrayList<>();
private final Set<String> staticImportTypes = new TreeSet<>();
private final Set<String> importTypes = new TreeSet<>();
private final List<OpenAPIResponse> apiResponses;

/**
* The produces media type for the controller. Null implies JSON.
Expand All @@ -57,12 +61,46 @@ public ControllerReader(TypeElement beanType, ProcessingContext ctx) {
this.ctx = ctx;
this.interfaces = initInterfaces();
this.interfaceMethods = initInterfaceMethods();
this.roles = Util.findRoles(beanType);
this.roles = buildRoles();
if (ctx.isOpenApiAvailable()) {
docHidden = initDocHidden();
}
this.hasValid = initHasValid();
this.produces = initProduces();
this.apiResponses = buildApiResponses();
}

private List<OpenAPIResponse> buildApiResponses() {
final var responses = new ArrayList<OpenAPIResponse>();

Optional.ofNullable(beanType.getAnnotation(OpenAPIResponses.class)).stream()
.map(OpenAPIResponses::value)
.flatMap(Arrays::stream)
.forEach(responses::add);

Arrays.stream(beanType.getAnnotationsByType(OpenAPIResponse.class)).forEach(responses::add);

for (final Element anInterface : interfaces) {

Optional.ofNullable(anInterface.getAnnotation(OpenAPIResponses.class)).stream()
.map(OpenAPIResponses::value)
.flatMap(Arrays::stream)
.forEach(responses::add);

Arrays.stream(anInterface.getAnnotationsByType(OpenAPIResponse.class))
.forEach(responses::add);
}

return responses;
}

private ArrayList<String> buildRoles() {
final var roleList = new ArrayList<>(Util.findRoles(beanType));

for (final Element anInterface : interfaces) {
roleList.addAll(Util.findRoles(anInterface));
}
return roleList;
}

protected void addImports(boolean withSingleton) {
Expand Down Expand Up @@ -267,6 +305,10 @@ public List<MethodReader> methods() {
return methods;
}

public List<OpenAPIResponse> openApiResponses() {
return apiResponses;
}

public String path() {

return Optional.ofNullable(findAnnotation(Controller.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ private List<OpenAPIResponse> buildApiResponses() {
.map(OpenAPIResponses::value)
.flatMap(Arrays::stream),
Arrays.stream(method.getAnnotationsByType(OpenAPIResponse.class))));

return Stream.concat(methodResponses, superMethodResponses).collect(Collectors.toList());

var responses =
Stream.concat(methodResponses, superMethodResponses).collect(Collectors.toList());

responses.addAll(bean.openApiResponses());
return responses;
}

public <A extends Annotation> A findAnnotation(Class<A> type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.example.myapp.web.test;

import org.example.myapp.web.AppRoles;
import org.example.myapp.web.Roles;

import io.avaje.http.api.Get;
import io.avaje.http.api.MediaType;
import io.avaje.http.api.OpenAPIResponse;
Expand All @@ -10,11 +13,13 @@
import io.swagger.v3.oas.annotations.tags.Tag;

@Path("javalin")
@Roles(AppRoles.ANYONE)
@OpenAPIDefinition(
info =
@Info(
title = "Example service showing off the Path extension method of controller",
description = ""))
@OpenAPIResponse(responseCode = "403", description = "Not Authorized")
public interface HealthController {
/**
* Standard Get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
}
}
},
"403" : {
"description" : "Not Authorized"
},
"200" : {
"description" : "a health check",
"content" : {
Expand Down