Skip to content

Commit

Permalink
Remove Hardcoded Swagger Version (#13917)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitsultana authored Aug 30, 2024
1 parent 7522d8a commit e14d887
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
package org.apache.pinot.common.swagger;

import io.swagger.jaxrs.config.BeanConfig;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Objects;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.pinot.common.utils.PinotStaticHttpHandler;
import org.apache.pinot.spi.utils.CommonConstants;
import org.glassfish.grizzly.http.server.CLStaticHttpHandler;
Expand Down Expand Up @@ -53,8 +57,28 @@ public static void setupSwagger(String componentType, String resourcePackage, bo
// map both /api and /help to swagger docs. /api because it looks nice. /help for backward compatibility
httpServer.getServerConfiguration().addHttpHandler(staticHttpHandler, "/api/", "/help/");

URL swaggerDistLocation = classLoader.getResource(CommonConstants.CONFIG_OF_SWAGGER_RESOURCES_PATH);
String swaggerVersion = findSwaggerVersion(classLoader);
URL swaggerDistLocation = classLoader.getResource(
CommonConstants.CONFIG_OF_SWAGGER_RESOURCES_PATH + swaggerVersion + "/");
CLStaticHttpHandler swaggerDist = new PinotStaticHttpHandler(new URLClassLoader(new URL[]{swaggerDistLocation}));
httpServer.getServerConfiguration().addHttpHandler(swaggerDist, "/swaggerui-dist/");
}

private static String findSwaggerVersion(ClassLoader classLoader) {
try {
Properties pomProperties = new Properties();
InputStream inputStream = Objects.requireNonNull(
classLoader.getResourceAsStream(CommonConstants.SWAGGER_POM_PROPERTIES_PATH),
"Unable to find pom properties file: " + CommonConstants.SWAGGER_POM_PROPERTIES_PATH);
pomProperties.load(inputStream);
String version = pomProperties.getProperty("version");
if (StringUtils.isEmpty(version)) {
throw new IllegalStateException("Unable to find version in swagger pom properties file. Available keys: "
+ pomProperties.keySet());
}
return version;
} catch (Exception e) {
throw new RuntimeException("Error finding swagger version", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ private CommonConstants() {
"org.apache.pinot.spi.eventlistener.query.NoOpBrokerQueryEventListener";

public static final String SWAGGER_AUTHORIZATION_KEY = "oauth";
public static final String CONFIG_OF_SWAGGER_RESOURCES_PATH = "META-INF/resources/webjars/swagger-ui/5.17.14/";
public static final String SWAGGER_POM_PROPERTIES_PATH = "META-INF/maven/org.webjars/swagger-ui/pom.properties";
public static final String CONFIG_OF_SWAGGER_RESOURCES_PATH = "META-INF/resources/webjars/swagger-ui/";
public static final String CONFIG_OF_TIMEZONE = "pinot.timezone";

public static final String DATABASE = "database";
Expand Down

0 comments on commit e14d887

Please sign in to comment.