|
14 | 14 | import java.util.List; |
15 | 15 | import java.util.Map; |
16 | 16 | import java.util.Optional; |
| 17 | +import java.util.regex.Matcher; |
| 18 | +import java.util.regex.Pattern; |
17 | 19 |
|
18 | 20 | import javax.sql.DataSource; |
19 | 21 |
|
|
50 | 52 | import io.swagger.v3.oas.models.OpenAPI; |
51 | 53 | import io.swagger.v3.oas.models.PathItem.HttpMethod; |
52 | 54 | import io.swagger.v3.oas.models.parameters.RequestBody; |
| 55 | +import io.swagger.v3.oas.models.servers.Server; |
| 56 | +import io.swagger.v3.oas.models.servers.ServerVariable; |
53 | 57 | import io.swagger.v3.parser.core.models.ParseOptions; |
54 | 58 | import io.swagger.v3.parser.core.models.SwaggerParseResult; |
55 | 59 |
|
@@ -263,12 +267,62 @@ private String extractBaseUrl(String fullUrl) { |
263 | 267 | } |
264 | 268 | } |
265 | 269 |
|
| 270 | + private String resolveBaseUrl(ServiceExtensionContext context, OpenAPI openAPI, String openapiUrl) { |
| 271 | + String configuredBaseUrl = context.getSetting(API_BASE_URL, null); |
| 272 | + if (configuredBaseUrl != null && !configuredBaseUrl.isBlank()) { |
| 273 | + return configuredBaseUrl; |
| 274 | + } |
| 275 | + |
| 276 | + String serverUrl = resolveOpenApiServerUrl(openAPI); |
| 277 | + if (serverUrl == null || serverUrl.isBlank()) { |
| 278 | + return extractBaseUrl(openapiUrl); |
| 279 | + } |
| 280 | + |
| 281 | + if (serverUrl.startsWith("/")) { |
| 282 | + return extractBaseUrl(openapiUrl) + serverUrl; |
| 283 | + } |
| 284 | + |
| 285 | + return serverUrl; |
| 286 | + } |
| 287 | + |
| 288 | + private String resolveOpenApiServerUrl(OpenAPI openAPI) { |
| 289 | + if (openAPI == null || openAPI.getServers() == null || openAPI.getServers().isEmpty()) { |
| 290 | + return null; |
| 291 | + } |
| 292 | + |
| 293 | + Server server = openAPI.getServers().get(0); |
| 294 | + String url = server.getUrl(); |
| 295 | + if (url == null || url.isBlank()) { |
| 296 | + return null; |
| 297 | + } |
| 298 | + |
| 299 | + Map<String, ServerVariable> variables = server.getVariables(); |
| 300 | + if (variables == null || variables.isEmpty()) { |
| 301 | + return url; |
| 302 | + } |
| 303 | + |
| 304 | + Pattern pattern = Pattern.compile("\\{([^}]+)\\}"); |
| 305 | + Matcher matcher = pattern.matcher(url); |
| 306 | + StringBuffer resolved = new StringBuffer(); |
| 307 | + while (matcher.find()) { |
| 308 | + String varName = matcher.group(1); |
| 309 | + ServerVariable variable = variables.get(varName); |
| 310 | + String replacement = variable != null ? variable.getDefault() : null; |
| 311 | + if (replacement == null) { |
| 312 | + replacement = ""; |
| 313 | + } |
| 314 | + matcher.appendReplacement(resolved, Matcher.quoteReplacement(replacement)); |
| 315 | + } |
| 316 | + matcher.appendTail(resolved); |
| 317 | + return resolved.toString(); |
| 318 | + } |
| 319 | + |
266 | 320 | @SuppressWarnings("unchecked") |
267 | 321 | private void createAssets(ServiceExtensionContext context) { |
268 | 322 | Monitor monitor = context.getMonitor(); |
269 | 323 | Slugify slg = Slugify.builder().lowerCase(false).build(); |
270 | 324 | OpenAPI openAPI = readOpenAPISchema(context.getMonitor()); |
271 | | - String baseUrl = context.getSetting(API_BASE_URL, extractBaseUrl(openapiUrl)); |
| 325 | + String baseUrl = resolveBaseUrl(context, openAPI, openapiUrl); |
272 | 326 | boolean forceHttpDataFixed = context.getSetting(OPENAPI_FORCE_HTTPDATAFIXED, "false") |
273 | 327 | .equals("true"); |
274 | 328 |
|
|
0 commit comments