changed resource, method stub for generator options

This commit is contained in:
Nadezhda Makarkina
2015-11-03 11:12:28 +03:00
parent 5d8c23dd09
commit 4eeee3b24d
3 changed files with 107 additions and 7 deletions

View File

@@ -1,6 +1,8 @@
package io.swagger.generator.online;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.ClientOptInput;
import io.swagger.codegen.ClientOpts;
import io.swagger.codegen.Codegen;
@@ -14,19 +16,31 @@ import io.swagger.generator.util.ZipUtil;
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;
import io.swagger.util.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Generator {
static Logger LOGGER = LoggerFactory.getLogger(Generator.class);
public static Map<String, String> getOptions(String language) {
return null;
public static Map<String, CliOption> getOptions(String language) throws ApiException {
CodegenConfig config = null;
try {
config = CodegenConfigLoader.forName(language);
} catch (Exception e) {
throw new BadRequestException(400, String.format("Unsupported target %s supplied. %s", language, e));
}
Map<String, CliOption> map = new LinkedHashMap<String, CliOption>();
for (CliOption option : config.cliOptions()) {
map.put(option.getOpt(), option);
}
return map;
}
public enum Type {

View File

@@ -3,6 +3,7 @@ package io.swagger.generator.resource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.Codegen;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenType;
@@ -89,19 +90,43 @@ public class SwaggerResource {
return Response.status(500).build();
}
}
@GET
@Path("/clients/{language}")
@Produces({MediaType.APPLICATION_JSON})
@ApiOperation(
value = "Returns options for a client library",
response = String.class,
response = CliOption.class,
responseContainer = "map",
tags = "clients")
public Response getClientOptions(
@Context HttpServletRequest request,
@ApiParam(value = "The target language for the client library", required = true) @PathParam("language") String language) throws Exception {
@ApiParam(value = "The target language for the client library", required = true)
@PathParam("language") String language) throws Exception {
Map<String, String> opts = Generator.getOptions(language);
Map<String, CliOption> opts = Generator.getOptions(language);
if (opts != null) {
return Response.ok().entity(opts).build();
} else {
return Response.status(404).build();
}
}
@GET
@Path("/server/{framework}")
@Produces({MediaType.APPLICATION_JSON})
@ApiOperation(
value = "Returns options for a server framework",
response = CliOption.class,
responseContainer = "map",
tags = "clients")
public Response getServerOptions(
@Context HttpServletRequest request,
@ApiParam(value = "The target language for the server framework", required = true)
@PathParam("framework") String framework) throws Exception {
Map<String, CliOption> opts = Generator.getOptions(framework);
if (opts != null) {
return Response.ok().entity(opts).build();

View File

@@ -1,7 +1,11 @@
package io.swagger.generator.online;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfigLoader;
import io.swagger.codegen.options.AkkaScalaClientOptionsProvider;
import io.swagger.codegen.options.AndroidClientOptionsProvider;
import io.swagger.codegen.options.AsyncScalaClientOptionsProvider;
@@ -37,6 +41,9 @@ import io.swagger.generator.exception.ApiException;
import io.swagger.generator.model.GeneratorInput;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
@@ -47,8 +54,13 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
public class OnlineGeneratorOptionsTest {
private static final String OPTIONS_PROVIDER = "optionsProvider";
@@ -74,7 +86,7 @@ public class OnlineGeneratorOptionsTest {
}
@Test(dataProvider = OPTIONS_PROVIDER)
public void optionsTest(OptionsProvider provider) throws ApiException, IOException {
public void generateOptionsTest(OptionsProvider provider) throws ApiException, IOException {
final GeneratorInput input = new GeneratorInput();
final HashMap<String, InvocationCounter> options = convertOptions(provider);
@@ -137,4 +149,53 @@ public class OnlineGeneratorOptionsTest {
return value;
}
}
@Test(dataProvider = OPTIONS_PROVIDER)
public void getOptionsTest(OptionsProvider provider) throws ApiException {
final Map<String, CliOption> opts = Generator.getOptions(provider.getLanguage());
final Function cliOptionWrapper = new Function<CliOption, CliOptionProxy>() {
@Nullable
@Override
public CliOptionProxy apply(@Nullable CliOption option) {
return new CliOptionProxy(option);
}
};
final List<CliOptionProxy> actual = Lists.transform(new ArrayList(opts.values()), cliOptionWrapper);
final List<CliOptionProxy> expected = Lists.transform(
CodegenConfigLoader.forName(provider.getLanguage()).cliOptions(), cliOptionWrapper);
assertEquals(actual, expected);
}
private static class CliOptionProxy {
private final CliOption wrapped;
public CliOptionProxy(CliOption wrapped){
this.wrapped = wrapped;
}
public CliOption getWrapped() {
return wrapped;
}
@Override
public int hashCode() {
return Objects.hash(wrapped.getOpt(), wrapped.getDescription(), wrapped.getType(),
wrapped.getDefault(), wrapped.getEnum());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof CliOptionProxy) {
final CliOption that = ((CliOptionProxy) obj).getWrapped();
return Objects.equals(wrapped.getOpt(), that.getOpt())
&& Objects.equals(wrapped.getDescription(), that.getDescription())
&& Objects.equals(wrapped.getType(), that.getType())
&& Objects.equals(wrapped.getDefault(), that.getDefault())
&& Objects.equals(wrapped.getEnum(), that.getEnum());
}
return false;
}
}
}