From 8c8802d57cd0d082a8090d37654fab332d164feb Mon Sep 17 00:00:00 2001 From: dbulawa Date: Tue, 14 Sep 2021 16:18:47 +0200 Subject: [PATCH] feat(*): add support for generating more hosts Add support for generating more hosts to OpenAPI 3.0 specification with backwards compatibility. --- .../restdocs/spec/ApiDetails.java | 15 ++++++++++----- .../OpenApi30SpecificationGenerator.java | 7 +++++-- .../PostmanCollectionSpecificationGenerator.java | 2 +- .../OpenApi30SpecificationGeneratorTest.java | 4 +++- ...stmanCollectionSpecificationGeneratorTest.java | 4 +++- .../restdocs/mojo/AbstractGenerateMojo.java | 11 +++++++++-- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/ApiDetails.java b/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/ApiDetails.java index 9593eb8..5c15a12 100644 --- a/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/ApiDetails.java +++ b/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/ApiDetails.java @@ -3,6 +3,7 @@ import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.Set; public class ApiDetails { @@ -15,7 +16,7 @@ public class ApiDetails { private String name; private String version; private String description; - private String host; + private Set hosts; private String basePath; private List schemes; private SpecificationFormat format; @@ -27,7 +28,7 @@ public class ApiDetails { public ApiDetails() { this.name = DEFAULT_NAME; this.version = DEFAULT_VERSION; - this.host = DEFAULT_HOST; + this.hosts = Collections.singleton(DEFAULT_HOST); this.basePath = null; this.schemes = DEFAULT_SCHEMES; this.format = DEFAULT_FORMAT; @@ -63,12 +64,16 @@ public ApiDetails description(String description){ return this; } + public Set getHosts() { + return hosts; + } + public String getHost() { - return host; + return getHosts().stream().findFirst().orElseThrow(); } - public ApiDetails host(String host) { - this.host = host != null ? host : DEFAULT_HOST; + public ApiDetails hosts(Set hosts) { + this.hosts = (hosts != null && !hosts.isEmpty()) ? hosts : Collections.singleton(DEFAULT_HOST); return this; } diff --git a/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/generator/openapi_v3/OpenApi30SpecificationGenerator.java b/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/generator/openapi_v3/OpenApi30SpecificationGenerator.java index 5e9d4ce..237c37f 100644 --- a/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/generator/openapi_v3/OpenApi30SpecificationGenerator.java +++ b/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/generator/openapi_v3/OpenApi30SpecificationGenerator.java @@ -51,10 +51,13 @@ public String generate(ApiDetails details, List models) throws Sp private List createServerList(ApiDetails details) throws SpecificationGeneratorException { List servers = new ArrayList<>(); + for (String scheme : details.getSchemes()) { try { - URL url = SpecificationGeneratorUtils.createBaseUrl(scheme, details.getHost(), details.getBasePath() == null ? "" : details.getBasePath()); - servers.add(new Server().url(url.toString())); + for (String host : details.getHosts()) { + URL url = SpecificationGeneratorUtils.createBaseUrl(scheme, host, details.getBasePath() == null ? "" : details.getBasePath()); + servers.add(new Server().url(url.toString())); + } } catch (MalformedURLException e) { throw new SpecificationGeneratorException("Unable to build server url.", e); } diff --git a/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/generator/postman/PostmanCollectionSpecificationGenerator.java b/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/generator/postman/PostmanCollectionSpecificationGenerator.java index e28297d..d0dd156 100644 --- a/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/generator/postman/PostmanCollectionSpecificationGenerator.java +++ b/restdocs-spec-generator/src/main/java/com/berkleytechnologyservices/restdocs/spec/generator/postman/PostmanCollectionSpecificationGenerator.java @@ -61,7 +61,7 @@ private String createBaseUrl(ApiDetails details) throws SpecificationGeneratorEx } try { - return SpecificationGeneratorUtils.createBaseUrl(details.getSchemes().get(0), details.getHost(), details.getBasePath()).toString(); + return SpecificationGeneratorUtils.createBaseUrl(details.getSchemes().get(0), details.getHosts().iterator().next(), details.getBasePath()).toString(); } catch (MalformedURLException e) { throw new SpecificationGeneratorException("Unable to build base url.", e); } diff --git a/restdocs-spec-generator/src/test/java/com/berkleytechnologyservices/restdocs/spec/generator/openapi_v3/OpenApi30SpecificationGeneratorTest.java b/restdocs-spec-generator/src/test/java/com/berkleytechnologyservices/restdocs/spec/generator/openapi_v3/OpenApi30SpecificationGeneratorTest.java index c1eac7c..c2858e0 100644 --- a/restdocs-spec-generator/src/test/java/com/berkleytechnologyservices/restdocs/spec/generator/openapi_v3/OpenApi30SpecificationGeneratorTest.java +++ b/restdocs-spec-generator/src/test/java/com/berkleytechnologyservices/restdocs/spec/generator/openapi_v3/OpenApi30SpecificationGeneratorTest.java @@ -11,6 +11,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; + import static com.berkleytechnologyservices.restdocs.spec.generator.test.ResourceModels.*; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.contentOf; @@ -42,7 +44,7 @@ public void testGenerateWithDefaults() throws SpecificationGeneratorException { @Test public void testGenerateHostWithPort() throws SpecificationGeneratorException { - ApiDetails apiDetails = new ApiDetails().host("example.com:8080"); + ApiDetails apiDetails = new ApiDetails().hosts(Collections.singleton("example.com:8080")); String rawOutput = generator.generate(apiDetails, list(getMockResource())); diff --git a/restdocs-spec-generator/src/test/java/com/berkleytechnologyservices/restdocs/spec/generator/postman/PostmanCollectionSpecificationGeneratorTest.java b/restdocs-spec-generator/src/test/java/com/berkleytechnologyservices/restdocs/spec/generator/postman/PostmanCollectionSpecificationGeneratorTest.java index c8b418e..770a08f 100644 --- a/restdocs-spec-generator/src/test/java/com/berkleytechnologyservices/restdocs/spec/generator/postman/PostmanCollectionSpecificationGeneratorTest.java +++ b/restdocs-spec-generator/src/test/java/com/berkleytechnologyservices/restdocs/spec/generator/postman/PostmanCollectionSpecificationGeneratorTest.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; + import static com.berkleytechnologyservices.restdocs.spec.generator.test.ResourceModels.field; import static com.berkleytechnologyservices.restdocs.spec.generator.test.ResourceModels.request; import static com.berkleytechnologyservices.restdocs.spec.generator.test.ResourceModels.requiredParam; @@ -44,7 +46,7 @@ public void testGenerateWithDefaults() throws SpecificationGeneratorException { @Test public void testGenerateHostWithPort() throws SpecificationGeneratorException { - ApiDetails apiDetails = new ApiDetails().host("example.com:8080"); + ApiDetails apiDetails = new ApiDetails().hosts(Collections.singleton("example.com:8080")); String rawOutput = generator.generate(apiDetails, list(getMockResource())); diff --git a/restdocs-spec-maven-plugin/src/main/java/com/berkleytechnologyservices/restdocs/mojo/AbstractGenerateMojo.java b/restdocs-spec-maven-plugin/src/main/java/com/berkleytechnologyservices/restdocs/mojo/AbstractGenerateMojo.java index ed341cd..b02c8b5 100644 --- a/restdocs-spec-maven-plugin/src/main/java/com/berkleytechnologyservices/restdocs/mojo/AbstractGenerateMojo.java +++ b/restdocs-spec-maven-plugin/src/main/java/com/berkleytechnologyservices/restdocs/mojo/AbstractGenerateMojo.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; /** @@ -48,10 +49,16 @@ public abstract class AbstractGenerateMojo extends AbstractMojo { @Parameter(defaultValue = "${project.description}", required = true) private String description; + /** + * More hosts to specify (OpenAPI 3.0) + */ + @Parameter + private Set hosts; + /** * Host */ - @Parameter(defaultValue = "localhost", required = true) + @Parameter(defaultValue = "localhost") private String host; /** @@ -207,7 +214,7 @@ private ApiDetails createApiDetails(SpecificationOptions options) { .name(name) .version(version) .description(description) - .host(host) + .hosts((hosts == null || hosts.isEmpty()) ? Collections.singleton(host) : hosts) .basePath(basePath) .schemes(schemes) .format(options.getFormat())