Java Code Examples for io.swagger.v3.oas.models.servers.Server#getUrl()

The following examples show how to use io.swagger.v3.oas.models.servers.Server#getUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenAPICodegenUtils.java    From product-microgateway with Apache License 2.0 6 votes vote down vote up
/**
 * Open API server object can have server templating. Ex: https://{customerId}.saas-app.com:{port}/v2.
 * When adding the back end url this method will replace the template values with the default value.
 *
 * @param server  {@link Server} object of the open API definition
 * @return templated server url replaced with default values
 */
private static String replaceOpenAPIServerTemplate(Server server) {
    //server url templating can have urls similar to 'https://{customerId}.saas-app.com:{port}/v2'
    String url = server.getUrl();
    Pattern serverTemplate = Pattern.compile("\\{.*?}");
    Matcher matcher = serverTemplate.matcher(url);
    while (matcher.find()) {
        if (server.getVariables() != null && server.getVariables()
                .containsKey(matcher.group(0).substring(1, matcher.group(0).length() - 1))) {
            String defaultValue = server.getVariables()
                    .get(matcher.group(0).substring(1, matcher.group(0).length() - 1)).getDefault();
            url = url.replaceAll("\\" + matcher.group(0), defaultValue);
        } else {
            outStream.println(
                    CliConstants.WARN_LOG_PATTERN + "Open API server url templating is used for the url : " + url
                            + ". But default values is not provided for the variable '" + matcher.group(0)
                            + "'. Hence correct url will not be resolved during the runtime "
                            + "unless url is overridden during the runtime");
        }
    }
    return url;
}
 
Example 2
Source File: OAIToAPIConverter.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
private List<String> mapServersToEndpoint(List<Server> servers) {
    List<String> endpoints = new ArrayList<>();
    for (Server server : servers) {
        ServerVariables serverVariables = server.getVariables();
        String serverUrl = server.getUrl();
        if (CollectionUtils.isEmpty(serverVariables)) {
            endpoints.add(serverUrl);
        } else {
            List<String> evaluatedUrls = Collections.singletonList(serverUrl);
            for (Map.Entry<String, ServerVariable> serverVar : serverVariables.entrySet()) {
                evaluatedUrls = evaluateServerUrlsForOneVar(serverVar.getKey(), serverVar.getValue(),
                        evaluatedUrls);
            }
            endpoints.addAll(evaluatedUrls);
        }
    }
    return endpoints;
}
 
Example 3
Source File: SwaggerConverter.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
static List<UriBuilder> createUriBuilders(
        List<Server> servers, UriBuilder definitionUriBuilder) {
    List<UriBuilder> urls = new ArrayList<>();
    for (Server server : servers) {
        String url = server.getUrl();
        if (server.getVariables() != null) {
            for (Map.Entry<String, ServerVariable> entry : server.getVariables().entrySet()) {
                // default is always set
                url = url.replace("{" + entry.getKey() + "}", entry.getValue().getDefault());
            }
        }
        try {
            UriBuilder uriBuilder = UriBuilder.parse(url);
            if (!hasSupportedScheme(uriBuilder)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(
                            "Ignoring server URL "
                                    + url
                                    + " because of unsupported scheme: "
                                    + uriBuilder.getScheme());
                }
                continue;
            }
            if (!uriBuilder.isEmpty()) {
                uriBuilder.withDefaultPath("");
            }

            urls.add(uriBuilder.merge(definitionUriBuilder));
        } catch (IllegalArgumentException e) {
            LOG.warn("Failed to create server URL from: " + url, e);
        }
    }
    return urls;
}