|
| 1 | +/* |
| 2 | + * Copyright 2023 Signal Messenger, LLC |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +package org.signal.openapi; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.annotation.JsonView; |
| 9 | +import com.fasterxml.jackson.databind.JavaType; |
| 10 | +import com.fasterxml.jackson.databind.type.SimpleType; |
| 11 | +import io.dropwizard.auth.Auth; |
| 12 | +import io.swagger.v3.jaxrs2.ResolvedParameter; |
| 13 | +import io.swagger.v3.jaxrs2.ext.AbstractOpenAPIExtension; |
| 14 | +import io.swagger.v3.jaxrs2.ext.OpenAPIExtension; |
| 15 | +import io.swagger.v3.oas.models.Components; |
| 16 | +import java.lang.annotation.Annotation; |
| 17 | +import java.lang.reflect.Type; |
| 18 | +import java.util.Iterator; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.ServiceLoader; |
| 22 | +import java.util.Set; |
| 23 | +import javax.ws.rs.Consumes; |
| 24 | +import org.whispersystems.textsecuregcm.auth.AuthenticatedAccount; |
| 25 | +import org.whispersystems.textsecuregcm.auth.DisabledPermittedAuthenticatedAccount; |
| 26 | + |
| 27 | +/** |
| 28 | + * One of the extension mechanisms of Swagger Core library (OpenAPI processor) is via custom implementations |
| 29 | + * of the {@link AbstractOpenAPIExtension} class. |
| 30 | + * <p/> |
| 31 | + * The purpose of this extension is to customize certain aspects of the OpenAPI model generation on a lower level. |
| 32 | + * This extension works in coordination with {@link OpenApiReader} that has access to the model on a higher level. |
| 33 | + * <p/> |
| 34 | + * The extension is enabled by being listed in {@code META-INF/services/io.swagger.v3.jaxrs2.ext.OpenAPIExtension} file. |
| 35 | + * @see ServiceLoader |
| 36 | + * @see OpenApiReader |
| 37 | + * @see <a href="https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Extensions">Swagger 2.X Extensions</a> |
| 38 | + */ |
| 39 | +public class OpenApiExtension extends AbstractOpenAPIExtension { |
| 40 | + |
| 41 | + public static final ResolvedParameter AUTHENTICATED_ACCOUNT = new ResolvedParameter(); |
| 42 | + |
| 43 | + public static final ResolvedParameter OPTIONAL_AUTHENTICATED_ACCOUNT = new ResolvedParameter(); |
| 44 | + |
| 45 | + public static final ResolvedParameter DISABLED_PERMITTED_AUTHENTICATED_ACCOUNT = new ResolvedParameter(); |
| 46 | + |
| 47 | + public static final ResolvedParameter OPTIONAL_DISABLED_PERMITTED_AUTHENTICATED_ACCOUNT = new ResolvedParameter(); |
| 48 | + |
| 49 | + /** |
| 50 | + * When parsing endpoint methods, Swagger will treat the first parameter not annotated as header/path/query param |
| 51 | + * as a request body (and will ignore other not annotated parameters). In our case, this behavior conflicts with |
| 52 | + * the {@code @Auth}-annotated parameters. Here we're checking if parameters are known to be anything other than |
| 53 | + * a body and return an appropriate {@link ResolvedParameter} representation. |
| 54 | + */ |
| 55 | + @Override |
| 56 | + public ResolvedParameter extractParameters( |
| 57 | + final List<Annotation> annotations, |
| 58 | + final Type type, |
| 59 | + final Set<Type> typesToSkip, |
| 60 | + final Components components, |
| 61 | + final Consumes classConsumes, |
| 62 | + final Consumes methodConsumes, |
| 63 | + final boolean includeRequestBody, |
| 64 | + final JsonView jsonViewAnnotation, |
| 65 | + final Iterator<OpenAPIExtension> chain) { |
| 66 | + |
| 67 | + if (annotations.stream().anyMatch(a -> a.annotationType().equals(Auth.class))) { |
| 68 | + // this is the case of authenticated endpoint, |
| 69 | + if (type instanceof SimpleType simpleType |
| 70 | + && simpleType.getRawClass().equals(AuthenticatedAccount.class)) { |
| 71 | + return AUTHENTICATED_ACCOUNT; |
| 72 | + } |
| 73 | + if (type instanceof SimpleType simpleType |
| 74 | + && simpleType.getRawClass().equals(DisabledPermittedAuthenticatedAccount.class)) { |
| 75 | + return DISABLED_PERMITTED_AUTHENTICATED_ACCOUNT; |
| 76 | + } |
| 77 | + if (type instanceof SimpleType simpleType |
| 78 | + && isOptionalOfType(simpleType, AuthenticatedAccount.class)) { |
| 79 | + return OPTIONAL_AUTHENTICATED_ACCOUNT; |
| 80 | + } |
| 81 | + if (type instanceof SimpleType simpleType |
| 82 | + && isOptionalOfType(simpleType, DisabledPermittedAuthenticatedAccount.class)) { |
| 83 | + return OPTIONAL_DISABLED_PERMITTED_AUTHENTICATED_ACCOUNT; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return super.extractParameters( |
| 88 | + annotations, |
| 89 | + type, |
| 90 | + typesToSkip, |
| 91 | + components, |
| 92 | + classConsumes, |
| 93 | + methodConsumes, |
| 94 | + includeRequestBody, |
| 95 | + jsonViewAnnotation, |
| 96 | + chain); |
| 97 | + } |
| 98 | + |
| 99 | + private static boolean isOptionalOfType(final SimpleType simpleType, final Class<?> expectedType) { |
| 100 | + if (!simpleType.getRawClass().equals(Optional.class)) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + final List<JavaType> typeParameters = simpleType.getBindings().getTypeParameters(); |
| 104 | + if (typeParameters.isEmpty()) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + return typeParameters.get(0) instanceof SimpleType optionalParameterType |
| 108 | + && optionalParameterType.getRawClass().equals(expectedType); |
| 109 | + } |
| 110 | +} |
0 commit comments