Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.google.common.collect.ImmutableMap;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.apache.commons.lang3.ObjectUtils;
import org.openapitools.codegen.auth.AuthParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -17,6 +19,7 @@
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -58,6 +61,7 @@ public String buildMergedSpec() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
List<SpecWithPaths> allPaths = new ArrayList<>();
List<Server> allServers = new ArrayList<>();

for (String specRelatedPath : specRelatedPaths) {
String specPath = inputSpecRootDirectory + File.separator + specRelatedPath;
Expand All @@ -74,13 +78,14 @@ public String buildMergedSpec() {
isJson = true;
}
}
allServers.addAll(ObjectUtils.defaultIfNull(result.getServers(), Collections.emptyList()));
allPaths.add(new SpecWithPaths(specRelatedPath, result.getPaths().keySet()));
} catch (Exception e) {
LOGGER.error("Failed to read file: {}. It would be ignored", specPath);
}
}

Map<String, Object> mergedSpec = generatedMergedSpec(openapiVersion, allPaths);
Map<String, Object> mergedSpec = generatedMergedSpec(openapiVersion, allPaths, allServers);
String mergedFilename = this.mergeFileName + (isJson ? ".json" : ".yaml");
Path mergedFilePath = Paths.get(inputSpecRootDirectory, mergedFilename);

Expand All @@ -94,8 +99,8 @@ public String buildMergedSpec() {
return mergedFilePath.toString();
}

private Map<String, Object> generatedMergedSpec(String openapiVersion, List<SpecWithPaths> allPaths) {
Map<String, Object> spec = generateHeader(openapiVersion, mergedFileInfoName, mergedFileInfoDescription, mergedFileInfoVersion);
private Map<String, Object> generatedMergedSpec(String openapiVersion, List<SpecWithPaths> allPaths, List<Server> allServers) {
Map<String, Object> spec = generateHeader(openapiVersion, mergedFileInfoName, mergedFileInfoDescription, mergedFileInfoVersion, allServers);
Map<String, Object> paths = new HashMap<>();
spec.put("paths", paths);

Expand All @@ -111,17 +116,25 @@ private Map<String, Object> generatedMergedSpec(String openapiVersion, List<Spec
return spec;
}

private static Map<String, Object> generateHeader(String openapiVersion, String title, String description, String version) {
private static Map<String, Object> generateHeader(String openapiVersion, String title, String description, String version, List<Server> allServers) {
Map<String, Object> map = new HashMap<>();
map.put("openapi", openapiVersion);
map.put("info", ImmutableMap.of(
"title", title,
"description", description,
"version", version
));
map.put("servers", Collections.singleton(
ImmutableMap.of("url", "http://localhost:8080")
));

Set<ImmutableMap<String, String>> servers = allServers.stream()
.map(Server::getUrl)
.distinct()
.map(url -> ImmutableMap.of("url", url))
.collect(Collectors.collectingAndThen(Collectors.toSet(), Optional::of))
.filter(Predicate.not(Set::isEmpty))
.orElseGet(() -> Collections.singleton(ImmutableMap.of("url", "http://localhost:8080")));

map.put("servers", servers);

return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.openapitools.codegen.languages.SpringCodegen.*;

public class MergedSpecBuilderTest {

@Test
Expand Down Expand Up @@ -55,6 +57,7 @@ private void assertFilesFromMergedSpec(String mergedSpec) throws IOException {
.readLocation(mergedSpec, null, parseOptions).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.additionalProperties().put(REQUEST_MAPPING_OPTION, "api_interface");
codegen.setOutputDir(output.getAbsolutePath());

ClientOptInput input = new ClientOptInput();
Expand All @@ -78,10 +81,18 @@ private void assertFilesFromMergedSpec(String mergedSpec) throws IOException {
.assertParameter("param1")
.hasType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("PathVariable", ImmutableMap.of("value", "\"param1\""));
.containsWithNameAndAttributes("PathVariable", ImmutableMap.of("value", "\"param1\""))
.toParameter()
.toMethod()
.toFileAssert()
.fileContains("@RequestMapping(\"${openapi.mergedSpec.base-path:/my-context-root/v1}\")")
;

JavaFileAssert.assertThat(files.get("Spec2Api.java"))
.assertMethod("spec2Operation").hasReturnType("ResponseEntity<Spec2Model>");
.assertMethod("spec2Operation").hasReturnType("ResponseEntity<Spec2Model>")
.toFileAssert()
.fileContains("@RequestMapping(\"${openapi.mergedSpec.base-path:/my-context-root/v1}\")")
;

JavaFileAssert.assertThat(files.get("Spec1Model.java"))
.assertMethod("getSpec1Field").hasReturnType("String");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"description": "Specification to reproduce nullable issue with Array",
"title": "ArrayNullableTest Api"
},
"servers": [
{
"url": "api.my-domain.com/my-context-root/v1"
},
{
"url": "hom-api.my-domain.com/my-context-root/v1"
}
],
"paths": {
"/spec1": {
"get": {
Expand Down Expand Up @@ -71,4 +79,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ info:
version: 1.0.0
description: Specification to reproduce nullable issue with Array
title: ArrayNullableTest Api
servers:
- url: api.my-domain.com/my-context-root/v1
- url: hom-api.my-domain.com/my-context-root/v1
paths:
/spec1:
get:
Expand Down Expand Up @@ -43,4 +46,4 @@ components:
type: object
properties:
spec1Field:
type: string
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"description": "Specification to reproduce nullable issue with Array",
"title": "ArrayNullableTest Api"
},
"servers": [
{
"url": "api.my-domain.com/my-context-root/v1"
},
{
"url": "hom-api.my-domain.com/my-context-root/v1"
}
],
"paths": {
"/spec2": {
"get": {
Expand Down Expand Up @@ -40,4 +48,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ info:
version: 1.0.0
description: Specification to reproduce nullable issue with Array
title: ArrayNullableTest Api
servers:
- url: api.my-domain.com/my-context-root/v1
- url: hom-api.my-domain.com/my-context-root/v1
paths:
/spec2:
get:
Expand All @@ -24,4 +27,4 @@ components:
type: object
properties:
spec2Field:
type: number
type: number
Loading