-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenApiExtension.java
67 lines (60 loc) · 3.03 KB
/
OpenApiExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package org.signal.openapi;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeBindings;
import io.dropwizard.auth.Auth;
import io.swagger.v3.jaxrs2.ResolvedParameter;
import io.swagger.v3.jaxrs2.ext.AbstractOpenAPIExtension;
import io.swagger.v3.oas.models.Components;
import org.whispersystems.textsecuregcm.auth.AuthenticatedAccount;
import org.whispersystems.textsecuregcm.auth.DisabledPermittedAuthenticatedAccount;
import javax.ws.rs.Consumes;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class OpenApiExtension extends AbstractOpenAPIExtension {
private static final Class<Auth> AUTH_ANNOTATION_CLASS = Auth.class;
private static final ResolvedParameter AUTHENTICATED_ACCOUNT = new ResolvedParameter();
private static final ResolvedParameter OPTIONAL_AUTHENTICATED_ACCOUNT = new ResolvedParameter();
private static final ResolvedParameter DISABLED_PERMITTED_AUTHENTICATED_ACCOUNT = new ResolvedParameter();
private static final ResolvedParameter OPTIONAL_DISABLED_PERMITTED_AUTHENTICATED_ACCOUNT = new ResolvedParameter();
@Override
public ResolvedParameter extractParameters(
List<Annotation> annotations,
Type type,
Set<Type> typesToSkip,
Components components,
Consumes classConsumes,
Consumes methodConsumes,
boolean includeRequestBody,
JsonView jsonViewAnnotation,
Iterator<OpenAPIExtension> chain) {
if (annotations.stream().anyMatch(a -> a.annotationType().equals(AUTH_ANNOTATION_CLASS))) {
Optional<Class<?>> rawClass = extractRawClass(type);
if (rawClass.isPresent()) {
if (AuthenticatedAccount.class.isAssignableFrom(rawClass.get())) {
return AUTHENTICATED_ACCOUNT;
} else if (DisabledPermittedAuthenticatedAccount.class.isAssignableFrom(rawClass.get())) {
return DISABLED_PERMITTED_AUTHENTICATED_ACCOUNT;
} else if (Optional.class.isAssignableFrom(rawClass.get())) {
Optional<Class<?>> parameterType = extractParameterType(type);
if (parameterType.isPresent() && AuthenticatedAccount.class.isAssignableFrom(parameterType.get())) {
return OPTIONAL_AUTHENTICATED_ACCOUNT;
} else if (parameterType.isPresent()
&& DisabledPermittedAuthenticatedAccount.class.isAssignableFrom(parameterType.get())) {
return OPTIONAL_DISABLED_PERMITTED_AUTHENTICATED_ACCOUNT;
}
}
}
}
return super.extractParameters(
annotations,
type,
typesToSkip,
components,
classConsumes,
methodConsumes,
includeRequestBody