forked from loafle/openapi-generator-original
Merge remote-tracking branch 'origin/master' into 2.3.0
This commit is contained in:
commit
ef5c6a939d
@ -1,17 +1,15 @@
|
||||
/**
|
||||
* Copyright 2016 SmartBear Software
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package io.swagger.generator;
|
||||
@ -32,13 +30,13 @@ public class Bootstrap extends HttpServlet {
|
||||
DynamicSwaggerConfig bc = new DynamicSwaggerConfig();
|
||||
bc.setBasePath("/api");
|
||||
bc.setTitle("Swagger Generator");
|
||||
bc.setDescription("This is an online swagger codegen server. You can find out more " +
|
||||
"at https://github.com/swagger-api/swagger-codegen or on [irc.freenode.net, #swagger](http://swagger.io/irc/).");
|
||||
bc.setDescription("This is an online swagger codegen server. You can find out more "
|
||||
+ "at https://github.com/swagger-api/swagger-codegen or on [irc.freenode.net, #swagger](http://swagger.io/irc/).");
|
||||
bc.setTermsOfServiceUrl("http://swagger.io/terms/");
|
||||
bc.setContact("apiteam@swagger.io");
|
||||
bc.setLicense("Apache 2.0");
|
||||
InputStream stream = getClass().getResourceAsStream("/version.prop");
|
||||
if(stream == null) {
|
||||
if (stream == null) {
|
||||
bc.setVersion("0.0.0");
|
||||
} else {
|
||||
try {
|
||||
|
@ -21,7 +21,8 @@ public class DynamicSwaggerConfig extends BeanConfig {
|
||||
static {
|
||||
List<CodegenConfig> extensions = Codegen.getExtensions();
|
||||
for (CodegenConfig config : extensions) {
|
||||
if (config.getTag().equals(CodegenType.CLIENT) || config.getTag().equals(CodegenType.DOCUMENTATION)) {
|
||||
if (config.getTag().equals(CodegenType.CLIENT)
|
||||
|| config.getTag().equals(CodegenType.DOCUMENTATION)) {
|
||||
clients.add(config.getName());
|
||||
} else if (config.getTag().equals(CodegenType.SERVER)) {
|
||||
servers.add(config.getName());
|
||||
@ -44,7 +45,7 @@ public class DynamicSwaggerConfig extends BeanConfig {
|
||||
}
|
||||
|
||||
Operation get = clientPath.getGet();
|
||||
if(get != null) {
|
||||
if (get != null) {
|
||||
framework = get.getParameters().get(0);
|
||||
if (framework instanceof PathParameter) {
|
||||
PathParameter param = (PathParameter) framework;
|
||||
@ -63,7 +64,7 @@ public class DynamicSwaggerConfig extends BeanConfig {
|
||||
}
|
||||
|
||||
Operation get = serverPath.getGet();
|
||||
if(get != null) {
|
||||
if (get != null) {
|
||||
framework = get.getParameters().get(0);
|
||||
if (framework instanceof PathParameter) {
|
||||
PathParameter param = (PathParameter) framework;
|
||||
@ -72,8 +73,6 @@ public class DynamicSwaggerConfig extends BeanConfig {
|
||||
}
|
||||
}
|
||||
|
||||
return swagger.info(getInfo())
|
||||
.host(getHost())
|
||||
.basePath("/api");
|
||||
return swagger.info(getInfo()).host(getHost()).basePath("/api");
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,15 @@
|
||||
/**
|
||||
* Copyright 2016 SmartBear Software
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package io.swagger.generator.exception;
|
||||
@ -25,8 +23,7 @@ public class ApiException extends Exception {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int getCode()
|
||||
{
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,15 @@
|
||||
/**
|
||||
* Copyright 2016 SmartBear Software
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package io.swagger.generator.exception;
|
||||
|
@ -1,17 +1,15 @@
|
||||
/**
|
||||
* Copyright 2016 SmartBear Software
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package io.swagger.generator.exception;
|
||||
|
@ -1,17 +1,15 @@
|
||||
/**
|
||||
* Copyright 2016 SmartBear Software
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package io.swagger.generator.model;
|
||||
@ -30,8 +28,7 @@ public class ApiResponse {
|
||||
String type;
|
||||
String message;
|
||||
|
||||
public ApiResponse() {
|
||||
}
|
||||
public ApiResponse() {}
|
||||
|
||||
public ApiResponse(int code, String message) {
|
||||
this.code = code;
|
||||
|
@ -1,17 +1,15 @@
|
||||
/**
|
||||
* Copyright 2016 SmartBear Software
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package io.swagger.generator.model;
|
||||
|
@ -57,4 +57,4 @@ public class GeneratorInput {
|
||||
public void setSecurityDefinition(SecuritySchemeDefinition auth) {
|
||||
this.auth = auth;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,15 @@
|
||||
/**
|
||||
* Copyright 2016 SmartBear Software
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package io.swagger.generator.model;
|
||||
@ -22,8 +20,7 @@ public class InputOption {
|
||||
private Boolean required;
|
||||
private String defaultValue;
|
||||
|
||||
public InputOption() {
|
||||
}
|
||||
public InputOption() {}
|
||||
|
||||
public InputOption(String name, String description, String defaultValue, Boolean required) {
|
||||
this.name = name;
|
||||
|
@ -6,15 +6,15 @@ public class ResponseCode {
|
||||
private String code;
|
||||
private String link;
|
||||
|
||||
public ResponseCode() {
|
||||
}
|
||||
public ResponseCode() {}
|
||||
|
||||
public ResponseCode(String code, String link) {
|
||||
setCode(code);
|
||||
setLink(link);
|
||||
}
|
||||
|
||||
@ApiModelProperty(value = "File download code", example = "d40029be-eda6-4d62-b1ef-d05e2e91a72a")
|
||||
@ApiModelProperty(value = "File download code",
|
||||
example = "d40029be-eda6-4d62-b1ef-d05e2e91a72a")
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
@ -23,7 +23,9 @@ public class ResponseCode {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@ApiModelProperty(value = "URL for fetching the generated client", example = "http://generator.swagger.io:80/api/gen/download/d40029be-eda6-4d62-b1ef-d05e2e91a72a")
|
||||
@ApiModelProperty(
|
||||
value = "URL for fetching the generated client",
|
||||
example = "http://generator.swagger.io:80/api/gen/download/d40029be-eda6-4d62-b1ef-d05e2e91a72a")
|
||||
public String getLink() {
|
||||
return link;
|
||||
}
|
||||
@ -31,4 +33,4 @@ public class ResponseCode {
|
||||
public void setLink(String link) {
|
||||
this.link = link;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,8 @@ public class Generator {
|
||||
try {
|
||||
config = CodegenConfigLoader.forName(language);
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException(String.format("Unsupported target %s supplied. %s", language, e));
|
||||
throw new BadRequestException(String.format("Unsupported target %s supplied. %s",
|
||||
language, e));
|
||||
}
|
||||
Map<String, CliOption> map = new LinkedHashMap<String, CliOption>();
|
||||
for (CliOption option : config.cliOptions()) {
|
||||
@ -38,8 +39,7 @@ public class Generator {
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
CLIENT("client"),
|
||||
SERVER("server");
|
||||
CLIENT("client"), SERVER("server");
|
||||
|
||||
private String name;
|
||||
|
||||
@ -60,37 +60,39 @@ public class Generator {
|
||||
return generate(language, opts, Type.SERVER);
|
||||
}
|
||||
|
||||
private static String generate(String language, GeneratorInput opts, Type type) throws ApiException {
|
||||
private static String generate(String language, GeneratorInput opts, Type type)
|
||||
throws ApiException {
|
||||
LOGGER.debug(String.format("generate %s for %s", type.getTypeName(), language));
|
||||
if (opts == null) {
|
||||
throw new BadRequestException("No options were supplied");
|
||||
}
|
||||
JsonNode node = opts.getSpec();
|
||||
if(node != null && "{}".equals(node.toString())) {
|
||||
if (node != null && "{}".equals(node.toString())) {
|
||||
LOGGER.debug("ignoring empty spec");
|
||||
node = null;
|
||||
}
|
||||
Swagger swagger;
|
||||
if (node == null) {
|
||||
if (opts.getSwaggerUrl() != null) {
|
||||
if(opts.getAuthorizationValue() != null) {
|
||||
List<AuthorizationValue> authorizationValues = new ArrayList<AuthorizationValue>();
|
||||
if (opts.getAuthorizationValue() != null) {
|
||||
List<AuthorizationValue> authorizationValues =
|
||||
new ArrayList<AuthorizationValue>();
|
||||
authorizationValues.add(opts.getAuthorizationValue());
|
||||
|
||||
swagger = new SwaggerParser().read(opts.getSwaggerUrl(), authorizationValues, true);
|
||||
}
|
||||
else {
|
||||
swagger =
|
||||
new SwaggerParser().read(opts.getSwaggerUrl(), authorizationValues,
|
||||
true);
|
||||
} else {
|
||||
swagger = new SwaggerParser().read(opts.getSwaggerUrl());
|
||||
}
|
||||
} else {
|
||||
throw new BadRequestException("No swagger specification was supplied");
|
||||
}
|
||||
} else if(opts.getAuthorizationValue() != null) {
|
||||
} else if (opts.getAuthorizationValue() != null) {
|
||||
List<AuthorizationValue> authorizationValues = new ArrayList<AuthorizationValue>();
|
||||
authorizationValues.add(opts.getAuthorizationValue());
|
||||
swagger = new SwaggerParser().read(node, authorizationValues, true);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
swagger = new SwaggerParser().read(node, true);
|
||||
}
|
||||
if (swagger == null) {
|
||||
@ -99,12 +101,11 @@ public class Generator {
|
||||
|
||||
String destPath = null;
|
||||
|
||||
if(opts != null && opts.getOptions() != null) {
|
||||
if (opts != null && opts.getOptions() != null) {
|
||||
destPath = opts.getOptions().get("outputFolder");
|
||||
}
|
||||
if(destPath == null) {
|
||||
destPath = language + "-"
|
||||
+ type.getTypeName();
|
||||
if (destPath == null) {
|
||||
destPath = language + "-" + type.getTypeName();
|
||||
}
|
||||
|
||||
ClientOptInput clientOptInput = new ClientOptInput();
|
||||
@ -112,14 +113,12 @@ public class Generator {
|
||||
String outputFolder = getTmpFolder().getAbsolutePath() + File.separator + destPath;
|
||||
String outputFilename = outputFolder + "-bundle.zip";
|
||||
|
||||
clientOptInput
|
||||
.opts(clientOpts)
|
||||
.swagger(swagger);
|
||||
clientOptInput.opts(clientOpts).swagger(swagger);
|
||||
|
||||
CodegenConfig codegenConfig=null;
|
||||
CodegenConfig codegenConfig = null;
|
||||
try {
|
||||
codegenConfig = CodegenConfigLoader.forName(language);
|
||||
} catch(RuntimeException e) {
|
||||
} catch (RuntimeException e) {
|
||||
throw new BadRequestException("Unsupported target " + language + " supplied");
|
||||
}
|
||||
|
||||
@ -143,20 +142,19 @@ public class Generator {
|
||||
ZipUtil zip = new ZipUtil();
|
||||
zip.compressFiles(filesToAdd, outputFilename);
|
||||
} else {
|
||||
throw new BadRequestException("A target generation was attempted, but no files were created!");
|
||||
throw new BadRequestException(
|
||||
"A target generation was attempted, but no files were created!");
|
||||
}
|
||||
for(File file: files) {
|
||||
for (File file : files) {
|
||||
try {
|
||||
file.delete();
|
||||
}
|
||||
catch(Exception e) {
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("unable to delete file " + file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
try {
|
||||
new File(outputFolder).delete();
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("unable to delete output folder " + outputFolder);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
@ -17,35 +17,26 @@ public class ExceptionWriter implements ExceptionMapper<Exception> {
|
||||
public Response toResponse(Exception exception) {
|
||||
if (exception instanceof javax.ws.rs.WebApplicationException) {
|
||||
javax.ws.rs.WebApplicationException e = (javax.ws.rs.WebApplicationException) exception;
|
||||
return Response
|
||||
.status(e.getResponse().getStatus())
|
||||
.entity(new ApiResponse(e.getResponse().getStatus(),
|
||||
exception.getMessage())).build();
|
||||
return Response.status(e.getResponse().getStatus())
|
||||
.entity(new ApiResponse(e.getResponse().getStatus(), exception.getMessage()))
|
||||
.build();
|
||||
} else if (exception instanceof com.fasterxml.jackson.core.JsonParseException) {
|
||||
return Response.status(400)
|
||||
.entity(new ApiResponse(400, "bad input")).build();
|
||||
return Response.status(400).entity(new ApiResponse(400, "bad input")).build();
|
||||
} else if (exception instanceof ValidationException) {
|
||||
ValidationException e = (ValidationException) exception;
|
||||
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
|
||||
} else if (exception instanceof NotFoundException) {
|
||||
return Response
|
||||
.status(Status.NOT_FOUND)
|
||||
.entity(new ApiResponse(ApiResponse.ERROR, exception
|
||||
.getMessage())).build();
|
||||
return Response.status(Status.NOT_FOUND)
|
||||
.entity(new ApiResponse(ApiResponse.ERROR, exception.getMessage())).build();
|
||||
} else if (exception instanceof BadRequestException) {
|
||||
return Response
|
||||
.status(Status.BAD_REQUEST)
|
||||
.entity(new ApiResponse(ApiResponse.ERROR, exception
|
||||
.getMessage())).build();
|
||||
return Response.status(Status.BAD_REQUEST)
|
||||
.entity(new ApiResponse(ApiResponse.ERROR, exception.getMessage())).build();
|
||||
} else if (exception instanceof ApiException) {
|
||||
return Response
|
||||
.status(Status.BAD_REQUEST)
|
||||
.entity(new ApiResponse(ApiResponse.ERROR, exception
|
||||
.getMessage())).build();
|
||||
return Response.status(Status.BAD_REQUEST)
|
||||
.entity(new ApiResponse(ApiResponse.ERROR, exception.getMessage())).build();
|
||||
} else {
|
||||
return Response.status(500)
|
||||
.entity(new ApiResponse(500, "something bad happened"))
|
||||
return Response.status(500).entity(new ApiResponse(500, "something bad happened"))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,8 @@ public class SwaggerResource {
|
||||
static {
|
||||
List<CodegenConfig> extensions = Codegen.getExtensions();
|
||||
for (CodegenConfig config : extensions) {
|
||||
if (config.getTag().equals(CodegenType.CLIENT) || config.getTag().equals(CodegenType.DOCUMENTATION)) {
|
||||
if (config.getTag().equals(CodegenType.CLIENT)
|
||||
|| config.getTag().equals(CodegenType.DOCUMENTATION)) {
|
||||
clients.add(config.getName());
|
||||
} else if (config.getTag().equals(CodegenType.SERVER)) {
|
||||
servers.add(config.getName());
|
||||
@ -48,12 +49,11 @@ public class SwaggerResource {
|
||||
@GET
|
||||
@Path("/download/{fileId}")
|
||||
@Produces({MediaType.APPLICATION_OCTET_STREAM})
|
||||
@ApiOperation(value = "Downloads a pre-generated file",
|
||||
notes = "A valid `fileId` is generated by the `/clients/{language}` or `/servers/{language}` POST " +
|
||||
"operations. The fileId code can be used just once, after which a new `fileId` will need to " +
|
||||
"be requested.",
|
||||
response = String.class,
|
||||
tags = {"clients", "servers"})
|
||||
@ApiOperation(
|
||||
value = "Downloads a pre-generated file",
|
||||
notes = "A valid `fileId` is generated by the `/clients/{language}` or `/servers/{language}` POST "
|
||||
+ "operations. The fileId code can be used just once, after which a new `fileId` will need to "
|
||||
+ "be requested.", response = String.class, tags = {"clients", "servers"})
|
||||
public Response downloadFile(@PathParam("fileId") String fileId) throws Exception {
|
||||
Generated g = fileMap.get(fileId);
|
||||
System.out.println("looking for fileId " + fileId);
|
||||
@ -64,16 +64,15 @@ public class SwaggerResource {
|
||||
|
||||
try {
|
||||
FileUtils.deleteDirectory(file.getParentFile());
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
System.out.println("failed to delete file " + file.getAbsolutePath());
|
||||
}
|
||||
|
||||
return Response.ok(bytes, "application/zip")
|
||||
.header("Content-Disposition", "attachment; filename=\"" + g.getFriendlyName() + "-generated.zip\"")
|
||||
.header("Accept-Range", "bytes")
|
||||
.header("Content-Length", bytes.length)
|
||||
.build();
|
||||
return Response
|
||||
.ok(bytes, "application/zip")
|
||||
.header("Content-Disposition",
|
||||
"attachment; filename=\"" + g.getFriendlyName() + "-generated.zip\"")
|
||||
.header("Accept-Range", "bytes").header("Content-Length", bytes.length).build();
|
||||
} else {
|
||||
return Response.status(404).build();
|
||||
}
|
||||
@ -84,23 +83,22 @@ public class SwaggerResource {
|
||||
@ApiOperation(
|
||||
value = "Generates a client library",
|
||||
notes = "Accepts a `GeneratorInput` options map for spec location and generation options",
|
||||
response = ResponseCode.class,
|
||||
tags = "clients")
|
||||
response = ResponseCode.class, tags = "clients")
|
||||
public Response generateClient(
|
||||
@Context HttpServletRequest request,
|
||||
@ApiParam(value = "The target language for the client library", required = true) @PathParam("language") String language,
|
||||
@ApiParam(value = "Configuration for building the client library", required = true) GeneratorInput opts) throws Exception {
|
||||
@ApiParam(value = "Configuration for building the client library", required = true) GeneratorInput opts)
|
||||
throws Exception {
|
||||
|
||||
String filename = Generator.generateClient(language, opts);
|
||||
String host = System.getenv("GENERATOR_HOST");
|
||||
|
||||
if(StringUtils.isBlank(host)) {
|
||||
if (StringUtils.isBlank(host)) {
|
||||
String scheme = request.getHeader("X-SSL");
|
||||
String port = "";
|
||||
if("1".equals(scheme)) {
|
||||
if ("1".equals(scheme)) {
|
||||
scheme = "https";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
scheme = request.getScheme();
|
||||
port = ":" + request.getServerPort();
|
||||
}
|
||||
@ -124,15 +122,12 @@ public class SwaggerResource {
|
||||
@GET
|
||||
@Path("/clients/{language}")
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
@ApiOperation(
|
||||
value = "Returns options for a client library",
|
||||
response = CliOption.class,
|
||||
responseContainer = "map",
|
||||
tags = "clients")
|
||||
@ApiOperation(value = "Returns options for a client library", response = CliOption.class,
|
||||
responseContainer = "map", tags = "clients")
|
||||
public Response getClientOptions(
|
||||
@SuppressWarnings("unused") @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, CliOption> opts = Generator.getOptions(language);
|
||||
|
||||
@ -146,15 +141,12 @@ public class SwaggerResource {
|
||||
@GET
|
||||
@Path("/servers/{framework}")
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
@ApiOperation(
|
||||
value = "Returns options for a server framework",
|
||||
response = CliOption.class,
|
||||
responseContainer = "map",
|
||||
tags = "servers")
|
||||
@ApiOperation(value = "Returns options for a server framework", response = CliOption.class,
|
||||
responseContainer = "map", tags = "servers")
|
||||
public Response getServerOptions(
|
||||
@SuppressWarnings("unused") @Context HttpServletRequest request,
|
||||
@ApiParam(value = "The target language for the server framework", required = true)
|
||||
@PathParam("framework") String framework) throws Exception {
|
||||
@ApiParam(value = "The target language for the server framework", required = true) @PathParam("framework") String framework)
|
||||
throws Exception {
|
||||
|
||||
Map<String, CliOption> opts = Generator.getOptions(framework);
|
||||
|
||||
@ -168,9 +160,7 @@ public class SwaggerResource {
|
||||
@GET
|
||||
@Path("/clients")
|
||||
@ApiOperation(value = "Gets languages supported by the client generator",
|
||||
response = String.class,
|
||||
responseContainer = "List",
|
||||
tags = "clients")
|
||||
response = String.class, responseContainer = "List", tags = "clients")
|
||||
public Response clientOptions() {
|
||||
String[] languages = new String[clients.size()];
|
||||
languages = clients.toArray(languages);
|
||||
@ -180,9 +170,7 @@ public class SwaggerResource {
|
||||
@GET
|
||||
@Path("/servers")
|
||||
@ApiOperation(value = "Gets languages supported by the server generator",
|
||||
response = String.class,
|
||||
responseContainer = "List",
|
||||
tags = "servers")
|
||||
response = String.class, responseContainer = "List", tags = "servers")
|
||||
public Response serverOptions() {
|
||||
String[] languages = new String[servers.size()];
|
||||
languages = servers.toArray(languages);
|
||||
@ -191,22 +179,22 @@ public class SwaggerResource {
|
||||
|
||||
@POST
|
||||
@Path("/servers/{framework}")
|
||||
@ApiOperation(value = "Generates a server library",
|
||||
@ApiOperation(
|
||||
value = "Generates a server library",
|
||||
notes = "Accepts a `GeneratorInput` options map for spec location and generation options.",
|
||||
response = ResponseCode.class,
|
||||
tags = "servers")
|
||||
public Response generateServerForLanguage(
|
||||
@Context HttpServletRequest request,
|
||||
@ApiParam(value = "framework", required = true) @PathParam("framework") String framework,
|
||||
@ApiParam(value = "parameters", required = true) GeneratorInput opts)
|
||||
throws Exception {
|
||||
response = ResponseCode.class, tags = "servers")
|
||||
public Response generateServerForLanguage(@Context HttpServletRequest request, @ApiParam(
|
||||
value = "framework", required = true) @PathParam("framework") String framework,
|
||||
@ApiParam(value = "parameters", required = true) GeneratorInput opts) throws Exception {
|
||||
if (framework == null) {
|
||||
throw new BadRequestException("Framework is required");
|
||||
}
|
||||
String filename = Generator.generateServer(framework, opts);
|
||||
System.out.println("generated name: " + filename);
|
||||
|
||||
String host = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
|
||||
String host =
|
||||
request.getScheme() + "://" + request.getServerName() + ":"
|
||||
+ request.getServerPort();
|
||||
|
||||
if (filename != null) {
|
||||
String code = String.valueOf(UUID.randomUUID().toString());
|
||||
|
@ -1,17 +1,15 @@
|
||||
/**
|
||||
* Copyright 2016 SmartBear Software
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package io.swagger.generator.util;
|
||||
@ -26,8 +24,8 @@ import java.io.IOException;
|
||||
|
||||
public class ApiOriginFilter implements javax.servlet.Filter {
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
HttpServletResponse res = (HttpServletResponse) response;
|
||||
res.addHeader("Access-Control-Allow-Origin", "*");
|
||||
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
|
||||
@ -36,10 +34,8 @@ public class ApiOriginFilter implements javax.servlet.Filter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
public void destroy() {}
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
}
|
||||
}
|
||||
public void init(FilterConfig filterConfig) throws ServletException {}
|
||||
}
|
||||
|
@ -17,4 +17,4 @@ public class JacksonJsonProvider extends JacksonJaxbJsonProvider {
|
||||
public JacksonJsonProvider() {
|
||||
super.setMapper(commonMapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,4 +25,4 @@ public class ValidationException extends Exception {
|
||||
public void setMessage(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,4 +26,4 @@ public class ValidationMessage {
|
||||
public void setSeverity(String severity) {
|
||||
this.severity = severity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,15 @@
|
||||
/**
|
||||
* Copyright 2016 SmartBear Software
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package io.swagger.generator.util;
|
||||
@ -27,8 +25,9 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* This utility compresses a list of files to standard ZIP format file.
|
||||
* It is able to compresses all sub files and sub directories, recursively.
|
||||
* This utility compresses a list of files to standard ZIP format file. It is able to compresses all
|
||||
* sub files and sub directories, recursively.
|
||||
*
|
||||
* @author Ha Minh Nam
|
||||
*
|
||||
*/
|
||||
@ -40,6 +39,7 @@ public class ZipUtil {
|
||||
|
||||
/**
|
||||
* Compresses a collection of files to a destination zip file.
|
||||
*
|
||||
* @param listFiles A collection of files and directories
|
||||
* @param destZipFile The path of the destination zip file
|
||||
* @throws FileNotFoundException if file not found
|
||||
@ -64,14 +64,15 @@ public class ZipUtil {
|
||||
|
||||
/**
|
||||
* Adds a directory to the current zip output stream.
|
||||
* @param folder the directory to be added
|
||||
*
|
||||
* @param folder the directory to be added
|
||||
* @param parentFolder the path of parent directory
|
||||
* @param zos the current zip output stream
|
||||
* @throws FileNotFoundException if file not found
|
||||
* @throws IOException if IO exception occurs
|
||||
*/
|
||||
private void addFolderToZip(File folder, String parentFolder,
|
||||
ZipOutputStream zos) throws FileNotFoundException, IOException {
|
||||
private void addFolderToZip(File folder, String parentFolder, ZipOutputStream zos)
|
||||
throws FileNotFoundException, IOException {
|
||||
for (File file : folder.listFiles()) {
|
||||
if (file.isDirectory()) {
|
||||
addFolderToZip(file, parentFolder + "/" + file.getName(), zos);
|
||||
@ -80,8 +81,7 @@ public class ZipUtil {
|
||||
|
||||
zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
|
||||
|
||||
BufferedInputStream bis = new BufferedInputStream(
|
||||
new FileInputStream(file));
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
||||
|
||||
long bytesRead = 0;
|
||||
byte[] bytesIn = new byte[BUFFER_SIZE];
|
||||
@ -99,17 +99,17 @@ public class ZipUtil {
|
||||
|
||||
/**
|
||||
* Adds a file to the current zip output stream.
|
||||
*
|
||||
* @param file the file to be added
|
||||
* @param zos the current zip output stream
|
||||
* @throws FileNotFoundException if file not found
|
||||
* @throws IOException if IO exception occurs
|
||||
*/
|
||||
private static void addFileToZip(File file, ZipOutputStream zos)
|
||||
throws FileNotFoundException, IOException {
|
||||
private static void addFileToZip(File file, ZipOutputStream zos) throws FileNotFoundException,
|
||||
IOException {
|
||||
zos.putNextEntry(new ZipEntry(file.getName()));
|
||||
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
|
||||
file));
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
||||
|
||||
byte[] bytesIn = new byte[BUFFER_SIZE];
|
||||
int read = 0;
|
||||
|
@ -32,32 +32,35 @@ public class OnlineGeneratorOptionsTest {
|
||||
|
||||
@DataProvider(name = OPTIONS_PROVIDER)
|
||||
private static Object[][] listOptions() {
|
||||
return new Object[][]{{new AkkaScalaClientOptionsProvider()}, {new AndroidClientOptionsProvider()},
|
||||
{new AsyncScalaClientOptionsProvider()}, {new CSharpClientOptionsProvider()},
|
||||
{new CsharpDotNet2ClientOptionsProvider()}, {new DartClientOptionsProvider()},
|
||||
{new FlashClienOptionsProvider()}, {new JavaInflectorServerOptionsProvider()},
|
||||
return new Object[][] {
|
||||
{new AkkaScalaClientOptionsProvider()},
|
||||
{new AndroidClientOptionsProvider()},
|
||||
{new AsyncScalaClientOptionsProvider()},
|
||||
{new CSharpClientOptionsProvider()},
|
||||
{new CsharpDotNet2ClientOptionsProvider()},
|
||||
{new DartClientOptionsProvider()},
|
||||
{new FlashClienOptionsProvider()},
|
||||
{new JavaInflectorServerOptionsProvider()},
|
||||
{
|
||||
new JavaOptionsProvider(),
|
||||
new JavaOptionsProvider(ImmutableMap.of(CodegenConstants.LIBRARY, JavaClientCodegen.RETROFIT_2)
|
||||
),
|
||||
new JavaOptionsProvider(ImmutableMap.of(
|
||||
CodegenConstants.LIBRARY, JavaClientCodegen.RETROFIT_2,
|
||||
JavaClientCodegen.USE_RX_JAVA, "true")
|
||||
)
|
||||
},
|
||||
{new JaxRSServerOptionsProvider()},
|
||||
{new NodeJSServerOptionsProvider()}, {new ObjcClientOptionsProvider()},
|
||||
{new PerlClientOptionsProvider()}, {new PhpClientOptionsProvider()},
|
||||
{new PythonClientOptionsProvider()}, {new Qt5CPPOptionsProvider()},
|
||||
{new RubyClientOptionsProvider()}, {new ScalaClientOptionsProvider()},
|
||||
{new ScalatraServerOptionsProvider()}, {new SilexServerOptionsProvider()},
|
||||
{new SinatraServerOptionsProvider()}, {new SlimFrameworkServerOptionsProvider()},
|
||||
{new SpringOptionsProvider()}, {new StaticDocOptionsProvider()},
|
||||
{new StaticHtmlOptionsProvider()}, {new SwaggerOptionsProvider()},
|
||||
{new SwaggerYamlOptionsProvider()}, {new SwiftOptionsProvider()},
|
||||
{new TizenClientOptionsProvider()}, {new TypeScriptAngularClientOptionsProvider()},
|
||||
{new TypeScriptNodeClientOptionsProvider()}, {new LumenServerOptionsProvider()}
|
||||
};
|
||||
new JavaOptionsProvider(ImmutableMap.of(CodegenConstants.LIBRARY,
|
||||
JavaClientCodegen.RETROFIT_2)),
|
||||
new JavaOptionsProvider(
|
||||
ImmutableMap.of(CodegenConstants.LIBRARY,
|
||||
JavaClientCodegen.RETROFIT_2,
|
||||
JavaClientCodegen.USE_RX_JAVA, "true"))},
|
||||
{new JaxRSServerOptionsProvider()}, {new NodeJSServerOptionsProvider()},
|
||||
{new ObjcClientOptionsProvider()}, {new PerlClientOptionsProvider()},
|
||||
{new PhpClientOptionsProvider()}, {new PythonClientOptionsProvider()},
|
||||
{new Qt5CPPOptionsProvider()}, {new RubyClientOptionsProvider()},
|
||||
{new ScalaClientOptionsProvider()}, {new ScalatraServerOptionsProvider()},
|
||||
{new SilexServerOptionsProvider()}, {new SinatraServerOptionsProvider()},
|
||||
{new SlimFrameworkServerOptionsProvider()}, {new SpringOptionsProvider()},
|
||||
{new StaticDocOptionsProvider()}, {new StaticHtmlOptionsProvider()},
|
||||
{new SwaggerOptionsProvider()}, {new SwaggerYamlOptionsProvider()},
|
||||
{new SwiftOptionsProvider()}, {new TizenClientOptionsProvider()},
|
||||
{new TypeScriptAngularClientOptionsProvider()},
|
||||
{new TypeScriptNodeClientOptionsProvider()}, {new LumenServerOptionsProvider()}};
|
||||
}
|
||||
|
||||
@Test(dataProvider = OPTIONS_PROVIDER)
|
||||
@ -83,15 +86,15 @@ public class OnlineGeneratorOptionsTest {
|
||||
outputFilename = Generator.generateClient(provider.getLanguage(), input);
|
||||
}
|
||||
final File dir = new File(new File(outputFilename).getParent());
|
||||
|
||||
|
||||
try {
|
||||
FileUtils.deleteDirectory(dir);
|
||||
} catch (Exception e) { // directory can't be deleted for some reasons
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (InvocationCounter option : options.values()) {
|
||||
assertNotEquals(option.getCounter(), 0, String.format("Option \"%s\" wasn't processed.",
|
||||
option.getValue()));
|
||||
assertNotEquals(option.getCounter(), 0,
|
||||
String.format("Option \"%s\" wasn't processed.", option.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -135,24 +138,27 @@ public class OnlineGeneratorOptionsTest {
|
||||
public static void getOptionsTest(OptionsProvider provider) throws ApiException {
|
||||
final Map<String, CliOption> opts = Generator.getOptions(provider.getLanguage());
|
||||
|
||||
final Function<CliOption, CliOptionProxy> cliOptionWrapper = new Function<CliOption, CliOptionProxy>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public CliOptionProxy apply(@Nullable CliOption option) {
|
||||
return new CliOptionProxy(option);
|
||||
}
|
||||
};
|
||||
final Function<CliOption, CliOptionProxy> 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<CliOption>(opts.values()), cliOptionWrapper);
|
||||
final List<CliOptionProxy> expected = Lists.transform(
|
||||
CodegenConfigLoader.forName(provider.getLanguage()).cliOptions(), cliOptionWrapper);
|
||||
final List<CliOptionProxy> actual =
|
||||
Lists.transform(new ArrayList<CliOption>(opts.values()), cliOptionWrapper);
|
||||
final List<CliOptionProxy> expected =
|
||||
Lists.transform(CodegenConfigLoader.forName(provider.getLanguage()).cliOptions(),
|
||||
cliOptionWrapper);
|
||||
assertEquals(actual, expected);
|
||||
}
|
||||
|
||||
protected static class CliOptionProxy {
|
||||
private final CliOption wrapped;
|
||||
|
||||
public CliOptionProxy(CliOption wrapped){
|
||||
public CliOptionProxy(CliOption wrapped) {
|
||||
this.wrapped = wrapped;
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,8 @@ public class OnlineJavaClientOptionsTest {
|
||||
|
||||
@Test
|
||||
public void getOptionsTest() throws ApiException {
|
||||
final Map<String, CliOption> options = Generator.getOptions(new JavaOptionsProvider().getLanguage());
|
||||
final Map<String, CliOption> options =
|
||||
Generator.getOptions(new JavaOptionsProvider().getLanguage());
|
||||
assertNotNull(options);
|
||||
final CliOption opt = options.get(CodegenConstants.LIBRARY);
|
||||
assertNotNull(opt);
|
||||
|
Loading…
x
Reference in New Issue
Block a user