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

Enable code generation for OpenFeign with Spring-Boot 2 (#9118) #9120

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 @@ -33,6 +33,7 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String SPRING_CLOUD_LIBRARY = "spring-cloud";
public static final String IMPLICIT_HEADERS = "implicitHeaders";
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig";
public static final String TARGET_OPENFEIGN = "generateForOpenFeign";

protected String title = "swagger-petstore";
protected String configPackage = "io.swagger.configuration";
Expand All @@ -49,6 +50,7 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean implicitHeaders = false;
protected boolean swaggerDocketConfig = false;
protected boolean useOptional = false;
protected boolean openFeign = false;

public SpringCodegen() {
super();
Expand Down Expand Up @@ -81,6 +83,7 @@ public SpringCodegen() {
cliOptions.add(CliOption.newBoolean(SWAGGER_DOCKET_CONFIG, "Generate Spring Swagger Docket configuration class."));
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL,
"Use Optional container for optional parameters"));
cliOptions.add(CliOption.newBoolean(TARGET_OPENFEIGN,"Generate for usage with OpenFeign (instead of feign)"));

supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
Expand Down Expand Up @@ -178,7 +181,7 @@ public void processOpts() {
if (additionalProperties.containsKey(USE_TAGS)) {
this.setUseTags(Boolean.valueOf(additionalProperties.get(USE_TAGS).toString()));
}

if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION));
}
Expand All @@ -187,6 +190,10 @@ public void processOpts() {
this.setUseOptional(convertPropertyToBoolean(USE_OPTIONAL));
}

if (additionalProperties.containsKey(TARGET_OPENFEIGN)) {
this.setOpenFeign(convertPropertyToBoolean(TARGET_OPENFEIGN));
}

if (useBeanValidation) {
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
}
Expand All @@ -201,7 +208,7 @@ public void processOpts() {

typeMapping.put("file", "Resource");
importMapping.put("Resource", "org.springframework.core.io.Resource");

if (useOptional) {
writePropertyBack(USE_OPTIONAL, useOptional);
}
Expand Down Expand Up @@ -280,7 +287,7 @@ public void processOpts() {
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "JacksonConfiguration.java"));
}
}

if ((!this.delegatePattern && this.java8) || this.delegateMethod) {
additionalProperties.put("jdk8-no-delegate", true);
}
Expand All @@ -301,6 +308,10 @@ public void processOpts() {
additionalProperties.put(RESPONSE_WRAPPER, "Callable");
}

if(this.openFeign){
additionalProperties.put("isOpenFeign", "true");
}

// Some well-known Spring or Spring-Cloud response wrappers
switch (this.responseWrapper) {
case "Future":
Expand Down Expand Up @@ -673,7 +684,7 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {

return objs;
}

public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation;
}
Expand All @@ -682,4 +693,8 @@ public void setUseBeanValidation(boolean useBeanValidation) {
public void setUseOptional(boolean useOptional) {
this.useOptional = useOptional;
}

public void setOpenFeign(boolean openFeign) {
this.openFeign = openFeign;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package {{package}};

import org.springframework.cloud.netflix.feign.FeignClient;
{{^isOpenFeign}}
import org.springframework.cloud.netflix.feign.FeignClient;
{{/isOpenFeign}}
{{#isOpenFeign}}
import org.springframework.cloud.openfeign.FeignClient;
{{/isOpenFeign}}
import {{configPackage}}.ClientConfiguration;

{{=<% %>=}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class SpringOptionsProvider extends JavaOptionsProvider {
public static final String IMPLICIT_HEADERS = "false";
public static final String SWAGGER_DOCKET_CONFIG = "false";
public static final String USE_OPTIONAL = "false";
public static final String TARGET_OPENFEIGN = "false";

@Override
public String getLanguage() {
Expand All @@ -46,6 +47,7 @@ public Map<String, String> createOptions() {
options.put(SpringCodegen.IMPLICIT_HEADERS, IMPLICIT_HEADERS);
options.put(SpringCodegen.SWAGGER_DOCKET_CONFIG, SWAGGER_DOCKET_CONFIG);
options.put(SpringCodegen.USE_OPTIONAL, USE_OPTIONAL);
options.put(SpringCodegen.TARGET_OPENFEIGN, TARGET_OPENFEIGN);

return options;
}
Expand Down