Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 254 #267

Merged
merged 2 commits into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.samskivert.mustache.Template;
import io.swagger.codegen.v3.CliOption;
import io.swagger.codegen.v3.CodegenConstants;
import io.swagger.codegen.v3.CodegenContent;
import io.swagger.codegen.v3.CodegenModel;
import io.swagger.codegen.v3.CodegenOperation;
import io.swagger.codegen.v3.CodegenParameter;
Expand Down Expand Up @@ -34,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

import static io.swagger.codegen.v3.CodegenConstants.HAS_ENUMS_EXT_NAME;
import static io.swagger.codegen.v3.CodegenConstants.IS_ENUM_EXT_NAME;
Expand Down Expand Up @@ -505,6 +507,7 @@ public void setReturnContainer(final String returnContainer) {

if(implicitHeaders){
removeHeadersFromAllParams(operation.allParams);
removeHeadersFromContents(operation.contents);
}
}
}
Expand Down Expand Up @@ -567,6 +570,25 @@ private void removeHeadersFromAllParams(List<CodegenParameter> allParams) {
allParams.get(allParams.size()-1).getVendorExtensions().put(CodegenConstants.HAS_MORE_EXT_NAME, Boolean.FALSE);
}

private void removeHeadersFromContents(List<CodegenContent> contents) {
if(contents == null || contents.isEmpty()){
return;
}
for(int index = 0; index < contents.size(); index++) {
final CodegenContent codegenContent = contents.get(index);
final List<CodegenParameter> parameters = codegenContent.getParameters();
if (parameters == null || parameters.isEmpty()) {
continue;
}
final List<CodegenParameter> filteredParameters = parameters.stream()
.filter(codegenParameter -> !getBooleanValue(codegenParameter, CodegenConstants.IS_HEADER_PARAM_EXT_NAME))
.collect(Collectors.toList());
parameters.clear();
parameters.addAll(filteredParameters);
parameters.get(parameters.size()-1).getVendorExtensions().put(CodegenConstants.HAS_MORE_EXT_NAME, Boolean.FALSE);
}
}

@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
if(library.equals(SPRING_CLOUD_LIBRARY)) {
Expand Down Expand Up @@ -630,11 +652,6 @@ public String toBooleanGetter(String name) {
return getterAndSetterCapitalize(name);
}

@Override
public TemplateEngine getTemplateEngine() {
return new MustacheTemplateEngine(this);
}

public void setTitle(String title) {
this.title = title;
}
Expand Down Expand Up @@ -733,10 +750,4 @@ public void setUseBeanValidation(boolean useBeanValidation) {
public void setUseOptional(boolean useOptional) {
this.useOptional = useOptional;
}

// todo: remove this once handlebar templates for this generator are implemented
@Override
protected void setTemplateEngine() {
templateEngine = new MustacheTemplateEngine(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import java.util.Optional;
import java.io.IOException;
{{/isDelegate}}
import java.util.List;
import java.util.Map;
{{#async}}
import java.util.concurrent.Callable;
{{/async}}
Expand Down Expand Up @@ -99,7 +100,7 @@ public class {{classname}}Controller implements {{classname}} {
{{^jdk8}}
{{#operation}}
{{#contents}}
public {{#async}}Callable<{{/async}}ResponseEntity<{{>returnTypes}}>{{#async}}>{{/async}} {{operationId}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#has this 'more'}},{{/has}}{{/parameters}}) {
public {{#async}}Callable<{{/async}}ResponseEntity<{{>returnTypes}}>{{#async}}>{{/async}} {{operationId}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/parameters}}) {
{{^isDelegate}}
{{^async}}
String accept = request.getHeader("Accept");
Expand Down Expand Up @@ -138,7 +139,7 @@ public class {{classname}}Controller implements {{classname}} {
{{/async}}
{{/isDelegate}}
{{#isDelegate}}
return delegate.{{operationId}}({{#parameters}}{{paramName}}{{#has this 'more'}}, {{/has}}{{/parameters}});
return delegate.{{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}});
{{/isDelegate}}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/handlebars/JavaSpring/apiDelegate.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
{{/jdk8}}
import java.util.List;
import java.util.Map;
{{#jdk8}}
import java.util.Optional;
{{/jdk8}}
Expand Down Expand Up @@ -62,8 +63,8 @@ public interface {{classname}}Delegate {
/**
* @see {{classname}}#{{operationId}}
*/
{{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#parameters}}{{#isNot this 'file'}} {{>optionalDataType}} {{/isNot}}{{#is this 'file'}}MultipartFile{{/is}} {{paramName}}{{#has this 'more'}},
{{/has}}{{/parameters}}){{^jdk8}};{{/jdk8}}{{#jdk8}} {
{{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#parameters}}{{^isFile}} {{>optionalDataType}} {{/isFile}}{{#isFile}}MultipartFile{{/isFile}} {{paramName}}{{#hasMore}},
{{/hasMore}}{{/parameters}}){{^jdk8}};{{/jdk8}}{{#jdk8}} {
if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
{{#examples}}
if (getAcceptHeader().get().contains("{{{contentType}}}")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{#required}}
@NotNull
{{/required}}{{#is this 'container'}}{{#isNot this 'primitive-type'}}{{^isEnum}}
@Valid{{/isEnum}}{{/isNot}}{{/is}}{{#isNot this 'container'}}{{#isNot this 'primitive-type'}}
@Valid{{/isNot}}{{/isNot}}
{{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}}
@Valid{{/isPrimitiveType}}{{/isNotContainer}}
{{>beanValidationCore}}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ minLength not set, maxLength set
}}{{^minItems}}{{#maxItems}}@Size(max={{maxItems}}) {{/maxItems}}{{/minItems}}{{!
check for integer or long / all others=decimal type with @Decimal*
isInteger set
}}{{#is this 'integer'}}{{#minimum}}@Min({{minimum}}){{/minimum}}{{#maximum}} @Max({{maximum}}) {{/maximum}}{{/is}}{{!
}}{{#isInteger}}{{#minimum}}@Min({{minimum}}){{/minimum}}{{#maximum}} @Max({{maximum}}) {{/maximum}}{{/isInteger}}{{!
isLong set
}}{{#is this 'long'}}{{#minimum}}@Min({{minimum}}L){{/minimum}}{{#maximum}} @Max({{maximum}}L) {{/maximum}}{{/is}}{{!
}}{{#isLong}}{{#minimum}}@Min({{minimum}}L){{/minimum}}{{#maximum}} @Max({{maximum}}L) {{/maximum}}{{/isLong}}{{!
Not Integer, not Long => we have a decimal value!
}}{{#isNot this 'integer'}}{{#isNot this 'long'}}{{#minimum}}@DecimalMin("{{minimum}}"){{/minimum}}{{#maximum}} @DecimalMax("{{maximum}}") {{/maximum}}{{/isNot}}{{/isNot}}
}}{{^isInteger}}{{^isLong}}{{#minimum}}@DecimalMin("{{minimum}}"){{/minimum}}{{#maximum}} @DecimalMax("{{maximum}}") {{/maximum}}{{/isLong}}{{/isInteger}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'body-param'}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{#isNot this 'container'}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{/isNot}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody {{{dataType}}} {{paramName}}{{/is}}
{{#isBodyParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{/isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody {{{dataType}}} {{paramName}}{{/isBodyParam}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{#gson}}
{{#allowableValues}}
{{#enumVars}}
@SerializedName({{#is this 'integer'}}"{{/is}}{{#is this 'double'}}"{{/is}}{{#is this 'long'}}"{{/is}}{{#is this 'float'}}"{{/is}}{{{value}}}{{#is this 'integer'}}"{{/is}}{{#is this 'double'}}"{{/is}}{{#is this 'long'}}"{{/is}}{{#is this 'float'}}"{{/is}})
@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
{{{name}}}({{{value}}}){{^@last}},
{{/@last}}{{#@last}};{{/@last}}
{{/enumVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
{{#gson}}
{{#allowableValues}}{{#enumVars}}
@SerializedName({{#is this 'integer'}}"{{/is}}{{#is this 'double'}}"{{/is}}{{#is this 'long'}}"{{/is}}{{#is this 'float'}}"{{/is}}{{{value}}}{{#is this 'integer'}}"{{/is}}{{#is this 'double'}}"{{/is}}{{#is this 'long'}}"{{/is}}{{#is this 'float'}}"{{/is}})
@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
{{{name}}}({{{value}}}){{^@last}},
{{/@last}}{{#@last}};{{/@last}}{{/enumVars}}{{/allowableValues}}
{{/gson}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#returnContainer}}{{#is this 'map-container'}}Map{{/is}}{{#is this 'list-container'}}List{{/is}}{{/returnContainer}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}}
{{#returnContainer}}{{#isMapContainer}}Map{{/isMapContainer}}{{#isListContainer}}List{{/isListContainer}}{{/returnContainer}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'form-param'}}{{#isNot this 'file'}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestParam(value="{{baseName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) {{{dataType}}} {{paramName}}{{/isNot}}{{#is this 'file'}}@ApiParam(value = "file detail") {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestPart("file") MultipartFile {{baseName}}{{/is}}{{/is}}
{{#isFormParam}}{{#notFile}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestParam(value="{{baseName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}@ApiParam(value = "file detail") {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestPart("file") MultipartFile {{baseName}}{{/isFile}}{{/isFormParam}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'header-param'}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestHeader(value="{{baseName}}", required={{#required}}true{{/required}}{{^required}}false{{/required}}) {{>optionalDataType}} {{paramName}}{{/is}}
{{#isHeaderParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestHeader(value="{{baseName}}", required={{#required}}true{{/required}}{{^required}}false{{/required}}) {{>optionalDataType}} {{paramName}}{{/isHeaderParam}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#is this 'header-param'}}@ApiImplicitParam(name = "{{{paramName}}}", value = "{{{description}}}", {{#required}}required=true,{{/required}} dataType = "{{{dataType}}}", paramType = "header"){{#has this 'more'}},{{/has}}{{/is}}
{{#isHeaderParam}}@ApiImplicitParam(name = "{{{paramName}}}", value = "{{{description}}}", {{#required}}required=true,{{/required}} dataType = "{{{dataType}}}", paramType = "header"){{#hasMore}},{{/hasMore}}{{/isHeaderParam}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package {{package}};

{{#imports}}import {{import}};
{{/imports}}

import java.util.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringRunner.class)
@SpringBootTest
public class {{classname}}ControllerIntegrationTest {

@Autowired
private {{classname}} api;

{{#operations}}
{{#operation}}
{{#contents}}
@Test
public void {{operationId}}Test() throws Exception {
{{#parameters}}
{{^isFile}}
{{{dataType}}} {{paramName}} = {{{example}}};
{{/isFile}}
{{#isFile}}
org.springframework.web.multipart.MultipartFile {{paramName}} = null;
{{/isFile}}
{{/parameters}}
ResponseEntity<{{>returnTypes}}> responseEntity = api.{{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}});
assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
}

{{/contents}}
{{/operation}}
{{/operations}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
{{/useBeanValidation}}
{{/useBeanValidation}}

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.swagger;

import feign.Logger;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).run(args);
}

@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package {{package}};

{{#imports}}import {{import}};
{{/imports}}
import io.swagger.Application;

import java.util.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class {{classname}}Test {

@Autowired
private {{classname}} api;

{{#operations}}
{{#operation}}
{{#contents}}
@Test
public void {{operationId}}Test() throws Exception {
{{#parameters}}
{{^isFile}}
{{{dataType}}} {{paramName}} = {{{example}}};
{{/isFile}}
{{#isFile}}
org.springframework.web.multipart.MultipartFile {{paramName}} = null;
{{/isFile}}
{{/parameters}}
api.{{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}});
// todo: add verifications
}
{{/contents}}
{{/operation}}
{{/operations}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
public class ClientConfiguration {

{{#authMethods}}
{{#is this 'basic'}}
{{#isBasic}}
{{=<% %>=}}@Value("${<%title%>.security.<%name%>.username:}")<%={{ }}=%>
private String {{{name}}}Username;

Expand All @@ -36,26 +36,26 @@ public class ClientConfiguration {
return new BasicAuthRequestInterceptor(this.{{{name}}}Username, this.{{{name}}}Password);
}

{{/is}}
{{#is this 'api-key'}}
{{/isBasic}}
{{#isApiKey}}
{{=<% %>=}}@Value("${<%title%>.security.<%name%>.key:}")<%={{ }}=%>
private String {{{name}}}Key;

@Bean
@ConditionalOnProperty(name = "{{{title}}}.security.{{{name}}}.key")
public ApiKeyRequestInterceptor {{{name}}}RequestInterceptor() {
return new ApiKeyRequestInterceptor({{#is this 'key-in-header'}}"header"{{/is}}{{#isNot this 'key-in-header'}}"query"{{/isNot}}, "{{{keyParamName}}}", this.{{{name}}}Key);
return new ApiKeyRequestInterceptor({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{{keyParamName}}}", this.{{{name}}}Key);
}

{{/is}}
{{#is this 'oauth'}}
{{/isApiKey}}
{{#isOAuth}}
@Bean
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
public OAuth2FeignRequestInterceptor {{{name}}}RequestInterceptor() {
return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), {{{name}}}ResourceDetails());
}

{{#is this 'code'}}
{{#isCode}}
@Bean
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
@ConfigurationProperties("{{{title}}}.security.{{{name}}}")
Expand All @@ -66,8 +66,8 @@ public class ClientConfiguration {
return details;
}

{{/is}}
{{#is this 'password'}}
{{/isCode}}
{{#isPassword}}
@Bean
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
@ConfigurationProperties("{{{title}}}.security.{{{name}}}")
Expand All @@ -77,8 +77,8 @@ public class ClientConfiguration {
return details;
}

{{/is}}
{{#is this 'application'}}
{{/isPassword}}
{{#isApplication}}
@Bean
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
@ConfigurationProperties("{{{title}}}.security.{{{name}}}")
Expand All @@ -88,8 +88,8 @@ public class ClientConfiguration {
return details;
}

{{/is}}
{{#is this 'implicit'}}
{{/isApplication}}
{{#isImplicit}}
@Bean
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
@ConfigurationProperties("{{{title}}}.security.{{{name}}}")
Expand All @@ -99,7 +99,7 @@ public class ClientConfiguration {
return details;
}

{{/is}}
{{/is}}
{{/isImplicit}}
{{/isOAuth}}
{{/authMethods}}
}
Loading