Skip to content

Commit

Permalink
Add ability to annotate parameters by Hidden annotaion
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 committed Jul 29, 2024
1 parent e6d3fa3 commit d0c4832
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;

/**
* Marks a given resource, class or bean type as hidden, skipping while reading / resolving
**/
@Target({METHOD, TYPE, FIELD, ANNOTATION_TYPE})
@Target({PARAMETER, METHOD, TYPE, FIELD, ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Hidden {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public static Parameter applyAnnotations(
}

for (Annotation annotation : annotations) {
if (annotation instanceof io.swagger.v3.oas.annotations.Hidden) {
return null;
}
if (annotation instanceof io.swagger.v3.oas.annotations.Parameter) {
io.swagger.v3.oas.annotations.Parameter p = (io.swagger.v3.oas.annotations.Parameter) annotation;
if (p.hidden()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public ResolvedParameter extractParameters(List<Annotation> annotations,
pp.setIn(COOKIE_PARAM);
pp.setName(param.value());
parameter = pp;
} else if (annotation instanceof io.swagger.v3.oas.annotations.Hidden) {
return new ResolvedParameter();
} else if (annotation instanceof io.swagger.v3.oas.annotations.Parameter) {
if (((io.swagger.v3.oas.annotations.Parameter) annotation).hidden()) {
return new ResolvedParameter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,24 @@ public void testHiddenAnnotatedUserResource() throws IOException {
" content:\n" +
" 'application/json': {}\n" +
" 'application/xml': {}\n" +
" /user/3:\n" +
" get:\n" +
" summary: Select user\n" +
" operationId: selectUser\n" +
" parameters:\n" +
" - name: id\n" +
" in: query\n" +
" description: User id\n" +
" required: true\n" +
" schema:\n" +
" type: integer\n" +
" format: int32\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" 'application/json': {}\n" +
" 'application/xml': {}\n" +
"components:\n" +
" schemas:\n" +
" UserResourceBean:\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/user")
Expand Down Expand Up @@ -50,6 +52,17 @@ public Response createUserWithHiddenBeanProperty(
@Parameter(description = "Created user object", required = true) UserResourceBean user) {
return Response.ok().entity("").build();
}

@GET
@Operation(summary = "Select user")
@Path("/3")
public Response selectUser(
@QueryParam("id") @Parameter(description = "User id", required = true) Integer id,
@QueryParam("name") @Parameter(hidden = true) String name,
@QueryParam("secondName") @Hidden String secondName
) {
return Response.ok().entity("").build();
}
}

public class UserResourceBean {
Expand Down

0 comments on commit d0c4832

Please sign in to comment.