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;
}
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);
}
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.
}
}
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));
}
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);
}
Aggregations