Skip to content

Commit c30fb41

Browse files
rodrigoma3Rodrigo Maciel de Almeida
andauthored
issue 21965 - [REQ] MergedSpecBuilder load servers urls from input specs (#21966)
Co-authored-by: Rodrigo Maciel de Almeida <[email protected]>
1 parent 6b69273 commit c30fb41

File tree

6 files changed

+59
-13
lines changed

6 files changed

+59
-13
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/config/MergedSpecBuilder.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.google.common.collect.ImmutableMap;
66
import io.swagger.parser.OpenAPIParser;
77
import io.swagger.v3.oas.models.OpenAPI;
8+
import io.swagger.v3.oas.models.servers.Server;
89
import io.swagger.v3.parser.core.models.ParseOptions;
10+
import org.apache.commons.lang3.ObjectUtils;
911
import org.openapitools.codegen.auth.AuthParser;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
@@ -17,6 +19,7 @@
1719
import java.nio.file.Paths;
1820
import java.nio.file.StandardOpenOption;
1921
import java.util.*;
22+
import java.util.function.Predicate;
2023
import java.util.stream.Collectors;
2124
import java.util.stream.Stream;
2225

@@ -58,6 +61,7 @@ public String buildMergedSpec() {
5861
ParseOptions options = new ParseOptions();
5962
options.setResolve(true);
6063
List<SpecWithPaths> allPaths = new ArrayList<>();
64+
List<Server> allServers = new ArrayList<>();
6165

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

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

@@ -94,8 +99,8 @@ public String buildMergedSpec() {
9499
return mergedFilePath.toString();
95100
}
96101

97-
private Map<String, Object> generatedMergedSpec(String openapiVersion, List<SpecWithPaths> allPaths) {
98-
Map<String, Object> spec = generateHeader(openapiVersion, mergedFileInfoName, mergedFileInfoDescription, mergedFileInfoVersion);
102+
private Map<String, Object> generatedMergedSpec(String openapiVersion, List<SpecWithPaths> allPaths, List<Server> allServers) {
103+
Map<String, Object> spec = generateHeader(openapiVersion, mergedFileInfoName, mergedFileInfoDescription, mergedFileInfoVersion, allServers);
99104
Map<String, Object> paths = new HashMap<>();
100105
spec.put("paths", paths);
101106

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

114-
private static Map<String, Object> generateHeader(String openapiVersion, String title, String description, String version) {
119+
private static Map<String, Object> generateHeader(String openapiVersion, String title, String description, String version, List<Server> allServers) {
115120
Map<String, Object> map = new HashMap<>();
116121
map.put("openapi", openapiVersion);
117122
map.put("info", ImmutableMap.of(
118123
"title", title,
119124
"description", description,
120125
"version", version
121126
));
122-
map.put("servers", Collections.singleton(
123-
ImmutableMap.of("url", "http://localhost:8080")
124-
));
127+
128+
Set<ImmutableMap<String, String>> servers = allServers.stream()
129+
.map(Server::getUrl)
130+
.distinct()
131+
.map(url -> ImmutableMap.of("url", url))
132+
.collect(Collectors.collectingAndThen(Collectors.toSet(), Optional::of))
133+
.filter(Predicate.not(Set::isEmpty))
134+
.orElseGet(() -> Collections.singleton(ImmutableMap.of("url", "http://localhost:8080")));
135+
136+
map.put("servers", servers);
137+
125138
return map;
126139
}
127140

modules/openapi-generator/src/test/java/org/openapitools/codegen/config/MergedSpecBuilderTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.function.Function;
1919
import java.util.stream.Collectors;
2020

21+
import static org.openapitools.codegen.languages.SpringCodegen.*;
22+
2123
public class MergedSpecBuilderTest {
2224

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

5759
SpringCodegen codegen = new SpringCodegen();
60+
codegen.additionalProperties().put(REQUEST_MAPPING_OPTION, "api_interface");
5861
codegen.setOutputDir(output.getAbsolutePath());
5962

6063
ClientOptInput input = new ClientOptInput();
@@ -78,10 +81,18 @@ private void assertFilesFromMergedSpec(String mergedSpec) throws IOException {
7881
.assertParameter("param1")
7982
.hasType("String")
8083
.assertParameterAnnotations()
81-
.containsWithNameAndAttributes("PathVariable", ImmutableMap.of("value", "\"param1\""));
84+
.containsWithNameAndAttributes("PathVariable", ImmutableMap.of("value", "\"param1\""))
85+
.toParameter()
86+
.toMethod()
87+
.toFileAssert()
88+
.fileContains("@RequestMapping(\"${openapi.mergedSpec.base-path:/my-context-root/v1}\")")
89+
;
8290

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

8697
JavaFileAssert.assertThat(files.get("Spec1Model.java"))
8798
.assertMethod("getSpec1Field").hasReturnType("String");

modules/openapi-generator/src/test/resources/bugs/mergerTest/spec1.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
"description": "Specification to reproduce nullable issue with Array",
66
"title": "ArrayNullableTest Api"
77
},
8+
"servers": [
9+
{
10+
"url": "api.my-domain.com/my-context-root/v1"
11+
},
12+
{
13+
"url": "hom-api.my-domain.com/my-context-root/v1"
14+
}
15+
],
816
"paths": {
917
"/spec1": {
1018
"get": {
@@ -71,4 +79,4 @@
7179
}
7280
}
7381
}
74-
}
82+
}

modules/openapi-generator/src/test/resources/bugs/mergerTest/spec1.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ info:
33
version: 1.0.0
44
description: Specification to reproduce nullable issue with Array
55
title: ArrayNullableTest Api
6+
servers:
7+
- url: api.my-domain.com/my-context-root/v1
8+
- url: hom-api.my-domain.com/my-context-root/v1
69
paths:
710
/spec1:
811
get:
@@ -43,4 +46,4 @@ components:
4346
type: object
4447
properties:
4548
spec1Field:
46-
type: string
49+
type: string

modules/openapi-generator/src/test/resources/bugs/mergerTest/spec2.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
"description": "Specification to reproduce nullable issue with Array",
66
"title": "ArrayNullableTest Api"
77
},
8+
"servers": [
9+
{
10+
"url": "api.my-domain.com/my-context-root/v1"
11+
},
12+
{
13+
"url": "hom-api.my-domain.com/my-context-root/v1"
14+
}
15+
],
816
"paths": {
917
"/spec2": {
1018
"get": {
@@ -40,4 +48,4 @@
4048
}
4149
}
4250
}
43-
}
51+
}

modules/openapi-generator/src/test/resources/bugs/mergerTest/spec2.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ info:
33
version: 1.0.0
44
description: Specification to reproduce nullable issue with Array
55
title: ArrayNullableTest Api
6+
servers:
7+
- url: api.my-domain.com/my-context-root/v1
8+
- url: hom-api.my-domain.com/my-context-root/v1
69
paths:
710
/spec2:
811
get:
@@ -24,4 +27,4 @@ components:
2427
type: object
2528
properties:
2629
spec2Field:
27-
type: number
30+
type: number

0 commit comments

Comments
 (0)