Java Code Examples for io.swagger.v3.oas.models.OpenAPI#getServers()

The following examples show how to use io.swagger.v3.oas.models.OpenAPI#getServers() . 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: URLPathUtils.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public static URL getServerURL(OpenAPI openAPI, Map<String, String> userDefinedVariables) {
    final List<Server> servers = openAPI.getServers();
    if (servers == null || servers.isEmpty()) {
        once(LOGGER).warn("Server information seems not defined in the spec. Default to {}.", LOCAL_HOST);
        return getDefaultUrl();
    }
    // TODO need a way to obtain all server URLs
    return getServerURL(servers.get(0), userDefinedVariables);
}
 
Example 2
Source File: URLPathUtils.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Return the first complete URL from the OpenAPI specification
 *
 * @param openAPI current OpenAPI specification
 * @param userDefinedVariables User overrides for server variable templating
 * @return host
 */
public static String getHost(OpenAPI openAPI, final Map<String, String> userDefinedVariables) {
    if (openAPI.getServers() != null && openAPI.getServers().size() > 0) {
        URL url = getServerURL(openAPI.getServers().get(0), userDefinedVariables);
        return url != null ? sanitizeUrl(url.toString()) : "";
    }
    return LOCAL_HOST;
}
 
Example 3
Source File: OpenAPISerializer.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(OpenAPI value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    if (value != null) {
        gen.writeStartObject();
        gen.writeStringField("openapi", value.getOpenapi());
        if(value.getInfo() != null) {
            gen.writeObjectField("info", value.getInfo());
        }
        if(value.getExternalDocs() != null) {
            gen.writeObjectField("externalDocs", value.getExternalDocs());
        }
        if(value.getServers() != null) {
            gen.writeObjectField("servers", value.getServers());
        }
        if(value.getSecurity() != null) {
            gen.writeObjectField("security", value.getSecurity());
        }
        if(value.getTags() != null) {
            gen.writeObjectField("tags", value.getTags());
        }
        if(value.getPaths() != null) {
            gen.writeObjectField("paths", value.getPaths());
        }
        if(value.getComponents() != null) {
            gen.writeObjectField("components", value.getComponents());
        }
        if(value.getExtensions() != null) {
            for (Entry<String, Object> e : value.getExtensions().entrySet()) {
                gen.writeObjectField(e.getKey(), e.getValue());
            }
        }
        gen.writeEndObject();
    }
}
 
Example 4
Source File: ApiDocV3Service.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private void updateServerAndLink(OpenAPI openAPI, String serviceId, boolean hidden) {
    GatewayConfigProperties gatewayConfigProperties = gatewayClient.getGatewayConfigProperties();
    String swaggerLink = OpenApiUtil.getOpenApiLink(serviceId, gatewayConfigProperties);

    if (openAPI.getServers() != null) {
        openAPI.getServers()
            .forEach(server -> server.setUrl(
                String.format("%s://%s/%s", gatewayConfigProperties.getScheme(), gatewayConfigProperties.getHostname(), server.getUrl())));
    }
    if (!hidden) {
        openAPI.getInfo().setDescription(openAPI.getInfo().getDescription() + swaggerLink);
    }
}
 
Example 5
Source File: OpenAPICodegenUtils.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
/**
 * validate the availability of endpoints.
 * If the api-level endpoints are not provided and there are resources with no endpoints assigned,
 * an exception will be thrown.
 *
 * @param openAPI         {@link OpenAPI} object
 * @param openAPIFilePath file path to openAPI definition
 * @param openAPIVersion the version of the open API used
 */
private static void validateEndpointAvailability(OpenAPI openAPI, String openAPIFilePath, String openAPIVersion) {
    if (openAPI.getServers() != null && openAPI.getServers().get(0).getUrl() != null) {
        return;
    }
    if (openAPI.getExtensions().get(OpenAPIConstants.PRODUCTION_ENDPOINTS) != null ||
            openAPI.getExtensions().get(OpenAPIConstants.SANDBOX_ENDPOINTS) != null) {
        return;
    }

    boolean epsUnavailableForAll = openAPI.getPaths().entrySet().stream().anyMatch(path ->
            isResourceEpUnavailable(path.getValue().getGet()) ||
                    isResourceEpUnavailable(path.getValue().getPost()) ||
                    isResourceEpUnavailable(path.getValue().getPut()) ||
                    isResourceEpUnavailable(path.getValue().getTrace()) ||
                    isResourceEpUnavailable(path.getValue().getHead()) ||
                    isResourceEpUnavailable(path.getValue().getDelete()) ||
                    isResourceEpUnavailable(path.getValue().getPatch()) ||
                    isResourceEpUnavailable(path.getValue().getOptions())
    );

    if (epsUnavailableForAll) {
        String message;
        if (openAPISpec2.equals(openAPIVersion)) {
            message = "Either open API default attributes 'host' and 'basePath' values or ";
        } else {
            message = "Either open API default attribute 'servers' or ";
        }
        throw new CLIRuntimeException(
                message + "wso2 specific extensions '" + OpenAPIConstants.PRODUCTION_ENDPOINTS + "' and '"
                        + OpenAPIConstants.SANDBOX_ENDPOINTS
                        + "' properties are not included under API Level in openAPI " + "definition '"
                        + openAPIFilePath + "'. Please include at least one of them under API Level or "
                        + "provide those properties for all the resources to overcome this issue.");
    }
}
 
Example 6
Source File: TaskGenerateOpenApiTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_UseDefaultProperties_when_applicationPropertiesIsEmpty()
        throws Exception {
    taskGenerateOpenApi = new TaskGenerateOpenApi(applicationPropertiesFile,
            javaSource,
            this.getClass().getClassLoader(),
            generatedOpenAPI);
    taskGenerateOpenApi.execute();

    OpenAPI generatedOpenAPI = getGeneratedOpenAPI();
    Info info = generatedOpenAPI.getInfo();
    Assert.assertEquals(
            "Generated OpenAPI should have default application title",
            OpenApiSpecGenerator.DEFAULT_APPLICATION_TITLE,
            info.getTitle());
    Assert.assertEquals(
            "Generated OpenAPI should have default "
                    + "application API version",
            OpenApiSpecGenerator.DEFAULT_APPLICATION_API_VERSION,
            info.getVersion());

    List<Server> servers = generatedOpenAPI.getServers();
    Assert.assertEquals("Generated OpenAPI should a default server", 1,
            servers.size());
    Assert.assertEquals("Generated OpenAPI should have default url server",
            OpenApiSpecGenerator.DEFAULT_SERVER
                    + OpenApiSpecGenerator.DEFAULT_PREFIX,
            servers.get(0).getUrl());

    Assert.assertEquals(
            "Generated OpenAPI should have default server description",
            OpenApiSpecGenerator.DEFAULT_SERVER_DESCRIPTION,
            servers.get(0).getDescription());
}
 
Example 7
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "data")
public void readServerObject(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);

    Assert.assertNotNull(result);

    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);

    final List<Server> server = openAPI.getServers();
    Assert.assertNotNull(server);
    Assert.assertNotNull(server.get(0));
    Assert.assertNotNull(server.get(0).getUrl());
    Assert.assertEquals(server.get(0).getUrl(),"http://petstore.swagger.io/v2");

    Assert.assertNotNull(server.get(1));
    Assert.assertNotNull(server.get(1).getUrl());
    Assert.assertNotNull(server.get(1).getDescription());
    Assert.assertEquals(server.get(1).getUrl(),"https://development.gigantic-server.com/v1");
    Assert.assertEquals(server.get(1).getDescription(),"Development server");

    Assert.assertNotNull(server.get(2));
    Assert.assertNotNull(server.get(2).getVariables());
    Assert.assertNotNull(server.get(2).getVariables().values());
    Assert.assertNotNull(server.get(2).getVariables().get("username"));
    Assert.assertEquals(server.get(2).getVariables().get("username").getDefault(),"demo");
    Assert.assertEquals(server.get(2).getVariables().get("username").getDescription(),"this value is assigned by the service provider, in this example `gigantic-server.com`");
    Assert.assertNotNull(server.get(2).getVariables().get("port").getEnum());
    Assert.assertEquals(server.get(2).getVariables().get("port").getEnum().get(0),"8443");
    Assert.assertEquals(server.get(2).getVariables().get("port").getEnum().get(1),"443");
    Assert.assertEquals(server.get(2).getVariables().get("port").getDefault(),"8443");
    Assert.assertNotNull(server.get(2).getVariables().get("port"));
    Assert.assertNotNull(server.get(2).getVariables().get("basePath"));
    Assert.assertNotNull(server.get(2).getExtensions().get("x-server"));
    Assert.assertEquals(server.get(2).getExtensions().get("x-server").toString(),"server extension");
    Assert.assertEquals(server.get(2).getVariables().get("basePath").getDescription(),"testing overwriting");
    Assert.assertEquals(server.get(2).getVariables().get("basePath").getDefault(),"v2");
}
 
Example 8
Source File: OpenApiServersValidator.java    From servicecomb-toolkit with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Server> getListProperty(OpenAPI oasObject) {
  return oasObject.getServers();
}
 
Example 9
Source File: OpenApiServersDiffValidator.java    From servicecomb-toolkit with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Server> getListProperty(OpenAPI oasObject) {
  return oasObject.getServers();
}
 
Example 10
Source File: TaskGenerateOpenApiTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Test
public void should_UseGivenProperties_when_applicationPropertiesDefinesThem()
        throws Exception {

    String applicationTitle = "My title";
    String applicationAPIVersion = "1.1.1";
    String applicationServer = "https://example.com";
    String applicationPrefix = "/api";
    String applicationServerDescription = "Example API server";
    StringBuilder applicationPropertiesBuilder = new StringBuilder();
    applicationPropertiesBuilder
            .append(OpenApiSpecGenerator.APPLICATION_TITLE).append("=")
            .append(applicationTitle).append("\n")
            .append(OpenApiSpecGenerator.APPLICATION_API_VERSION)
            .append("=").append(applicationAPIVersion).append("\n")
            .append(OpenApiSpecGenerator.SERVER).append("=")
            .append(applicationServer).append("\n")
            .append(OpenApiSpecGenerator.PREFIX).append("=")
            .append(applicationPrefix).append("\n")
            .append(OpenApiSpecGenerator.SERVER_DESCRIPTION).append("=")
            .append(applicationServerDescription);
    FileUtils.writeStringToFile(applicationPropertiesFile,
            applicationPropertiesBuilder.toString(),
            StandardCharsets.UTF_8);

    taskGenerateOpenApi = new TaskGenerateOpenApi(applicationPropertiesFile,
            javaSource,
            this.getClass().getClassLoader(),
            generatedOpenAPI);
    taskGenerateOpenApi.execute();

    OpenAPI generatedOpenAPI = getGeneratedOpenAPI();
    Info info = generatedOpenAPI.getInfo();
    Assert.assertEquals(
            "Generated OpenAPI should use given application title",
            applicationTitle, info.getTitle());
    Assert.assertEquals(
            "Generated OpenAPI should use given"
                    + "application API version",
            applicationAPIVersion, info.getVersion());

    List<Server> servers = generatedOpenAPI.getServers();
    Assert.assertEquals("Generated OpenAPI should a defined server", 1,
            servers.size());
    Assert.assertEquals("Generated OpenAPI should use given url server",
            applicationServer + applicationPrefix,
            servers.get(0).getUrl());

    Assert.assertEquals(
            "Generated OpenAPI should use given server description",
            applicationServerDescription, servers.get(0).getDescription());
}