Search in sources :

Example 11 with ApiConfigAnnotationReader

use of com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader 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 outputFile = new File(outputFilePath);
    File outputDir = outputFile.getParentFile();
    if (!outputDir.isDirectory() || outputFile.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, outputFile, 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 12 with ApiConfigAnnotationReader

use of com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader in project endpoints-java by cloudendpoints.

the class RestServletRequestParamReaderTest method setUp.

@Before
public void setUp() throws Exception {
    endpointMethod = EndpointMethod.create(TestApi.class, TestApi.class.getMethod("test", Long.TYPE, List.class, SimpleDate.class, TestResource.class));
    request = new MockHttpServletRequest();
    ServiceContext serviceContext = ServiceContext.create();
    serializationConfig = new ApiSerializationConfig();
    TypeLoader typeLoader = new TypeLoader();
    apiConfig = new ApiConfig.Factory().create(serviceContext, typeLoader, TestApi.class);
    ApiConfigAnnotationReader annotationReader = new ApiConfigAnnotationReader();
    annotationReader.loadEndpointClass(serviceContext, TestApi.class, apiConfig);
    annotationReader.loadEndpointMethods(serviceContext, TestApi.class, apiConfig.getApiClassConfig().getMethods());
    methodConfig = apiConfig.getApiClassConfig().getMethods().get(endpointMethod);
}
Also used : ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServiceContext(com.google.api.server.spi.ServiceContext) TypeLoader(com.google.api.server.spi.TypeLoader) ApiSerializationConfig(com.google.api.server.spi.config.model.ApiSerializationConfig) Before(org.junit.Before)

Example 13 with ApiConfigAnnotationReader

use of com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader in project endpoints-java by cloudendpoints.

the class JsonConfigWriterTest method primitiveResponseIsBlocked.

@Test
public void primitiveResponseIsBlocked() throws Exception {
    final class Endpoint {

        @SuppressWarnings("unused")
        public int get() {
            return 4;
        }
    }
    new ApiConfigAnnotationReader().loadEndpointMethods(serviceContext, Endpoint.class, apiConfig.getApiClassConfig().getMethods());
    try {
        writer.writeConfig(Collections.singleton(apiConfig));
        fail();
    } catch (InvalidReturnTypeException e) {
    // Expected.
    }
}
Also used : ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) InvalidReturnTypeException(com.google.api.server.spi.config.validation.InvalidReturnTypeException) Test(org.junit.Test)

Example 14 with ApiConfigAnnotationReader

use of com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader in project endpoints-java by cloudendpoints.

the class JsonConfigWriterTest method writeConfigWithParameterizedTypeTransformerConfig.

/**
 * This tests writeConfig with a parameterized type which is transformed to {@code String}.
 * When the transformer is present, writeConfig should not throw an exception.
 */
@Test
public void writeConfigWithParameterizedTypeTransformerConfig() throws Exception {
    ApiConfig transformedApiConfig = configFactory.copy(apiConfig);
    transformedApiConfig.getSerializationConfig().addSerializationConfig(ParameterizedTypeTransformer.class);
    final class Endpoint {

        @SuppressWarnings("unused")
        public ParameterizedBean get(@Named("date") String id) {
            return null;
        }
    }
    new ApiConfigAnnotationReader().loadEndpointMethods(serviceContext, Endpoint.class, transformedApiConfig.getApiClassConfig().getMethods());
    writer.writeConfig(Collections.singleton(transformedApiConfig));
}
Also used : ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) Named(javax.inject.Named) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) Test(org.junit.Test)

Example 15 with ApiConfigAnnotationReader

use of com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader in project endpoints-java by cloudendpoints.

the class SchemaRepositoryTest method setUp.

@Before
public void setUp() throws Exception {
    TypeLoader typeLoader = new TypeLoader(getClass().getClassLoader());
    ApiConfigAnnotationReader annotationReader = new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes());
    this.repo = new SchemaRepository(typeLoader);
    this.configLoader = new ApiConfigLoader(new ApiConfig.Factory(), typeLoader, annotationReader);
    this.config = configLoader.loadConfiguration(ServiceContext.create(), FooEndpoint.class);
}
Also used : ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) ApiConfigLoader(com.google.api.server.spi.config.ApiConfigLoader) TypeLoader(com.google.api.server.spi.TypeLoader) Before(org.junit.Before)

Aggregations

ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)15 Test (org.junit.Test)9 TypeLoader (com.google.api.server.spi.TypeLoader)6 ApiConfigLoader (com.google.api.server.spi.config.ApiConfigLoader)5 Named (javax.inject.Named)5 Before (org.junit.Before)4 ServiceContext (com.google.api.server.spi.ServiceContext)3 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)3 ApiKey (com.google.api.server.spi.config.model.ApiKey)2 SchemaRepository (com.google.api.server.spi.config.model.SchemaRepository)2 InvalidReturnTypeException (com.google.api.server.spi.config.validation.InvalidReturnTypeException)2 EndpointsPrettyPrinter (com.google.api.server.spi.response.EndpointsPrettyPrinter)2 File (java.io.File)2 URLClassLoader (java.net.URLClassLoader)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 ApiSerializationConfig (com.google.api.server.spi.config.model.ApiSerializationConfig)1 ApiConfigValidator (com.google.api.server.spi.config.validation.ApiConfigValidator)1 PropertyParameterNameConflictException (com.google.api.server.spi.config.validation.PropertyParameterNameConflictException)1 DiscoveryGenerator (com.google.api.server.spi.discovery.DiscoveryGenerator)1