Skip to content

Commit dfff472

Browse files
committed
#1768: Added support for OpenAPI operating hiding annotations
1 parent e3d594c commit dfff472

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/AnnotationParser.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import io.jooby.annotations.Path;
1818
import io.jooby.annotations.PathParam;
1919
import io.jooby.annotations.QueryParam;
20+
import io.swagger.v3.oas.annotations.Hidden;
21+
import io.swagger.v3.oas.annotations.Operation;
2022
import io.swagger.v3.oas.models.media.Content;
2123
import io.swagger.v3.oas.models.media.ObjectSchema;
2224
import io.swagger.v3.oas.models.media.Schema;
@@ -49,7 +51,7 @@
4951
import java.util.stream.Collectors;
5052
import java.util.stream.Stream;
5153

52-
import static io.jooby.internal.openapi.AsmUtils.findAnnotationByType;
54+
import static io.jooby.internal.openapi.AsmUtils.*;
5355
import static io.jooby.internal.openapi.TypeFactory.KT_FUN_0;
5456
import static io.jooby.internal.openapi.TypeFactory.KT_KLASS;
5557
import static java.util.Collections.singletonList;
@@ -236,6 +238,11 @@ private static List<OperationExt> routerMethod(ParserContext ctx, String prefix,
236238
List<ParameterExt> arguments = routerArguments(ctx, method, requestBody::set);
237239
ResponseExt response = returnTypes(method);
238240

241+
// If the method is hidden, don't generate an operation for it
242+
if (isHidden(method.visibleAnnotations)) {
243+
return result;
244+
}
245+
239246
for (String httpMethod : httpMethod(method.visibleAnnotations)) {
240247
for (String pattern : httpPattern(ctx, classNode, method, httpMethod)) {
241248
OperationExt operation = new OperationExt(
@@ -266,6 +273,24 @@ private static boolean isDeprecated(List<AnnotationNode> annotations) {
266273
return false;
267274
}
268275

276+
private static boolean isHidden(List<AnnotationNode> annotations) {
277+
if (annotations != null) {
278+
// If the method is annotated with @Hidden, it's always hidden
279+
boolean hiddenAnnotationExists = annotations.stream()
280+
.anyMatch(a -> a.desc.equals(Type.getDescriptor(Hidden.class)));
281+
282+
if (hiddenAnnotationExists) {
283+
return true;
284+
}
285+
286+
// If the method is annotated with @Operation, and the value of "hidden" is true, it's hidden
287+
return findAnnotationByType(annotations, Operation.class)
288+
.stream()
289+
.anyMatch(it -> boolValue(toMap(it), "hidden"));
290+
}
291+
return false;
292+
}
293+
269294
private static ResponseExt returnTypes(MethodNode method) {
270295
Signature signature = Signature.create(method);
271296
String desc = Optional.ofNullable(method.signature).orElse(method.desc);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package issues;
2+
3+
import io.jooby.openapi.OpenAPIResult;
4+
import io.jooby.openapi.OpenAPITest;
5+
import issues.i1768.App1768;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class Issue1768 {
10+
@OpenAPITest(value = App1768.class)
11+
public void shouldIgnoreHiddenOperations(OpenAPIResult result) {
12+
assertEquals("openapi: 3.0.1\n" +
13+
"info:\n" +
14+
" title: 1768 API\n" +
15+
" description: 1768 API description\n" +
16+
" version: \"1.0\"\n" +
17+
"paths:\n" +
18+
" /c/not-hidden:\n" +
19+
" get:\n" +
20+
" operationId: notHidden\n" +
21+
" responses:\n" +
22+
" \"200\":\n" +
23+
" description: Success\n", result.toYaml());
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package issues.i1768;
2+
3+
import io.jooby.Jooby;
4+
5+
public class App1768 extends Jooby {
6+
{
7+
mvc(new Controller1768());
8+
}
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package issues.i1768;
2+
3+
import io.jooby.annotations.GET;
4+
import io.jooby.annotations.Path;
5+
import io.swagger.v3.oas.annotations.Hidden;
6+
import io.swagger.v3.oas.annotations.Operation;
7+
8+
@Path("/c")
9+
public class Controller1768 {
10+
11+
@Hidden
12+
@GET("/hidden-with-annotation")
13+
public void hiddenWithAnnotation() {
14+
15+
}
16+
17+
@Operation(hidden = true)
18+
@GET("/hidden-with-operation")
19+
public void hiddenWithOperation() {
20+
21+
}
22+
23+
@GET("/not-hidden")
24+
public void notHidden() {
25+
26+
}
27+
}

0 commit comments

Comments
 (0)