From 14ca776c190ea47df46cec3d02f0069df73deb00 Mon Sep 17 00:00:00 2001 From: aurobindoc Date: Thu, 31 Aug 2023 13:15:48 +0530 Subject: [PATCH] Added servers in swagger configuration --- .../swagger/SwaggerBundleConfiguration.java | 18 ++++++- .../DefaultServerWithServerConfigTest.java | 51 +++++++++++++++++++ src/test/resources/test-default-server.yaml | 17 +++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/federecio/dropwizard/swagger/DefaultServerWithServerConfigTest.java create mode 100644 src/test/resources/test-default-server.yaml diff --git a/src/main/java/io/federecio/dropwizard/swagger/SwaggerBundleConfiguration.java b/src/main/java/io/federecio/dropwizard/swagger/SwaggerBundleConfiguration.java index 2d7fc4b..1d6d840 100644 --- a/src/main/java/io/federecio/dropwizard/swagger/SwaggerBundleConfiguration.java +++ b/src/main/java/io/federecio/dropwizard/swagger/SwaggerBundleConfiguration.java @@ -36,7 +36,10 @@ import io.swagger.v3.oas.models.info.Contact; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.servers.Server; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.stream.Collectors; import javax.validation.constraints.NotEmpty; import org.checkerframework.checker.nullness.qual.Nullable; @@ -74,6 +77,8 @@ public class SwaggerBundleConfiguration implements Cloneable { @Nullable private String licenseUrl; + @Nullable private List servers; + @Nullable private String customJavascript; private SwaggerViewConfiguration swaggerViewConfiguration = new SwaggerViewConfiguration(); @@ -301,6 +306,17 @@ public void setIncludeSwaggerResource(final boolean include) { this.includeSwaggerResource = include; } + @Nullable + @JsonProperty + public List getServers() { + return servers != null ? new ArrayList<>(servers) : null; + } + + @JsonProperty + public void setServers(List servers) { + this.servers = servers != null ? new ArrayList<>(servers) : null; + } + @Nullable @JsonProperty public String getCustomJavascript() { @@ -342,7 +358,7 @@ public SwaggerConfiguration build() { final String[] exclusions = {SwaggerResource.PATH}; return new SwaggerConfiguration() - .openAPI(oas.info(info)) + .openAPI(oas.info(info).servers(servers)) .prettyPrint(prettyPrint) .readAllResources(readAllResources) .ignoredRoutes(Arrays.stream(exclusions).collect(Collectors.toSet())) diff --git a/src/test/java/io/federecio/dropwizard/swagger/DefaultServerWithServerConfigTest.java b/src/test/java/io/federecio/dropwizard/swagger/DefaultServerWithServerConfigTest.java new file mode 100644 index 0000000..56d437f --- /dev/null +++ b/src/test/java/io/federecio/dropwizard/swagger/DefaultServerWithServerConfigTest.java @@ -0,0 +1,51 @@ +/* + * Copyright © 2014 Federico Recio (N/A) + * + * 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. + */ +package io.federecio.dropwizard.swagger; + +import io.dropwizard.testing.ResourceHelpers; +import io.dropwizard.testing.junit5.DropwizardAppExtension; +import io.dropwizard.testing.junit5.DropwizardExtensionsSupport; +import io.restassured.RestAssured; +import org.eclipse.jetty.http.HttpStatus; +import org.hamcrest.core.StringContains; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +@ExtendWith(DropwizardExtensionsSupport.class) +public class DefaultServerWithServerConfigTest extends DropwizardTest { + + private static DropwizardAppExtension RULE = + new DropwizardAppExtension<>( + TestApplication.class, ResourceHelpers.resourceFilePath("test-default-server.yaml")); + + public DefaultServerWithServerConfigTest() { + super(RULE.getLocalPort(), "/"); + } + + @Test + @Disabled("passes when ran standalone, but fails inside the suite") + public void swaggerHasContactInfo() throws Exception { + RestAssured.expect() + .statusCode(HttpStatus.OK_200) + .body( + StringContains.containsString("\"name\":\"test-contact-info\""), + StringContains.containsString("\"email\":\"test-contact-email@test.com\""), + StringContains.containsString("\"url\":\"test-url.contact.com\"")) + .when() + .get(Path.from(basePath, "openapi.json")); + } +} diff --git a/src/test/resources/test-default-server.yaml b/src/test/resources/test-default-server.yaml new file mode 100644 index 0000000..a1d811c --- /dev/null +++ b/src/test/resources/test-default-server.yaml @@ -0,0 +1,17 @@ +server: + type: default + applicationConnectors: + - type: http + port: 0 + adminConnectors: + - type: http + port: 0 +swagger: + resourcePackage: io.federecio.dropwizard.swagger + servers: + - url: https://development.example.com/ + description: Development server + - url: https://staging.example.com/v1 + description: Staging server + - url: https://api.example.com/v1 + description: Production server