Search in sources :

Example 1 with SwaggerContext

use of com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext in project endpoints-java by cloudendpoints.

the class SwaggerGeneratorTest method testWriteSwagger_ArrayEndpoint.

@Test
public void testWriteSwagger_ArrayEndpoint() throws Exception {
    Swagger swagger = getSwagger(ArrayEndpoint.class, new SwaggerContext(), true);
    Swagger expected = readExpectedAsSwagger("array_endpoint.swagger");
    compareSwagger(expected, swagger);
}
Also used : SwaggerContext(com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext) Swagger(io.swagger.models.Swagger) Test(org.junit.Test)

Example 2 with SwaggerContext

use of com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext in project endpoints-java by cloudendpoints.

the class SwaggerGeneratorTest method testWriteSwagger_AbsoluteCommonPathEndpoint.

@Test
public void testWriteSwagger_AbsoluteCommonPathEndpoint() throws Exception {
    Swagger swagger = getSwagger(AbsoluteCommonPathEndpoint.class, new SwaggerContext(), true);
    Swagger expected = readExpectedAsSwagger("absolute_common_path_endpoint.swagger");
    compareSwagger(expected, swagger);
}
Also used : SwaggerContext(com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext) Swagger(io.swagger.models.Swagger) Test(org.junit.Test)

Example 3 with SwaggerContext

use of com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext in project endpoints-java by cloudendpoints.

the class SwaggerGeneratorTest method testWriteSwagger_FooEndpointLocalhost.

@Test
public void testWriteSwagger_FooEndpointLocalhost() throws Exception {
    Swagger swagger = getSwagger(FooEndpoint.class, new SwaggerContext().setApiRoot("http://localhost:8080/api"), false);
    Swagger expected = readExpectedAsSwagger("foo_endpoint_localhost.swagger");
    compareSwagger(expected, swagger);
}
Also used : SwaggerContext(com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext) Swagger(io.swagger.models.Swagger) Test(org.junit.Test)

Example 4 with SwaggerContext

use of com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext in project endpoints-java by cloudendpoints.

the class GetOpenApiDocAction method genOpenApiDoc.

/**
 * Generates an OpenAPI document for an array of service classes.
 *
 * @param classPath Class path to load service classes and their dependencies
 * @param outputFilePath File to store the OpenAPI document in
 * @param hostname The hostname to use for the OpenAPI document
 * @param basePath The base path to use for the OpenAPI document, e.g. /_ah/api
 * @param serviceClassNames Array of service class names of the API
 * @param outputToDisk Iff {@code true}, outputs a openapi.json to disk.
 * @return a single OpenAPI document representing all service classes.
 */
public String genOpenApiDoc(URL[] classPath, String outputFilePath, String hostname, String basePath, List<String> serviceClassNames, boolean outputToDisk) throws ClassNotFoundException, IOException, ApiConfigException {
    File outputDir = new File(outputFilePath).getParentFile();
    if (!outputDir.isDirectory()) {
        throw new IllegalArgumentException(outputFilePath + " is not a file");
    }
    ClassLoader classLoader = new URLClassLoader(classPath, getClass().getClassLoader());
    ApiConfig.Factory configFactory = new ApiConfig.Factory();
    Class<?>[] serviceClasses = loadClasses(classLoader, serviceClassNames);
    List<ApiConfig> apiConfigs = Lists.newArrayListWithCapacity(serviceClasses.length);
    TypeLoader typeLoader = new TypeLoader(classLoader);
    ApiConfigLoader configLoader = new ApiConfigLoader(configFactory, typeLoader, new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes()));
    ServiceContext serviceContext = ServiceContext.create();
    for (Class<?> serviceClass : serviceClasses) {
        apiConfigs.add(configLoader.loadConfiguration(serviceContext, serviceClass));
    }
    SwaggerGenerator generator = new SwaggerGenerator();
    SwaggerContext swaggerContext = new SwaggerContext().setHostname(hostname).setBasePath(basePath);
    Swagger swagger = generator.writeSwagger(apiConfigs, true, swaggerContext);
    String swaggerStr = Json.mapper().writer(new EndpointsPrettyPrinter()).writeValueAsString(swagger);
    if (outputToDisk) {
        Files.write(swaggerStr, new File(outputFilePath), UTF_8);
        System.out.println("OpenAPI document written to " + outputFilePath);
    }
    return swaggerStr;
}
Also used : ApiConfigLoader(com.google.api.server.spi.config.ApiConfigLoader) SwaggerContext(com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext) EndpointsPrettyPrinter(com.google.api.server.spi.response.EndpointsPrettyPrinter) ServiceContext(com.google.api.server.spi.ServiceContext) TypeLoader(com.google.api.server.spi.TypeLoader) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) SwaggerGenerator(com.google.api.server.spi.swagger.SwaggerGenerator) URLClassLoader(java.net.URLClassLoader) Swagger(io.swagger.models.Swagger) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 5 with SwaggerContext

use of com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext in project endpoints-java by cloudendpoints.

the class SwaggerGeneratorTest method testWriteSwagger_LimitMetricsEndpoint.

@Test
public void testWriteSwagger_LimitMetricsEndpoint() throws Exception {
    Swagger swagger = getSwagger(LimitMetricsEndpoint.class, new SwaggerContext(), true);
    Swagger expected = readExpectedAsSwagger("limit_metrics_endpoint.swagger");
    compareSwagger(expected, swagger);
}
Also used : SwaggerContext(com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext) Swagger(io.swagger.models.Swagger) Test(org.junit.Test)

Aggregations

SwaggerContext (com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext)8 Swagger (io.swagger.models.Swagger)8 Test (org.junit.Test)7 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)2 ServiceContext (com.google.api.server.spi.ServiceContext)1 TypeLoader (com.google.api.server.spi.TypeLoader)1 ApiConfigLoader (com.google.api.server.spi.config.ApiConfigLoader)1 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)1 EndpointsPrettyPrinter (com.google.api.server.spi.response.EndpointsPrettyPrinter)1 SwaggerGenerator (com.google.api.server.spi.swagger.SwaggerGenerator)1 File (java.io.File)1 URLClassLoader (java.net.URLClassLoader)1