Skip to content

Commit

Permalink
feat(*): add support for generating more hosts
Browse files Browse the repository at this point in the history
Add support for generating more hosts to OpenAPI 3.0 specification
with backwards compatibility.
  • Loading branch information
dbulawaGordic authored and klieber committed Oct 9, 2023
1 parent 6dfe9cc commit 8c8802d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public class ApiDetails {

Expand All @@ -15,7 +16,7 @@ public class ApiDetails {
private String name;
private String version;
private String description;
private String host;
private Set<String> hosts;
private String basePath;
private List<String> schemes;
private SpecificationFormat format;
Expand All @@ -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;
Expand Down Expand Up @@ -63,12 +64,16 @@ public ApiDetails description(String description){
return this;
}

public Set<String> 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<String> hosts) {
this.hosts = (hosts != null && !hosts.isEmpty()) ? hosts : Collections.singleton(DEFAULT_HOST);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ public String generate(ApiDetails details, List<ResourceModel> models) throws Sp

private List<Server> createServerList(ApiDetails details) throws SpecificationGeneratorException {
List<Server> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<String> hosts;

/**
* Host
*/
@Parameter(defaultValue = "localhost", required = true)
@Parameter(defaultValue = "localhost")
private String host;

/**
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 8c8802d

Please sign in to comment.