Skip to content

Added HttpClient5 flavour to Java OpenFeign client generator. #21007

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

Merged
merged 8 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions bin/configs/java-feign-hc5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: java
outputDir: samples/client/petstore/java/feign-hc5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the new folder to the github workflow so that the CI will test the change moving forward

.github/workflows/samples-java-client-jdk11.yaml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also regenerate the samples (step 3, run the update commands twice) and commit the new files

library: feign-hc5
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/Java
additionalProperties:
booleanGetterPrefix: is
artifactId: petstore-feign-hc5
hideGenerationTimestamp: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
public static final String ERROR_OBJECT_TYPE = "errorObjectType";

public static final String FEIGN = "feign";
public static final String FEIGN_HC5 = "feign-hc5";
public static final String GOOGLE_API_CLIENT = "google-api-client";
public static final String JERSEY2 = "jersey2";
public static final String JERSEY3 = "jersey3";
Expand Down Expand Up @@ -246,6 +247,7 @@ public JavaClientCodegen() {
supportedLibraries.put(JERSEY2, "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.17.1");
supportedLibraries.put(JERSEY3, "HTTP client: Jersey client 3.1.1. JSON processing: Jackson 2.17.1");
supportedLibraries.put(FEIGN, "HTTP client: OpenFeign 13.2.1. JSON processing: Jackson 2.17.1 or Gson 2.10.1");
supportedLibraries.put(FEIGN_HC5, "HTTP client: OpenFeign 13.2.1/HttpClient5 5.4.2. JSON processing: Jackson 2.17.1 or Gson 2.10.1");
supportedLibraries.put(OKHTTP_GSON, "[DEFAULT] HTTP client: OkHttp 4.11.0. JSON processing: Gson 2.10.1. Enable Parcelable models on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 4.11.0. JSON processing: Gson 2.10.1 (Retrofit 2.5.0) or Jackson 2.17.1. Enable the RxJava adapter using '-DuseRxJava[2/3]=true'. (RxJava 1.x or 2.x or 3.x)");
supportedLibraries.put(RESTTEMPLATE, "HTTP client: Spring RestTemplate 5.3.33 (6.1.5 if `useJakartaEe=true`). JSON processing: Jackson 2.17.1");
Expand Down Expand Up @@ -325,7 +327,7 @@ public void processOpts() {

// determine and cache client library type once
final boolean libApache = isLibrary(APACHE);
final boolean libFeign = isLibrary(FEIGN);
final boolean libFeign = isLibrary(FEIGN) || isLibrary(FEIGN_HC5);
final boolean libGoogleApiClient = isLibrary(GOOGLE_API_CLIENT);
final boolean libJersey2 = isLibrary(JERSEY2);
final boolean libJersey3 = isLibrary(JERSEY3);
Expand Down Expand Up @@ -745,6 +747,14 @@ public void processOpts() {
additionalProperties.remove(SERIALIZATION_LIBRARY_JSONB);
break;
}

if (isLibrary(FEIGN)) {
additionalProperties.put("feign-okhttp", "true");
} else if (isLibrary(FEIGN_HC5)) {
additionalProperties.put("feign-hc5", "true");
setTemplateDir(FEIGN);
setLibrary(FEIGN);
}

// authentication related files
// has OAuth defined
Expand Down Expand Up @@ -825,7 +835,7 @@ public int compare(CodegenParameter one, CodegenParameter another) {
}

// camelize path variables for Feign client
if (isLibrary(FEIGN)) {
if (isLibrary(FEIGN) || isLibrary(FEIGN_HC5)) {
OperationMap operations = objs.getOperations();
List<CodegenOperation> operationList = operations.getOperation();
Pattern methodPattern = Pattern.compile("^(.*):([^:]*)$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import java.util.logging.Level;
import java.util.logging.Logger;

{{#jackson}}
{{#feign-okhttp}}
import feign.okhttp.OkHttpClient;
{{/feign-okhttp}}
{{#feign-hc5}}
import feign.hc5.ApacheHttp5Client;
{{/feign-hc5}}
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
Expand Down Expand Up @@ -71,7 +76,12 @@ public class ApiClient {
{{#jackson}}
objectMapper = createObjectMapper();
feignBuilder = Feign.builder()
{{#feign-okhttp}}
.client(new OkHttpClient())
{{/feign-okhttp}}
{{#feign-hc5}}
.client(new ApacheHttp5Client())
{{/feign-hc5}}
.encoder(new FormEncoder(new JacksonEncoder(objectMapper)))
.decoder(new ApiResponseDecoder(objectMapper))
{{#hasOAuthMethods}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ dependencies {
implementation "io.github.openfeign:feign-jackson:$feign_version"
{{/jackson}}
implementation "io.github.openfeign:feign-slf4j:$feign_version"
{{#feign-okhttp}}
implementation "io.github.openfeign:feign-okhttp:$feign_version"
{{/feign-okhttp}}
{{#feign-hc5}}
implementation "io.github.openfeign:feign-hc5:$feign_version"
{{/feign-hc5}}
implementation "io.github.openfeign.form:feign-form:$feign_form_version"
{{#jackson}}
{{#joda}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ lazy val root = (project in file(".")).
{{/jackson}}
"io.github.openfeign" % "feign-slf4j" % "13.5" % "compile",
"io.github.openfeign.form" % "feign-form" % "3.8.0" % "compile",
{{#feign-okhttp}}
"io.github.openfeign" % "feign-okhttp" % "13.5" % "compile",
{{/feign-okhttp}}
{{#feign-hc5}}
"io.github.openfeign" % "feign-hc5" % "13.5" % "compile",
{{/feign-hc5}}
{{#jackson}}
"com.fasterxml.jackson.core" % "jackson-core" % "2.17.1" % "compile",
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.17.1" % "compile",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,20 @@
<artifactId>feign-form</artifactId>
<version>${feign-form-version}</version>
</dependency>
{{#feign-okhttp}}
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>${feign-version}</version>
</dependency>
{{/feign-okhttp}}
{{#feign-hc5}}
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hc5</artifactId>
<version>${feign-version}</version>
</dependency>
{{/feign-hc5}}

{{#jackson}}
<!-- JSON processing: jackson -->
Expand Down
Loading