Skip to content

Commit

Permalink
fix: do not scan resource methods hidden by an override
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Dec 9, 2024
1 parent 875dc7a commit 45549ad
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Expand Down Expand Up @@ -229,6 +231,8 @@ default Set<String> processTags(final AnnotationScannerContext context, final An
* @return all methods from the provided class and its ancestors
*/
default List<MethodInfo> getResourceMethods(final AnnotationScannerContext context, ClassInfo resource) {
Predicate<MethodInfo> hidden = context.io().operationIO()::isHidden;

Type resourceType = Type.create(resource.name(), Type.Kind.CLASS);
AugmentedIndexView index = context.getAugmentedIndex();
Map<ClassInfo, Type> chain = index.inheritanceChain(resource, resourceType);
Expand All @@ -249,6 +253,19 @@ default List<MethodInfo> getResourceMethods(final AnnotationScannerContext conte
.forEach(methods::add);
}

Set<MethodInfo> hiddenMethods = new HashSet<>();

methods.stream().filter(hidden).forEach(method -> {
hiddenMethods.add(method);
index.ancestry(method)
.values()
.stream()
.filter(Objects::nonNull)
.forEach(hiddenMethods::add);
});

methods.removeIf(hiddenMethods::contains);

return methods;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import java.time.LocalDate;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.openapi.annotations.Operation;

@Path(value = "/hi")
public class ExampleResource1 extends GenericResource implements Greetable {
Expand All @@ -26,4 +31,12 @@ public String greet(GreetingBean bean) {
return "hi " + bean.name + ", from: " + from + "; on date: " + date;
}

@Override
@GET
@Path(value = "/extension-alt")
@Produces(value = MediaType.TEXT_PLAIN)
@Operation(description = "example1 alternate extension")
public String helloExtensionAlt() {
return "hello example1";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.ParameterIn;
import org.eclipse.microprofile.openapi.annotations.enums.ParameterStyle;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
Expand Down Expand Up @@ -41,4 +42,12 @@ public String greet(GreetingBean bean) {
return "hello " + bean.name + ", from: " + from + "; on date: " + date;
}

@Override
@GET
@Path(value = "/extension-alt")
@Produces(value = MediaType.TEXT_PLAIN)
@Operation(hidden = true)
public String helloExtensionAlt() {
return "hello example2";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.openapi.annotations.Operation;

public class GenericResource {

@GET
Expand All @@ -14,4 +16,11 @@ public String helloExtension() {
return "hello extension";
}

@GET
@Path(value = "/extension-alt")
@Produces(value = MediaType.TEXT_PLAIN)
@Operation(description = "alternate extension")
public String helloExtensionAlt() {
return "hello extension alternate";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import java.time.LocalDate;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.openapi.annotations.Operation;

@Path(value = "/hi")
public class ExampleResource1 extends GenericResource implements Greetable {
Expand All @@ -26,4 +31,12 @@ public String greet(GreetingBean bean) {
return "hi " + bean.name + ", from: " + from + "; on date: " + date;
}

@Override
@GET
@Path(value = "/extension-alt")
@Produces(value = MediaType.TEXT_PLAIN)
@Operation(description = "example1 alternate extension")
public String helloExtensionAlt() {
return "hello example1";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.ParameterIn;
import org.eclipse.microprofile.openapi.annotations.enums.ParameterStyle;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
Expand Down Expand Up @@ -41,4 +42,12 @@ public String greet(GreetingBean bean) {
return "hello " + bean.name + ", from: " + from + "; on date: " + date;
}

@Override
@GET
@Path(value = "/extension-alt")
@Produces(value = MediaType.TEXT_PLAIN)
@Operation(hidden = true)
public String helloExtensionAlt() {
return "hello example2";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.openapi.annotations.Operation;

public class GenericResource {

@GET
Expand All @@ -14,4 +16,12 @@ public String helloExtension() {
return "hello extension";
}

@GET
@Path(value = "/extension-alt")
@Produces(value = MediaType.TEXT_PLAIN)
@Operation(description = "alternate extension")
public String helloExtensionAlt() {
return "hello extension alternate";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@
}
}
},
"/hi/extension-alt" : {
"get" : {
"description" : "example1 alternate extension",
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"text/plain" : {
"schema" : {
"type" : "string"
}
}
}
}
}
},
"parameters" : [ {
"allowEmptyValue" : true,
"description" : "The local date when the greeting is sent",
"in" : "header",
"name" : "date",
"schema" : {
"$ref" : "#/components/schemas/LocalDate"
}
} ]
},
"/hello/greet/{from}": {
"parameters": [
{
Expand Down

0 comments on commit 45549ad

Please sign in to comment.