From 6fbb8ab1e00e837209f566081b04b8196b52c16b Mon Sep 17 00:00:00 2001 From: russellb337 Date: Tue, 11 Aug 2015 12:42:06 -0700 Subject: [PATCH 01/11] add @Generated annotation to all Java class templates --- .../src/main/java/io/swagger/codegen/DefaultGenerator.java | 5 +++++ .../src/main/resources/Java/ApiClient.mustache | 1 + .../src/main/resources/Java/Configuration.mustache | 1 + .../src/main/resources/Java/JsonUtil.mustache | 1 + .../swagger-codegen/src/main/resources/Java/Pair.mustache | 1 + .../src/main/resources/Java/StringUtil.mustache | 1 + modules/swagger-codegen/src/main/resources/Java/api.mustache | 1 + .../src/main/resources/Java/apiException.mustache | 1 + .../src/main/resources/Java/auth/ApiKeyAuth.mustache | 1 + .../src/main/resources/Java/auth/Authentication.mustache | 1 + .../src/main/resources/Java/auth/HttpBasicAuth.mustache | 1 + .../src/main/resources/Java/auth/OAuth.mustache | 1 + .../src/main/resources/Java/generatedAnnotation.mustache | 1 + .../swagger-codegen/src/main/resources/Java/model.mustache | 1 + .../src/main/resources/JavaInflector/api.mustache | 1 + .../resources/JavaInflector/generatedAnnotation.mustache | 1 + .../src/main/resources/JavaInflector/model.mustache | 1 + .../src/main/resources/JavaJaxRS/ApiException.mustache | 1 + .../src/main/resources/JavaJaxRS/ApiOriginFilter.mustache | 1 + .../src/main/resources/JavaJaxRS/ApiResponseMessage.mustache | 1 + .../src/main/resources/JavaJaxRS/NotFoundException.mustache | 1 + .../src/main/resources/JavaJaxRS/api.mustache | 1 + .../src/main/resources/JavaJaxRS/apiService.mustache | 1 + .../src/main/resources/JavaJaxRS/apiServiceFactory.mustache | 1 + .../src/main/resources/JavaJaxRS/apiServiceImpl.mustache | 1 + .../main/resources/JavaJaxRS/generatedAnnotation.mustache | 1 + .../src/main/resources/JavaJaxRS/model.mustache | 1 + .../resources/JavaSpringMVC/generatedAnnotation.mustache | 1 + 28 files changed, 32 insertions(+) create mode 100644 modules/swagger-codegen/src/main/resources/Java/generatedAnnotation.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaInflector/generatedAnnotation.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/generatedAnnotation.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaSpringMVC/generatedAnnotation.mustache diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index 0b6f677857a..fb52d38739d 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -16,6 +16,7 @@ import io.swagger.models.auth.SecuritySchemeDefinition; import io.swagger.util.Json; import org.apache.commons.io.IOUtils; +import org.joda.time.DateTime; import java.io.File; import java.io.FileInputStream; @@ -62,6 +63,10 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { List files = new ArrayList(); try { config.processOpts(); + + config.additionalProperties().put("generatedTime", DateTime.now().toString()); + config.additionalProperties().put("generatorClass", config.getClass().toString()); + if (swagger.getInfo() != null) { Info info = swagger.getInfo(); if (info.getTitle() != null) { diff --git a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache index 79b44594816..6930b548d42 100644 --- a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache @@ -40,6 +40,7 @@ import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.OAuth; +{{>generatedAnnotation}} public class ApiClient { private Map hostMap = new HashMap(); private Map defaultHeaderMap = new HashMap(); diff --git a/modules/swagger-codegen/src/main/resources/Java/Configuration.mustache b/modules/swagger-codegen/src/main/resources/Java/Configuration.mustache index e936b423a91..4629c4e17be 100644 --- a/modules/swagger-codegen/src/main/resources/Java/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/Configuration.mustache @@ -1,5 +1,6 @@ package {{invokerPackage}}; +{{>generatedAnnotation}} public class Configuration { private static ApiClient defaultApiClient = new ApiClient(); diff --git a/modules/swagger-codegen/src/main/resources/Java/JsonUtil.mustache b/modules/swagger-codegen/src/main/resources/Java/JsonUtil.mustache index 29d5f55ecee..4866f8c0fce 100644 --- a/modules/swagger-codegen/src/main/resources/Java/JsonUtil.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/JsonUtil.mustache @@ -7,6 +7,7 @@ import com.fasterxml.jackson.core.JsonGenerator.Feature; import com.fasterxml.jackson.datatype.joda.*; +{{>generatedAnnotation}} public class JsonUtil { public static ObjectMapper mapper; diff --git a/modules/swagger-codegen/src/main/resources/Java/Pair.mustache b/modules/swagger-codegen/src/main/resources/Java/Pair.mustache index 9805c74903b..e2a47317afe 100644 --- a/modules/swagger-codegen/src/main/resources/Java/Pair.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/Pair.mustache @@ -1,5 +1,6 @@ package {{invokerPackage}}; +{{>generatedAnnotation}} public class Pair { private String name = ""; private String value = ""; diff --git a/modules/swagger-codegen/src/main/resources/Java/StringUtil.mustache b/modules/swagger-codegen/src/main/resources/Java/StringUtil.mustache index 035d6739dce..073966b0c21 100644 --- a/modules/swagger-codegen/src/main/resources/Java/StringUtil.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/StringUtil.mustache @@ -1,5 +1,6 @@ package {{invokerPackage}}; +{{>generatedAnnotation}} public class StringUtil { /** * Check if the given array contains the given value (with case-insensitive comparison). diff --git a/modules/swagger-codegen/src/main/resources/Java/api.mustache b/modules/swagger-codegen/src/main/resources/Java/api.mustache index c48c0503f5c..f8464e79fd2 100644 --- a/modules/swagger-codegen/src/main/resources/Java/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/api.mustache @@ -21,6 +21,7 @@ import java.io.File; import java.util.Map; import java.util.HashMap; +{{>generatedAnnotation}} {{#operations}} public class {{classname}} { private ApiClient {{localVariablePrefix}}apiClient; diff --git a/modules/swagger-codegen/src/main/resources/Java/apiException.mustache b/modules/swagger-codegen/src/main/resources/Java/apiException.mustache index 9afe96c6ffb..bb3b53b0aeb 100644 --- a/modules/swagger-codegen/src/main/resources/Java/apiException.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/apiException.mustache @@ -3,6 +3,7 @@ package {{invokerPackage}}; import java.util.Map; import java.util.List; +{{>generatedAnnotation}} public class ApiException extends Exception { private int code = 0; private String message = null; diff --git a/modules/swagger-codegen/src/main/resources/Java/auth/ApiKeyAuth.mustache b/modules/swagger-codegen/src/main/resources/Java/auth/ApiKeyAuth.mustache index a1824b551ca..04be4812292 100644 --- a/modules/swagger-codegen/src/main/resources/Java/auth/ApiKeyAuth.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/auth/ApiKeyAuth.mustache @@ -5,6 +5,7 @@ import {{invokerPackage}}.Pair; import java.util.Map; import java.util.List; +{{>generatedAnnotation}} public class ApiKeyAuth implements Authentication { private final String location; private final String paramName; diff --git a/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache b/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache index 265c74cb76f..037ef58ab90 100644 --- a/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache @@ -5,6 +5,7 @@ import {{invokerPackage}}.Pair; import java.util.Map; import java.util.List; +{{>generatedAnnotation}} public interface Authentication { /** Apply authentication settings to header and query params. */ void applyToParams(List queryParams, Map headerParams); diff --git a/modules/swagger-codegen/src/main/resources/Java/auth/HttpBasicAuth.mustache b/modules/swagger-codegen/src/main/resources/Java/auth/HttpBasicAuth.mustache index 032ea57d4e8..63813e2504e 100644 --- a/modules/swagger-codegen/src/main/resources/Java/auth/HttpBasicAuth.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/auth/HttpBasicAuth.mustache @@ -8,6 +8,7 @@ import java.util.List; import java.io.UnsupportedEncodingException; import javax.xml.bind.DatatypeConverter; +{{>generatedAnnotation}} public class HttpBasicAuth implements Authentication { private String username; private String password; diff --git a/modules/swagger-codegen/src/main/resources/Java/auth/OAuth.mustache b/modules/swagger-codegen/src/main/resources/Java/auth/OAuth.mustache index 66cf2ac8f0f..a1f1b6a827c 100644 --- a/modules/swagger-codegen/src/main/resources/Java/auth/OAuth.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/auth/OAuth.mustache @@ -5,6 +5,7 @@ import {{invokerPackage}}.Pair; import java.util.Map; import java.util.List; +{{>generatedAnnotation}} public class OAuth implements Authentication { @Override public void applyToParams(List queryParams, Map headerParams) { diff --git a/modules/swagger-codegen/src/main/resources/Java/generatedAnnotation.mustache b/modules/swagger-codegen/src/main/resources/Java/generatedAnnotation.mustache new file mode 100644 index 00000000000..49110fc1ad9 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/generatedAnnotation.mustache @@ -0,0 +1 @@ +@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}") \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/Java/model.mustache b/modules/swagger-codegen/src/main/resources/Java/model.mustache index 0e12f514c48..39f0f0b6fa1 100644 --- a/modules/swagger-codegen/src/main/resources/Java/model.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/model.mustache @@ -12,6 +12,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; * {{description}} **/{{/description}} @ApiModel(description = "{{{description}}}") +{{>generatedAnnotation}} public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { {{#vars}}{{#isEnum}} public enum {{datatypeWithEnum}} { diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache index 5f5b5b1df9a..829a9daeabc 100644 --- a/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache @@ -13,6 +13,7 @@ import {{modelPackage}}.*; {{#imports}}import {{import}}; {{/imports}} +{{>generatedAnnotation}} {{#operations}} public class {{classname}} { diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/generatedAnnotation.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/generatedAnnotation.mustache new file mode 100644 index 00000000000..49110fc1ad9 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/generatedAnnotation.mustache @@ -0,0 +1 @@ +@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}") \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/model.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/model.mustache index 300d5e61dd9..db2480a8540 100644 --- a/modules/swagger-codegen/src/main/resources/JavaInflector/model.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/model.mustache @@ -12,6 +12,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; * {{description}} **/{{/description}} @ApiModel(description = "{{{description}}}") +{{>generatedAnnotation}} public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { {{#vars}}{{#isEnum}} public enum {{datatypeWithEnum}} { diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiException.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiException.mustache index ffab3b1088e..11b4036b832 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiException.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiException.mustache @@ -1,5 +1,6 @@ package {{apiPackage}}; +{{>generatedAnnotation}} public class ApiException extends Exception{ private int code; public ApiException (int code, String msg) { diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiOriginFilter.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiOriginFilter.mustache index 68675432c64..5db3301b3d9 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiOriginFilter.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiOriginFilter.mustache @@ -5,6 +5,7 @@ import java.io.IOException; import javax.servlet.*; import javax.servlet.http.HttpServletResponse; +{{>generatedAnnotation}} public class ApiOriginFilter implements javax.servlet.Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiResponseMessage.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiResponseMessage.mustache index 94711b26efb..2b9a2b1f8c5 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiResponseMessage.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/ApiResponseMessage.mustache @@ -3,6 +3,7 @@ package {{apiPackage}}; import javax.xml.bind.annotation.XmlTransient; @javax.xml.bind.annotation.XmlRootElement +{{>generatedAnnotation}} public class ApiResponseMessage { public static final int ERROR = 1; public static final int WARNING = 2; diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/NotFoundException.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/NotFoundException.mustache index 8ab2c99e4f8..1bd5e207d7b 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/NotFoundException.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/NotFoundException.mustache @@ -1,5 +1,6 @@ package {{apiPackage}}; +{{>generatedAnnotation}} public class NotFoundException extends ApiException { private int code; public NotFoundException (int code, String msg) { diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache index 3216b60d516..9e1f808806e 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache @@ -26,6 +26,7 @@ import javax.ws.rs.*; {{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}} {{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}} @io.swagger.annotations.Api(value = "/{{baseName}}", description = "the {{baseName}} API") +{{>generatedAnnotation}} {{#operations}} public class {{classname}} { diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiService.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiService.mustache index 43e7cd8685c..a731da6c11c 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiService.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiService.mustache @@ -18,6 +18,7 @@ import com.sun.jersey.multipart.FormDataParam; import javax.ws.rs.core.Response; +{{>generatedAnnotation}} {{#operations}} public abstract class {{classname}}Service { {{#operation}} diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiServiceFactory.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiServiceFactory.mustache index a434311d285..1bb63bc0775 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiServiceFactory.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiServiceFactory.mustache @@ -3,6 +3,7 @@ package {{package}}.factories; import {{package}}.{{classname}}Service; import {{package}}.impl.{{classname}}ServiceImpl; +{{>generatedAnnotation}} public class {{classname}}ServiceFactory { private final static {{classname}}Service service = new {{classname}}ServiceImpl(); diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiServiceImpl.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiServiceImpl.mustache index d49fa4952a2..0acd755745e 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiServiceImpl.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/apiServiceImpl.mustache @@ -18,6 +18,7 @@ import com.sun.jersey.multipart.FormDataParam; import javax.ws.rs.core.Response; +{{>generatedAnnotation}} {{#operations}} public class {{classname}}ServiceImpl extends {{classname}}Service { {{#operation}} diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/generatedAnnotation.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/generatedAnnotation.mustache new file mode 100644 index 00000000000..49110fc1ad9 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/generatedAnnotation.mustache @@ -0,0 +1 @@ +@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}") \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/model.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/model.mustache index 300d5e61dd9..db2480a8540 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/model.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/model.mustache @@ -12,6 +12,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; * {{description}} **/{{/description}} @ApiModel(description = "{{{description}}}") +{{>generatedAnnotation}} public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { {{#vars}}{{#isEnum}} public enum {{datatypeWithEnum}} { diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/generatedAnnotation.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/generatedAnnotation.mustache new file mode 100644 index 00000000000..49110fc1ad9 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/generatedAnnotation.mustache @@ -0,0 +1 @@ +@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}") \ No newline at end of file From 38fccbd73d32effa6027259c110c3ea462a5386a Mon Sep 17 00:00:00 2001 From: russellb337 Date: Tue, 11 Aug 2015 12:42:18 -0700 Subject: [PATCH 02/11] add @Generated to all Java class templates --- .../src/main/resources/JavaSpringMVC/api.mustache | 1 + .../src/main/resources/JavaSpringMVC/apiException.mustache | 1 + .../src/main/resources/JavaSpringMVC/apiOriginFilter.mustache | 1 + .../src/main/resources/JavaSpringMVC/apiResponseMessage.mustache | 1 + .../src/main/resources/JavaSpringMVC/model.mustache | 1 + .../src/main/resources/JavaSpringMVC/notFoundException.mustache | 1 + .../src/main/resources/JavaSpringMVC/swaggerConfig.mustache | 1 + .../main/resources/JavaSpringMVC/swaggerUiConfiguration.mustache | 1 + .../src/main/resources/JavaSpringMVC/webApplication.mustache | 1 + .../main/resources/JavaSpringMVC/webMvcConfiguration.mustache | 1 + 10 files changed, 10 insertions(+) diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api.mustache index d6a24b69311..e6e8d6fc04c 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api.mustache @@ -30,6 +30,7 @@ import static org.springframework.http.MediaType.*; @Controller @RequestMapping(value = "/{{baseName}}", produces = {APPLICATION_JSON_VALUE}) @Api(value = "/{{baseName}}", description = "the {{baseName}} API") +{{>generatedAnnotation}} {{#operations}} public class {{classname}} { {{#operation}} diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiException.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiException.mustache index ffab3b1088e..11b4036b832 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiException.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiException.mustache @@ -1,5 +1,6 @@ package {{apiPackage}}; +{{>generatedAnnotation}} public class ApiException extends Exception{ private int code; public ApiException (int code, String msg) { diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiOriginFilter.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiOriginFilter.mustache index 68675432c64..5db3301b3d9 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiOriginFilter.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiOriginFilter.mustache @@ -5,6 +5,7 @@ import java.io.IOException; import javax.servlet.*; import javax.servlet.http.HttpServletResponse; +{{>generatedAnnotation}} public class ApiOriginFilter implements javax.servlet.Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiResponseMessage.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiResponseMessage.mustache index 94711b26efb..2b9a2b1f8c5 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiResponseMessage.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/apiResponseMessage.mustache @@ -3,6 +3,7 @@ package {{apiPackage}}; import javax.xml.bind.annotation.XmlTransient; @javax.xml.bind.annotation.XmlRootElement +{{>generatedAnnotation}} public class ApiResponseMessage { public static final int ERROR = 1; public static final int WARNING = 2; diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/model.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/model.mustache index 300d5e61dd9..db2480a8540 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/model.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/model.mustache @@ -12,6 +12,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; * {{description}} **/{{/description}} @ApiModel(description = "{{{description}}}") +{{>generatedAnnotation}} public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { {{#vars}}{{#isEnum}} public enum {{datatypeWithEnum}} { diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/notFoundException.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/notFoundException.mustache index 8ab2c99e4f8..1bd5e207d7b 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/notFoundException.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/notFoundException.mustache @@ -1,5 +1,6 @@ package {{apiPackage}}; +{{>generatedAnnotation}} public class NotFoundException extends ApiException { private int code; public NotFoundException (int code, String msg) { diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/swaggerConfig.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/swaggerConfig.mustache index b33ee7fc994..4a6e6879df3 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/swaggerConfig.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/swaggerConfig.mustache @@ -18,6 +18,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 //Loads the spring beans required by the framework @PropertySource("classpath:swagger.properties") @Import(SwaggerUiConfiguration.class) +{{>generatedAnnotation}} public class SwaggerConfig { @Bean ApiInfo apiInfo() { diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/swaggerUiConfiguration.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/swaggerUiConfiguration.mustache index 652dc310358..0b515fe2cc8 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/swaggerUiConfiguration.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/swaggerUiConfiguration.mustache @@ -8,6 +8,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter @Configuration @EnableWebMvc +{{>generatedAnnotation}} public class SwaggerUiConfiguration extends WebMvcConfigurerAdapter { private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" }; diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/webApplication.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/webApplication.mustache index 6910ad11b76..426f831582e 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/webApplication.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/webApplication.mustache @@ -2,6 +2,7 @@ package {{configPackage}}; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; +{{>generatedAnnotation}} public class WebApplication extends AbstractAnnotationConfigDispatcherServletInitializer { @Override diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/webMvcConfiguration.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/webMvcConfiguration.mustache index 03904e51e79..d60c126316e 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/webMvcConfiguration.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/webMvcConfiguration.mustache @@ -3,6 +3,7 @@ package {{configPackage}}; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; +{{>generatedAnnotation}} public class WebMvcConfiguration extends WebMvcConfigurationSupport { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { From a0eeb90a97b2a19ccc8555756cbf12af8ceea256 Mon Sep 17 00:00:00 2001 From: russellb337 Date: Tue, 11 Aug 2015 12:50:19 -0700 Subject: [PATCH 03/11] fixing bug --- .../src/main/java/io/swagger/codegen/DefaultGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index fb52d38739d..a2f32706a0e 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -64,7 +64,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { try { config.processOpts(); - config.additionalProperties().put("generatedTime", DateTime.now().toString()); + config.additionalProperties().put("generatedDate", DateTime.now().toString()); config.additionalProperties().put("generatorClass", config.getClass().toString()); if (swagger.getInfo() != null) { From b14edffc795083ea13ff35e733069989ac042744 Mon Sep 17 00:00:00 2001 From: David Biesack Date: Thu, 13 Aug 2015 13:46:32 -0400 Subject: [PATCH 04/11] improve static html; add template guards for collections. Update -core dependency --- .../io/swagger/codegen/CodegenOperation.java | 17 ++ .../main/resources/htmlDocs/index.mustache | 149 +++++++++++++++--- .../resources/htmlDocs/style.css.mustache | 8 + pom.xml | 2 +- 4 files changed, 152 insertions(+), 24 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenOperation.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenOperation.java index b795f8e38e2..4f30cc01ccb 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenOperation.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenOperation.java @@ -30,6 +30,23 @@ public class CodegenOperation { public List> examples; public ExternalDocs externalDocs; + private boolean nonempty(List params) + { + return params != null && params.size() > 0; + } + public boolean getHasBodyParam() { + return nonempty(bodyParams); + } + public boolean getHasQueryParams() { + return nonempty(bodyParams); + } + public boolean getHasHeaderParams() { + return nonempty(bodyParams); + } + public boolean getHasPathParams() { + return nonempty(bodyParams); + } + // legacy support public String nickname; } diff --git a/modules/swagger-codegen/src/main/resources/htmlDocs/index.mustache b/modules/swagger-codegen/src/main/resources/htmlDocs/index.mustache index ce881084bc2..584db3b7a85 100644 --- a/modules/swagger-codegen/src/main/resources/htmlDocs/index.mustache +++ b/modules/swagger-codegen/src/main/resources/htmlDocs/index.mustache @@ -1,52 +1,158 @@ - + + + - - API Reference + {{{appName}}} + +

{{{appName}}}

-
{{{appDescription}}} for {{partner}}
+
{{{appDescription}}}
{{#infoUrl}}
More information: {{{infoUrl}}}
{{/infoUrl}} {{#infoEmail}}
Contact Info: {{{infoEmail}}}
{{/infoEmail}} {{#version}}
Version: {{{version}}}
{{/version}}
{{{licenseInfo}}}
{{{licenseUrl}}}

Access

-
Customize this message as you see fit!
-

Methods

+ {{access}} + +

Methods

+ [ Jump to Models ] + + {{! for the tables of content, I cheat and don't use CSS styles.... }} +

Table of Contents

+
{{access}}
+ {{#apiInfo}} +
    + {{#apis}} + {{#operations}} + {{#operation}} +
  1. {{httpMethod}} {{path}}
  2. + {{/operation}} + {{/operations}} + {{/apis}} +
+ {{/apiInfo}} + {{#apiInfo}} {{#apis}} - {{#operations}}{{#operation}} -
-
{{httpMethod}}: {{path}}
-
{{#tags}}{{this}}{{/tags}}
-
{{nickname}} {{summary}}
+ {{#operations}} + {{#operation}} +
+
+ Up +
{{httpMethod}} {{path}}
+
{{summary}} ({{nickname}})
+ {{! notes is operation.description. So why rename it and make it super confusing???? }}
{{notes}}
-

Parameters

+ {{#hasPathParams}} +

Path parameters

- {{#allParams}}{{>queryParam}}{{>pathParam}}{{>bodyParam}}{{>headerParam}}{{>formParam}} - {{/allParams}} + {{#pathParams}}{{>pathParam}}{{/pathParams}}
+ {{/hasPathParams}} + + {{#hasConsumes}} +

Consumes

+ This API call consumes the following media types via the Content-Type request header: +
    + {{#consumes}} +
  • {{mediaType}}
  • + {{/consumes}} +
+ {{/hasConsumes}} + + {{#hasBodyParam}} +

Request body

+
+ {{#bodyParams}}{{>bodyParam}}{{/bodyParams}} +
+ {{/hasBodyParam}} + + {{#hasHeaderParam}} +

Request headers

+
+ {{#headerParam}}{{>headerParam}}{{/headerParam}} +
+ {{/hasHeaderParam}} + + {{#hasQueryParams}} +

Query parameters

+
+ {{#queryParams}}{{>queryParam}}{{/queryParams}} +
+ {{/hasQueryParams}} + + + + {{#hasExamples}} + {{#examples}} +

Example data

+
Content-Type: {{{contentType}}}
+
{{{example}}}
+ {{/examples}} + {{/hasExamples}} + + {{#hasProduces}} +

Produces

+ This API call produces the following media types according to the Accept request header; + the media type will be conveyed by the Content-Type response header. +
    + {{#produces}} +
  • {{mediaType}}
  • + {{/produces}} +
+ {{/hasProduces}} + +

Responses

+ {{#responses}} +

{{code}}

+ {{message}} {{#examples}}

Example data

Content-Type: {{{contentType}}}
{{example}}
{{/examples}} + {{/responses}}
-
- {{/operation}}{{/operations}} - {{/apis}}{{/apiInfo}} +
+ {{/operation}} + {{/operations}} + {{/apis}} + {{/apiInfo}} + + +

Models

+ [ Jump to Methods ] + +

Table of Contents

+
    + {{#models}} + {{#model}} +
  1. {{classname}}
  2. + {{/model}} + {{/models}} +
-

Models

{{#models}} {{#model}}
-

{{classname}}

+

{{classname}} Up

{{#vars}}
{{name}} {{#isNotRequired}}(optional){{/isNotRequired}}
{{datatype}} {{description}}
{{/vars}} @@ -54,8 +160,5 @@
{{/model}} {{/models}} - \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/htmlDocs/style.css.mustache b/modules/swagger-codegen/src/main/resources/htmlDocs/style.css.mustache index 14ab06a7f7e..d69f0e7b161 100644 --- a/modules/swagger-codegen/src/main/resources/htmlDocs/style.css.mustache +++ b/modules/swagger-codegen/src/main/resources/htmlDocs/style.css.mustache @@ -57,6 +57,10 @@ pre { margin-bottom: 2px; } +.http-method { + text-transform: uppercase; +} + pre.get { background-color: #0f6ab4; } @@ -96,6 +100,10 @@ code { background-color: #0f6ab4; } +.up { + float:right; +} + .parameter { width: 500px; } diff --git a/pom.xml b/pom.xml index a7e35de01c3..24d7de3b5a2 100644 --- a/pom.xml +++ b/pom.xml @@ -459,7 +459,7 @@ 1.0.10-SNAPSHOT 2.11.1 2.3.4 - 1.5.3-SNAPSHOT + 1.5.4-SNAPSHOT 2.1.4 2.3 1.2 From 894d571ea5b04a2b57fc4b404601abbb1565cc7f Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 19 Aug 2015 17:48:04 +0800 Subject: [PATCH 05/11] Change reserved word handling in python client. * First, remove the beginning underscores. * Then, append underscore if the var is reserved word or number. --- .../codegen/languages/PythonClientCodegen.java | 8 ++++---- .../main/resources/python/api_client.mustache | 8 +++----- .../resources/python/configuration.mustache | 4 ++-- .../src/main/resources/python/model.mustache | 17 ++++++++--------- .../python/swagger_client/api_client.py | 8 +++----- .../python/swagger_client/configuration.py | 4 ++-- .../python/swagger_client/models/category.py | 17 ++++++++--------- .../python/swagger_client/models/order.py | 17 ++++++++--------- .../python/swagger_client/models/pet.py | 17 ++++++++--------- .../python/swagger_client/models/tag.py | 17 ++++++++--------- .../python/swagger_client/models/user.py | 17 ++++++++--------- 11 files changed, 62 insertions(+), 72 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java index e0e1bdc07f8..61dc431b8a7 100755 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java @@ -119,7 +119,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig @Override public String escapeReservedWord(String name) { - return name + "_"; + return "_" + name; } @Override @@ -179,14 +179,14 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig // petId => pet_id name = underscore(dropDots(name)); + // remove leading underscore + name = name.replaceAll("^_*", ""); + // for reserved word or word starting with number, append _ if (reservedWords.contains(name) || name.matches("^\\d.*")) { name = escapeReservedWord(name); } - // remove leading underscore - name = name.replaceAll("^_*", ""); - return name; } diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index ca9a3c05104..7493907d9e8 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -202,11 +202,9 @@ class ApiClient(object): # and attributes which value is not None. # Convert attribute name to json key in # model definition for request. - obj_dict = {obj.attribute_map[key[1:]]: val - for key, val in iteritems(obj.__dict__) - if key != 'swagger_types' - and key != 'attribute_map' - and val is not None} + obj_dict = {obj.attribute_map[attr]: getattr(obj, attr) + for attr, _ in iteritems(obj.swagger_types) + if getattr(obj, attr) is not None} return {key: self.sanitize_for_serialization(val) for key, val in iteritems(obj_dict)} diff --git a/modules/swagger-codegen/src/main/resources/python/configuration.mustache b/modules/swagger-codegen/src/main/resources/python/configuration.mustache index 62633cdc4a1..d6d94ff5a50 100644 --- a/modules/swagger-codegen/src/main/resources/python/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/python/configuration.mustache @@ -23,9 +23,9 @@ import urllib3 try: import httplib except ImportError: - # python3 + # for python3 import http.client as httplib - + import sys import logging diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index c810655e382..4a93aeaf29d 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -86,18 +86,17 @@ class {{classname}}(object): """ result = {} - for name, prop in iteritems(self.__dict__): - if name == "attribute_map" or name == "swagger_types": - continue - if isinstance(prop, list): - result[name[1:]] = list(map( + for attr, _ in iteritems(self.swagger_types): + value = getattr(self, attr) + if isinstance(value, list): + result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, - prop + value )) - elif hasattr(prop, "to_dict"): - result[name[1:]] = prop.to_dict() + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() else: - result[name[1:]] = prop + result[attr] = value return result diff --git a/samples/client/petstore/python/swagger_client/api_client.py b/samples/client/petstore/python/swagger_client/api_client.py index ca9a3c05104..7493907d9e8 100644 --- a/samples/client/petstore/python/swagger_client/api_client.py +++ b/samples/client/petstore/python/swagger_client/api_client.py @@ -202,11 +202,9 @@ class ApiClient(object): # and attributes which value is not None. # Convert attribute name to json key in # model definition for request. - obj_dict = {obj.attribute_map[key[1:]]: val - for key, val in iteritems(obj.__dict__) - if key != 'swagger_types' - and key != 'attribute_map' - and val is not None} + obj_dict = {obj.attribute_map[attr]: getattr(obj, attr) + for attr, _ in iteritems(obj.swagger_types) + if getattr(obj, attr) is not None} return {key: self.sanitize_for_serialization(val) for key, val in iteritems(obj_dict)} diff --git a/samples/client/petstore/python/swagger_client/configuration.py b/samples/client/petstore/python/swagger_client/configuration.py index 8f469b72653..6bc58c90aea 100644 --- a/samples/client/petstore/python/swagger_client/configuration.py +++ b/samples/client/petstore/python/swagger_client/configuration.py @@ -23,9 +23,9 @@ import urllib3 try: import httplib except ImportError: - # python3 + # for python3 import http.client as httplib - + import sys import logging diff --git a/samples/client/petstore/python/swagger_client/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py index 615e65ea839..bf70a7d10fd 100644 --- a/samples/client/petstore/python/swagger_client/models/category.py +++ b/samples/client/petstore/python/swagger_client/models/category.py @@ -97,18 +97,17 @@ class Category(object): """ result = {} - for name, prop in iteritems(self.__dict__): - if name == "attribute_map" or name == "swagger_types": - continue - if isinstance(prop, list): - result[name[1:]] = list(map( + for attr, _ in iteritems(self.swagger_types): + value = getattr(self, attr) + if isinstance(value, list): + result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, - prop + value )) - elif hasattr(prop, "to_dict"): - result[name[1:]] = prop.to_dict() + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() else: - result[name[1:]] = prop + result[attr] = value return result diff --git a/samples/client/petstore/python/swagger_client/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py index a381853b91f..1a7612df190 100644 --- a/samples/client/petstore/python/swagger_client/models/order.py +++ b/samples/client/petstore/python/swagger_client/models/order.py @@ -203,18 +203,17 @@ class Order(object): """ result = {} - for name, prop in iteritems(self.__dict__): - if name == "attribute_map" or name == "swagger_types": - continue - if isinstance(prop, list): - result[name[1:]] = list(map( + for attr, _ in iteritems(self.swagger_types): + value = getattr(self, attr) + if isinstance(value, list): + result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, - prop + value )) - elif hasattr(prop, "to_dict"): - result[name[1:]] = prop.to_dict() + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() else: - result[name[1:]] = prop + result[attr] = value return result diff --git a/samples/client/petstore/python/swagger_client/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py index 57dc93387a9..cda4fa1e8cd 100644 --- a/samples/client/petstore/python/swagger_client/models/pet.py +++ b/samples/client/petstore/python/swagger_client/models/pet.py @@ -203,18 +203,17 @@ class Pet(object): """ result = {} - for name, prop in iteritems(self.__dict__): - if name == "attribute_map" or name == "swagger_types": - continue - if isinstance(prop, list): - result[name[1:]] = list(map( + for attr, _ in iteritems(self.swagger_types): + value = getattr(self, attr) + if isinstance(value, list): + result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, - prop + value )) - elif hasattr(prop, "to_dict"): - result[name[1:]] = prop.to_dict() + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() else: - result[name[1:]] = prop + result[attr] = value return result diff --git a/samples/client/petstore/python/swagger_client/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py index bfa389520a3..8108052aae3 100644 --- a/samples/client/petstore/python/swagger_client/models/tag.py +++ b/samples/client/petstore/python/swagger_client/models/tag.py @@ -97,18 +97,17 @@ class Tag(object): """ result = {} - for name, prop in iteritems(self.__dict__): - if name == "attribute_map" or name == "swagger_types": - continue - if isinstance(prop, list): - result[name[1:]] = list(map( + for attr, _ in iteritems(self.swagger_types): + value = getattr(self, attr) + if isinstance(value, list): + result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, - prop + value )) - elif hasattr(prop, "to_dict"): - result[name[1:]] = prop.to_dict() + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() else: - result[name[1:]] = prop + result[attr] = value return result diff --git a/samples/client/petstore/python/swagger_client/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py index 7380bc94582..576473adcfa 100644 --- a/samples/client/petstore/python/swagger_client/models/user.py +++ b/samples/client/petstore/python/swagger_client/models/user.py @@ -247,18 +247,17 @@ class User(object): """ result = {} - for name, prop in iteritems(self.__dict__): - if name == "attribute_map" or name == "swagger_types": - continue - if isinstance(prop, list): - result[name[1:]] = list(map( + for attr, _ in iteritems(self.swagger_types): + value = getattr(self, attr) + if isinstance(value, list): + result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, - prop + value )) - elif hasattr(prop, "to_dict"): - result[name[1:]] = prop.to_dict() + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() else: - result[name[1:]] = prop + result[attr] = value return result From 2b613055822187b1fb3d21c4618d6c167c836fac Mon Sep 17 00:00:00 2001 From: Andrew B Date: Thu, 20 Aug 2015 14:16:10 -0700 Subject: [PATCH 06/11] Fixing issue #1089 --- .../codegen/languages/JavaClientCodegen.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java index 678cce9263a..b9e298b125c 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java @@ -92,7 +92,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { @Override public void processOpts() { super.processOpts(); - + if (additionalProperties.containsKey("invokerPackage")) { this.setInvokerPackage((String) additionalProperties.get("invokerPackage")); } else { @@ -130,6 +130,8 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { this.setLocalVariablePrefix((String) additionalProperties.get("localVariablePrefix")); } + this.sanitizeConfig(); + final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator); supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); supportingFiles.add(new SupportingFile("ApiClient.mustache", invokerFolder, "ApiClient.java")); @@ -146,7 +148,26 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java")); } - + private void sanitizeConfig() { + // Sanitize any config options here. We also have to update the additionalProperties because + // the whole additionalProperties object is injected into the main object passed to the mustache layer + + this.setApiPackage(sanitizePackageName(apiPackage)); + if (additionalProperties.containsKey("apiPackage")) { + this.additionalProperties.put("apiPackage", apiPackage); + } + + this.setModelPackage(sanitizePackageName(modelPackage)); + if (additionalProperties.containsKey("modelPackage")) { + this.additionalProperties.put("modelPackage", modelPackage); + } + + this.setInvokerPackage(sanitizePackageName(invokerPackage)); + if (additionalProperties.containsKey("invokerPackage")) { + this.additionalProperties.put("invokerPackage", invokerPackage); + } + } + @Override public String escapeReservedWord(String name) { return "_" + name; @@ -342,4 +363,14 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { public void setLocalVariablePrefix(String localVariablePrefix) { this.localVariablePrefix = localVariablePrefix; } + + private String sanitizePackageName(String packageName) { + packageName = packageName.trim(); + packageName = packageName.replaceAll("[^a-zA-Z0-9_\\.]", "_"); + if(Strings.isNullOrEmpty(packageName)) { + return "invalidPackageName"; + } + return packageName; + } + } From b7cd38d38b28f6be093596b199d5e0af8b15055e Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 21 Aug 2015 09:42:31 +0800 Subject: [PATCH 07/11] Update objc client. Sanitize request parameters (path, query, header, form, body). --- .../resources/objc/ApiClient-body.mustache | 63 +++++++++- .../resources/objc/ApiClient-header.mustache | 9 ++ .../src/main/resources/objc/api-body.mustache | 24 +--- .../objc/SwaggerClient/SWGApiClient.h | 9 ++ .../objc/SwaggerClient/SWGApiClient.m | 63 +++++++++- .../petstore/objc/SwaggerClient/SWGPet.h | 2 +- .../petstore/objc/SwaggerClient/SWGPetApi.m | 82 ++++++------- .../petstore/objc/SwaggerClient/SWGStoreApi.m | 40 +++---- .../petstore/objc/SwaggerClient/SWGUserApi.m | 108 ++++++------------ 9 files changed, 231 insertions(+), 169 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache index a7d290022f6..b7643de6ddd 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache @@ -467,6 +467,7 @@ static void (^reachabilityChangeBlock)(int); -(NSNumber*) requestWithCompletionBlock: (NSString*) path method: (NSString*) method + pathParams: (NSDictionary *) pathParams queryParams: (NSDictionary*) queryParams formParams: (NSDictionary *) formParams files: (NSDictionary *) files @@ -499,12 +500,25 @@ static void (^reachabilityChangeBlock)(int); self.responseSerializer = [AFHTTPResponseSerializer serializer]; } + // sanitize parameters + pathParams = [self sanitizeForSerialization:pathParams]; + queryParams = [self sanitizeForSerialization:queryParams]; + headerParams = [self sanitizeForSerialization:headerParams]; + formParams = [self sanitizeForSerialization:formParams]; + body = [self sanitizeForSerialization:body]; + // auth setting [self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings]; + NSMutableString *resourcePath = [NSMutableString stringWithString:path]; + [pathParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + [resourcePath replaceCharactersInRange:[resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", key, @"}"]] + withString:[SWGApiClient escape:obj]]; + }]; + NSMutableURLRequest * request = nil; - NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; + NSString* pathWithQueryParams = [self pathWithQueryParamsToString:resourcePath queryParams:queryParams]; if ([pathWithQueryParams hasPrefix:@"/"]) { pathWithQueryParams = [pathWithQueryParams substringFromIndex:1]; } @@ -540,20 +554,21 @@ static void (^reachabilityChangeBlock)(int); } } + // request cache BOOL hasHeaderParams = false; if(headerParams != nil && [headerParams count] > 0) { hasHeaderParams = true; } if(offlineState) { - NSLog(@"%@ cache forced", path); + NSLog(@"%@ cache forced", resourcePath); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) { - NSLog(@"%@ cache enabled", path); + NSLog(@"%@ cache enabled", resourcePath); [request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; } else { - NSLog(@"%@ cache disabled", path); + NSLog(@"%@ cache disabled", resourcePath); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } @@ -671,4 +686,44 @@ static void (^reachabilityChangeBlock)(int); *querys = [NSDictionary dictionaryWithDictionary:querysWithAuth]; } +- (id) sanitizeForSerialization:(id) object { + if (object == nil) { + return nil; + } + else if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]] || [object isKindOfClass:[SWGQueryParamCollection class]]) { + return object; + } + else if ([object isKindOfClass:[NSDate class]]) { + return [object ISO8601String]; + } + else if ([object isKindOfClass:[NSArray class]]) { + NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[object count]]; + [object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + if (obj) { + [sanitizedObjs addObject:[self sanitizeForSerialization:obj]]; + } + }]; + return sanitizedObjs; + } + else if ([object isKindOfClass:[NSDictionary class]]) { + NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[object count]]; + [object enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + if (obj) { + [sanitizedObjs setValue:[self sanitizeForSerialization:obj] forKey:key]; + } + }]; + return sanitizedObjs; + } + else if ([object isKindOfClass:[SWGObject class]]) { + return [object toDictionary]; + } + else { + NSException *e = [NSException + exceptionWithName:@"InvalidObjectArgumentException" + reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", object] + userInfo:nil]; + @throw e; + } +} + @end diff --git a/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache b/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache index 46d424d735f..eba62705879 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache @@ -168,6 +168,7 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; * * @param path Request url. * @param method Request method. + * @param pathParams Request path parameters. * @param queryParams Request query parameters. * @param body Request body. * @param headerParams Request header parameters. @@ -180,6 +181,7 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; */ -(NSNumber*) requestWithCompletionBlock:(NSString*) path method:(NSString*) method + pathParams:(NSDictionary *) pathParams queryParams:(NSDictionary*) queryParams formParams:(NSDictionary *) formParams files:(NSDictionary *) files @@ -191,4 +193,11 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; responseType:(NSString *) responseType completionBlock:(void (^)(id, NSError *))completionBlock; +/** + * Sanitize object for request + * + * @param object The query/path/header/form/body param to be sanitized. + */ +- (id) sanitizeForSerialization:(id) object; + @end diff --git a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache index 198c5a89fdf..a47b39c22cd 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache @@ -88,8 +88,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - {{#pathParams}}[resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"{{baseName}}", @"}"]] withString: [{{classPrefix}}ApiClient escape:{{paramName}}]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + {{#pathParams}}if ({{paramName}} != nil) { + pathParams[@"{{baseName}}"] = {{paramName}}; + } {{/pathParams}} NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -132,22 +135,6 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; {{#bodyParam}} bodyParam = {{paramName}}; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[({{classPrefix}}Object*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [({{classPrefix}}Object*)bodyParam toDictionary]; - } {{/bodyParam}}{{^bodyParam}} {{#formParams}} {{#notFile}} @@ -167,6 +154,7 @@ {{/requiredParamCount}} return [self.apiClient requestWithCompletionBlock: resourcePath method: @"{{httpMethod}}" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h index 7802a5b46c0..84f28faa8c6 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h @@ -172,6 +172,7 @@ extern NSString *const SWGResponseObjectErrorKey; * * @param path Request url. * @param method Request method. + * @param pathParams Request path parameters. * @param queryParams Request query parameters. * @param body Request body. * @param headerParams Request header parameters. @@ -184,6 +185,7 @@ extern NSString *const SWGResponseObjectErrorKey; */ -(NSNumber*) requestWithCompletionBlock:(NSString*) path method:(NSString*) method + pathParams:(NSDictionary *) pathParams queryParams:(NSDictionary*) queryParams formParams:(NSDictionary *) formParams files:(NSDictionary *) files @@ -195,4 +197,11 @@ extern NSString *const SWGResponseObjectErrorKey; responseType:(NSString *) responseType completionBlock:(void (^)(id, NSError *))completionBlock; +/** + * Sanitize object for request + * + * @param object The query/path/header/form/body param to be sanitized. + */ +- (id) sanitizeForSerialization:(id) object; + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m index 22b5c256402..906169f4d12 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m @@ -467,6 +467,7 @@ static void (^reachabilityChangeBlock)(int); -(NSNumber*) requestWithCompletionBlock: (NSString*) path method: (NSString*) method + pathParams: (NSDictionary *) pathParams queryParams: (NSDictionary*) queryParams formParams: (NSDictionary *) formParams files: (NSDictionary *) files @@ -499,12 +500,25 @@ static void (^reachabilityChangeBlock)(int); self.responseSerializer = [AFHTTPResponseSerializer serializer]; } + // sanitize parameters + pathParams = [self sanitizeForSerialization:pathParams]; + queryParams = [self sanitizeForSerialization:queryParams]; + headerParams = [self sanitizeForSerialization:headerParams]; + formParams = [self sanitizeForSerialization:formParams]; + body = [self sanitizeForSerialization:body]; + // auth setting [self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings]; + NSMutableString *resourcePath = [NSMutableString stringWithString:path]; + [pathParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + [resourcePath replaceCharactersInRange:[resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", key, @"}"]] + withString:[SWGApiClient escape:obj]]; + }]; + NSMutableURLRequest * request = nil; - NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; + NSString* pathWithQueryParams = [self pathWithQueryParamsToString:resourcePath queryParams:queryParams]; if ([pathWithQueryParams hasPrefix:@"/"]) { pathWithQueryParams = [pathWithQueryParams substringFromIndex:1]; } @@ -540,20 +554,21 @@ static void (^reachabilityChangeBlock)(int); } } + // request cache BOOL hasHeaderParams = false; if(headerParams != nil && [headerParams count] > 0) { hasHeaderParams = true; } if(offlineState) { - NSLog(@"%@ cache forced", path); + NSLog(@"%@ cache forced", resourcePath); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) { - NSLog(@"%@ cache enabled", path); + NSLog(@"%@ cache enabled", resourcePath); [request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; } else { - NSLog(@"%@ cache disabled", path); + NSLog(@"%@ cache disabled", resourcePath); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } @@ -671,4 +686,44 @@ static void (^reachabilityChangeBlock)(int); *querys = [NSDictionary dictionaryWithDictionary:querysWithAuth]; } +- (id) sanitizeForSerialization:(id) object { + if (object == nil) { + return nil; + } + else if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]] || [object isKindOfClass:[SWGQueryParamCollection class]]) { + return object; + } + else if ([object isKindOfClass:[NSDate class]]) { + return [object ISO8601String]; + } + else if ([object isKindOfClass:[NSArray class]]) { + NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[object count]]; + [object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + if (obj) { + [sanitizedObjs addObject:[self sanitizeForSerialization:obj]]; + } + }]; + return sanitizedObjs; + } + else if ([object isKindOfClass:[NSDictionary class]]) { + NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[object count]]; + [object enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + if (obj) { + [sanitizedObjs setValue:[self sanitizeForSerialization:obj] forKey:key]; + } + }]; + return sanitizedObjs; + } + else if ([object isKindOfClass:[SWGObject class]]) { + return [object toDictionary]; + } + else { + NSException *e = [NSException + exceptionWithName:@"InvalidObjectArgumentException" + reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", object] + userInfo:nil]; + @throw e; + } +} + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPet.h b/samples/client/petstore/objc/SwaggerClient/SWGPet.h index e340e0e2b86..84f10969e5b 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPet.h +++ b/samples/client/petstore/objc/SwaggerClient/SWGPet.h @@ -7,8 +7,8 @@ * Do not edit the class manually. */ -#import "SWGTag.h" #import "SWGCategory.h" +#import "SWGTag.h" @protocol SWGPet diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m index f2e7fb64030..4f30301c658 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m @@ -80,7 +80,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -115,27 +116,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"PUT" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -172,7 +158,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -207,27 +194,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -264,7 +236,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -310,6 +283,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -346,7 +320,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -392,6 +367,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -433,8 +409,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (petId != nil) { + pathParams[@"petId"] = petId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -462,7 +441,7 @@ NSString *requestContentType = [SWGApiClient selectHeaderContentType:@[]]; // Authentication setting - NSArray *authSettings = @[@"api_key", @"petstore_auth"]; + NSArray *authSettings = @[@"petstore_auth", @"api_key"]; id bodyParam = nil; NSMutableDictionary *formParams = [[NSMutableDictionary alloc] init]; @@ -474,6 +453,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -521,8 +501,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (petId != nil) { + pathParams[@"petId"] = petId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -570,6 +553,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -614,8 +598,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (petId != nil) { + pathParams[@"petId"] = petId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -657,6 +644,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"DELETE" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -704,8 +692,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (petId != nil) { + pathParams[@"petId"] = petId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -753,6 +744,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files diff --git a/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m b/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m index ed792988597..792147ebed1 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m @@ -77,7 +77,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -117,6 +118,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -153,7 +155,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -188,27 +191,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -250,8 +238,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"orderId", @"}"]] withString: [SWGApiClient escape:orderId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (orderId != nil) { + pathParams[@"orderId"] = orderId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -291,6 +282,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -332,8 +324,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"orderId", @"}"]] withString: [SWGApiClient escape:orderId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (orderId != nil) { + pathParams[@"orderId"] = orderId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -373,6 +368,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"DELETE" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files diff --git a/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m b/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m index 46a76b35b57..92dd00fa28d 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m @@ -80,7 +80,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -115,27 +116,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -172,7 +158,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -207,27 +194,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -264,7 +236,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -299,27 +272,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -359,7 +317,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -407,6 +366,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -440,7 +400,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -480,6 +441,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -521,8 +483,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (username != nil) { + pathParams[@"username"] = username; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -562,6 +527,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -606,8 +572,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (username != nil) { + pathParams[@"username"] = username; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -642,27 +611,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"PUT" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -704,8 +658,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (username != nil) { + pathParams[@"username"] = username; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -745,6 +702,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"DELETE" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files From 6218ad139ffddf17eaf09ecea700f3d3c6806155 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 21 Aug 2015 11:20:29 +0800 Subject: [PATCH 08/11] Fix issue that it will throw error if not pass optional form param in objc client. --- .../src/main/resources/objc/api-body.mustache | 4 +++- .../client/petstore/objc/SwaggerClient/SWGPetApi.m | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache index a47b39c22cd..9c7d8388b23 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache @@ -138,7 +138,9 @@ {{/bodyParam}}{{^bodyParam}} {{#formParams}} {{#notFile}} - formParams[@"{{paramName}}"] = {{paramName}}; + if ({{paramName}}) { + formParams[@"{{paramName}}"] = {{paramName}}; + } {{/notFile}}{{#isFile}} files[@"{{paramName}}"] = {{paramName}}; {{/isFile}} diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m index 4f30301c658..d79584f90bf 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m @@ -541,11 +541,15 @@ - formParams[@"name"] = name; + if (name) { + formParams[@"name"] = name; + } - formParams[@"status"] = status; + if (status) { + formParams[@"status"] = status; + } @@ -732,7 +736,9 @@ - formParams[@"additionalMetadata"] = additionalMetadata; + if (additionalMetadata) { + formParams[@"additionalMetadata"] = additionalMetadata; + } From 4189d0765bbb6214f435b2c9211284ee5387dfd6 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 21 Aug 2015 11:40:59 +0800 Subject: [PATCH 09/11] Update api body template of objc client --- .../swagger-codegen/src/main/resources/objc/api-body.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache index 9c7d8388b23..534c887fe7b 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache @@ -139,7 +139,7 @@ {{#formParams}} {{#notFile}} if ({{paramName}}) { - formParams[@"{{paramName}}"] = {{paramName}}; + formParams[@"{{baseName}}"] = {{paramName}}; } {{/notFile}}{{#isFile}} files[@"{{paramName}}"] = {{paramName}}; From 07162811a724337e832e27fc988e5c9793500f67 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 21 Aug 2015 08:56:22 -0700 Subject: [PATCH 10/11] fix for #1099, added file check --- .../src/main/java/io/swagger/codegen/DefaultGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index 1e0ae01c127..9f601930c04 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -193,7 +193,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { for (String templateName : config.apiTemplateFiles().keySet()) { String filename = config.apiFilename(templateName, tag); - if (!config.shouldOverwrite(filename)) { + if (!config.shouldOverwrite(filename) && new File(filename).exists()) { continue; } From 44b4af0374da2994dec4734c54c060a640ec2b5b Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 21 Aug 2015 15:02:56 -0700 Subject: [PATCH 11/11] added check, fix for #1103 --- .../java/io/swagger/codegen/DefaultCodegen.java | 4 +++- .../src/test/scala/Java/JavaModelTest.scala | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index bf58bd1cc36..212358b9627 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -636,7 +636,9 @@ public class DefaultCodegen { if (np.getMaximum() != null) { allowableValues.put("max", np.getMaximum()); } - property.allowableValues = allowableValues; + if(allowableValues.size() > 0) { + property.allowableValues = allowableValues; + } } if (p instanceof StringProperty) { diff --git a/modules/swagger-codegen/src/test/scala/Java/JavaModelTest.scala b/modules/swagger-codegen/src/test/scala/Java/JavaModelTest.scala index 81e252aa117..0043045a29d 100644 --- a/modules/swagger-codegen/src/test/scala/Java/JavaModelTest.scala +++ b/modules/swagger-codegen/src/test/scala/Java/JavaModelTest.scala @@ -2,6 +2,7 @@ package Java import io.swagger.codegen.languages.JavaClientCodegen import io.swagger.models._ +import io.swagger.models.parameters._ import io.swagger.models.properties._ import io.swagger.util.Json import org.junit.runner.RunWith @@ -382,7 +383,6 @@ class JavaModelTest2 extends FlatSpec with Matchers { cm.vars.size should be(1) val vars = cm.vars - Json.prettyPrint(vars.get(0)) vars.get(0).baseName should be("_") vars.get(0).getter should be("getU") vars.get(0).setter should be("setU") @@ -393,4 +393,17 @@ class JavaModelTest2 extends FlatSpec with Matchers { vars.get(0).hasMore should equal(null) vars.get(0).isNotContainer should equal(true) } + + it should "convert a parameter" in { + val parameter = new QueryParameter() + .property( + new IntegerProperty()) + .name("limit") + .required(true) + + val codegen = new JavaClientCodegen() + val cp = codegen.fromParameter(parameter, null) + + cp.allowableValues should be (null) + } }