Skip to content

Commit

Permalink
Merge pull request #327 from aurobindoc/3.0.x
Browse files Browse the repository at this point in the history
Add servers in SwaggerBundleConfiguration
  • Loading branch information
jplock authored Aug 31, 2023
2 parents b480d0a + 14ca776 commit dbd14e5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,6 +77,8 @@ public class SwaggerBundleConfiguration implements Cloneable {

@Nullable private String licenseUrl;

@Nullable private List<Server> servers;

@Nullable private String customJavascript;

private SwaggerViewConfiguration swaggerViewConfiguration = new SwaggerViewConfiguration();
Expand Down Expand Up @@ -301,6 +306,17 @@ public void setIncludeSwaggerResource(final boolean include) {
this.includeSwaggerResource = include;
}

@Nullable
@JsonProperty
public List<Server> getServers() {
return servers != null ? new ArrayList<>(servers) : null;
}

@JsonProperty
public void setServers(List<Server> servers) {
this.servers = servers != null ? new ArrayList<>(servers) : null;
}

@Nullable
@JsonProperty
public String getCustomJavascript() {
Expand Down Expand Up @@ -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()))
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TestConfiguration> 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\":\"[email protected]\""),
StringContains.containsString("\"url\":\"test-url.contact.com\""))
.when()
.get(Path.from(basePath, "openapi.json"));
}
}
17 changes: 17 additions & 0 deletions src/test/resources/test-default-server.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dbd14e5

Please sign in to comment.