diff --git a/.gitignore b/.gitignore index 0a1624c..1ab1109 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ target *.iws /dep out -.DS_Store \ No newline at end of file +.DS_Store +.vscode \ No newline at end of file diff --git a/dropwizard-swagger-bom/pom.xml b/dropwizard-swagger-bom/pom.xml index c649431..c768efd 100644 --- a/dropwizard-swagger-bom/pom.xml +++ b/dropwizard-swagger-bom/pom.xml @@ -9,7 +9,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> in.vectorpro.dropwizard dropwizard-swagger-parent - 5.0.0-1-rc.1 + 5.0.0-1-rc.2 dropwizard-swagger-bom @@ -73,7 +73,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> in.vectorpro.dropwizard dropwizard-swagger - 5.0.0-1-rc.1 + 5.0.0-1-rc.2 in.vectorpro.dropwizard diff --git a/dropwizard-swagger/pom.xml b/dropwizard-swagger/pom.xml index f6bb940..219c43d 100644 --- a/dropwizard-swagger/pom.xml +++ b/dropwizard-swagger/pom.xml @@ -22,7 +22,7 @@ in.vectorpro.dropwizard dropwizard-swagger-parent - 5.0.0-1-rc.1 + 5.0.0-1-rc.2 dropwizard-swagger diff --git a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerBundle.java b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerBundle.java index c18c5e2..9e4c9f0 100644 --- a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerBundle.java +++ b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerBundle.java @@ -42,8 +42,10 @@ public void initialize(Bootstrap bootstrap) { @Override public void run(T configuration, Environment environment) throws Exception { + final SwaggerBundleConfiguration swaggerBundleConfiguration = getSwaggerBundleConfiguration(configuration); + if (swaggerBundleConfiguration == null) { throw new IllegalStateException( "You need to provide an instance of SwaggerBundleConfiguration"); @@ -55,6 +57,7 @@ public void run(T configuration, Environment environment) throws Exception { final ConfigurationHelper configurationHelper = new ConfigurationHelper(configuration, swaggerBundleConfiguration); + new AssetsBundle( "/swagger-static", configurationHelper.getSwaggerUriPath(), null, "swagger-assets") .run(configuration, environment); @@ -67,20 +70,19 @@ public void run(T configuration, Environment environment) throws Exception { .run(configuration, environment); final SwaggerConfiguration oasConfiguration = swaggerBundleConfiguration.build(); - new JaxrsOpenApiContextBuilder().openApiConfiguration(oasConfiguration).buildContext(true); + new JaxrsOpenApiContextBuilder<>().openApiConfiguration(oasConfiguration).buildContext(true); environment.jersey().register(new OpenApiResource()); environment.jersey().register(new BackwardsCompatibleSwaggerResource()); environment.jersey().register(new SwaggerSerializers()); + if (swaggerBundleConfiguration.isIncludeSwaggerResource()) { environment .jersey() .register( new SwaggerResource( - configurationHelper.getUrlPattern(), swaggerBundleConfiguration.getSwaggerViewConfiguration(), - swaggerBundleConfiguration.getSwaggerOAuth2Configuration(), - swaggerBundleConfiguration.getContextRoot())); + swaggerBundleConfiguration.getSwaggerOAuth2Configuration())); } } diff --git a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerResource.java b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerResource.java index 754cd3e..f076f0e 100644 --- a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerResource.java +++ b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerResource.java @@ -25,35 +25,21 @@ @Path(PATH) @Produces(MediaType.TEXT_HTML) public class SwaggerResource { + static final String PATH = "/swagger"; + private final SwaggerViewConfiguration viewConfiguration; private final SwaggerOAuth2Configuration oAuth2Configuration; - private final String contextRoot; - private final String urlPattern; public SwaggerResource( - String urlPattern, SwaggerViewConfiguration viewConfiguration, SwaggerOAuth2Configuration oAuth2Configuration) { - this.urlPattern = urlPattern; - this.viewConfiguration = viewConfiguration; - this.oAuth2Configuration = oAuth2Configuration; - this.contextRoot = "/"; - } - - public SwaggerResource( - String urlPattern, - SwaggerViewConfiguration viewConfiguration, - SwaggerOAuth2Configuration oAuth2Configuration, - String contextRoot) { this.viewConfiguration = viewConfiguration; this.oAuth2Configuration = oAuth2Configuration; - this.urlPattern = urlPattern; - this.contextRoot = contextRoot; } @GET public SwaggerView get() { - return new SwaggerView(contextRoot, urlPattern, viewConfiguration, oAuth2Configuration); + return new SwaggerView(viewConfiguration, oAuth2Configuration); } } diff --git a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerView.java b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerView.java index b659f69..6918670 100644 --- a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerView.java +++ b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerView.java @@ -17,75 +17,25 @@ import io.dropwizard.views.common.View; import java.nio.charset.StandardCharsets; -import jakarta.annotation.Nullable; /** - * Serves the content of Swagger's index page which has been "templatized" to support replacing the - * directory in which Swagger's static content is located (i.e. JS files) and the path with which - * requests to resources need to be prefixed. + * Serves the content of Swagger's index page which has been "templatized" to support dynamic + * configuration of Swagger UI. The template calculates paths dynamically from the browser URL, + * enabling the application to work behind proxies with different path prefixes. */ public class SwaggerView extends View { - private static final String SWAGGER_URI_PATH = "/swagger-static"; - - private final String swaggerAssetsPath; - private final String contextPath; - private final SwaggerViewConfiguration viewConfiguration; private final SwaggerOAuth2Configuration oauth2Configuration; public SwaggerView( - final String contextRoot, - final String urlPattern, final SwaggerViewConfiguration viewConfiguration, final SwaggerOAuth2Configuration oauth2Configuration) { super(viewConfiguration.getTemplateUrl(), StandardCharsets.UTF_8); - - String contextRootPrefix = "/".equals(contextRoot) ? "" : contextRoot; - - // swagger-static should be found on the root context - if (!contextRootPrefix.isEmpty()) { - swaggerAssetsPath = contextRootPrefix + SWAGGER_URI_PATH; - } else { - swaggerAssetsPath = - (urlPattern.equals("/") ? SWAGGER_URI_PATH : (urlPattern + SWAGGER_URI_PATH)); - } - - contextPath = urlPattern.equals("/") ? contextRootPrefix : (contextRootPrefix + urlPattern); - this.viewConfiguration = viewConfiguration; this.oauth2Configuration = oauth2Configuration; } - /** - * Returns the title for the browser header - * - * @return String - */ - @Nullable - public String getTitle() { - return viewConfiguration.getPageTitle(); - } - - /** - * Returns the path with which all requests for Swagger's static content need to be prefixed - * - * @return String - */ - public String getSwaggerAssetsPath() { - return swaggerAssetsPath; - } - - /** - * Returns the path with with which all requests made by Swagger's UI to Resources need to be - * prefixed - * - * @return String - */ - public String getContextPath() { - return contextPath; - } - /** @return {@link SwaggerViewConfiguration} containing every properties to customize swagger */ public SwaggerViewConfiguration getViewConfiguration() { return viewConfiguration; diff --git a/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl b/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl index 3957991..842839e 100644 --- a/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl +++ b/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl @@ -1,3 +1,20 @@ +<#-- + + Copyright © 2021 Vector Pro (teamvectorpro@googlegroups.com) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> <#-- @ftlvariable name="" type="SwaggerView" --> @@ -5,20 +22,51 @@ ${viewConfiguration.pageTitle} - - - - +
- - - + - - - - - - - - - - - - - + ${viewConfiguration.pageTitle} + - - - + + + +