Skip to content

Commit ebcc65f

Browse files
committed
Merge branch 'master' of https://github.com/ibaranga/openapi-generator into ibaranga-master
2 parents 4185782 + 1ab55cd commit ebcc65f

File tree

7 files changed

+113
-1
lines changed

7 files changed

+113
-1
lines changed

docs/generators/java-camel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
9292
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
9393
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
9494
|sourceFolder|source folder for generated code| |src/main/java|
95+
|springHttpClientAdapter|Allows users to choose between different HTTP client implementations for Spring HTTP interfaces (`web-client`, `rest-client`, `rest-template`).| |web-client|
9596
|testOutput|Set output folder for models and APIs tests| |${project.build.directory}/generated-test-sources/openapi|
9697
|title|server title name or client service name| |OpenAPI Spring|
9798
|unhandledException|Declare operation methods to throw a generic exception and allow unhandled exceptions (useful for Spring `@ControllerAdvice` directives).| |false|

docs/generators/spring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
8585
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
8686
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
8787
|sourceFolder|source folder for generated code| |src/main/java|
88+
|springHttpClientAdapter|Allows users to choose between different HTTP client implementations for Spring HTTP interfaces (`web-client`, `rest-client`, `rest-template`).| |web-client|
8889
|testOutput|Set output folder for models and APIs tests| |${project.build.directory}/generated-test-sources/openapi|
8990
|title|server title name or client service name| |OpenAPI Spring|
9091
|unhandledException|Declare operation methods to throw a generic exception and allow unhandled exceptions (useful for Spring `@ControllerAdvice` directives).| |false|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
import java.util.regex.Matcher;
3737
import java.util.regex.Pattern;
3838
import java.util.stream.Collectors;
39+
import java.util.stream.Stream;
3940

4041
import lombok.Getter;
42+
import lombok.RequiredArgsConstructor;
4143
import lombok.Setter;
4244
import org.apache.commons.lang3.StringUtils;
4345
import org.apache.commons.lang3.tuple.Pair;
@@ -102,6 +104,7 @@ public class SpringCodegen extends AbstractJavaCodegen
102104
public static final String SPRING_BOOT = "spring-boot";
103105
public static final String SPRING_CLOUD_LIBRARY = "spring-cloud";
104106
public static final String SPRING_HTTP_INTERFACE = "spring-http-interface";
107+
public static final String SPRING_HTTP_CLIENT_ADAPTER = "springHttpClientAdapter";
105108
public static final String API_FIRST = "apiFirst";
106109
public static final String SPRING_CONTROLLER = "useSpringController";
107110
public static final String HATEOAS = "hateoas";
@@ -126,6 +129,25 @@ public class SpringCodegen extends AbstractJavaCodegen
126129
}
127130
}
128131

132+
@Getter
133+
@RequiredArgsConstructor
134+
public enum SpringHttpClientAdapter {
135+
web_client("web-client", "Use WebClientAdapter", "httpInterfacesWebClientConfiguration.mustache"),
136+
rest_client("rest-client", "Use RestClientAdapter", "httpInterfacesRestClientConfiguration.mustache"),
137+
rest_template("rest-template", "Use RestTemplateAdapter", "httpInterfacesRestTemplateConfiguration.mustache");
138+
139+
private final String key;
140+
private final String description;
141+
private final String templateFileName;
142+
143+
static SpringHttpClientAdapter fromKey(String key) {
144+
return Stream.of(values()).filter(value -> value.getKey().equals(key)).findFirst().orElseThrow(
145+
() -> new IllegalArgumentException("Invalid SpringHttpClientAdapter key: " + key)
146+
);
147+
}
148+
149+
}
150+
129151
public static final String OPEN_BRACE = "{";
130152
public static final String CLOSE_BRACE = "}";
131153

@@ -165,6 +187,7 @@ public class SpringCodegen extends AbstractJavaCodegen
165187
protected boolean generatedConstructorWithRequiredArgs = true;
166188
@Getter @Setter
167189
protected RequestMappingMode requestMappingMode = RequestMappingMode.controller;
190+
@Setter SpringHttpClientAdapter springHttpClientAdapter = SpringHttpClientAdapter.web_client;
168191

169192
public SpringCodegen() {
170193
super();
@@ -268,6 +291,10 @@ public SpringCodegen() {
268291
generatedConstructorWithRequiredArgs));
269292
cliOptions.add(new CliOption(RESOURCE_FOLDER, RESOURCE_FOLDER_DESC).defaultValue(this.getResourceFolder()));
270293

294+
cliOptions.add(CliOption.newString(SPRING_HTTP_CLIENT_ADAPTER,
295+
"Allows users to choose between different HTTP client implementations for Spring HTTP interfaces (`web-client`, `rest-client`, `rest-template`).")
296+
.defaultValue(SpringHttpClientAdapter.web_client.getKey())
297+
);
271298
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
272299
supportedLibraries.put(SPRING_CLOUD_LIBRARY,
273300
"Spring-Cloud-Feign client with Spring-Boot auto-configured settings.");
@@ -443,7 +470,9 @@ public void processOpts() {
443470
useJakartaEe=true;
444471
applyJakartaPackage();
445472
}
473+
446474
convertPropertyToStringAndWriteBack(RESOURCE_FOLDER, this::setResourceFolder);
475+
convertPropertyToTypeAndWriteBack(SPRING_HTTP_CLIENT_ADAPTER, SpringHttpClientAdapter::fromKey, this::setSpringHttpClientAdapter);
447476

448477
typeMapping.put("file", "org.springframework.core.io.Resource");
449478
importMapping.put("org.springframework.core.io.Resource", "org.springframework.core.io.Resource");
@@ -528,7 +557,12 @@ public void processOpts() {
528557
}
529558
}
530559
} else if (SPRING_HTTP_INTERFACE.equals(library)) {
531-
supportingFiles.add(new SupportingFile("httpInterfacesConfiguration.mustache",
560+
if (!reactive && springHttpClientAdapter == SpringHttpClientAdapter.web_client) {
561+
LOGGER.warn("Configuration mismatch: The 'web-client' is selected as the HTTP client adapter, "
562+
+ "but 'reactive' is set to 'false'. "
563+
+ "Consider using 'rest-template' or 'rest-client' for non-reactive configurations.");
564+
}
565+
supportingFiles.add(new SupportingFile(springHttpClientAdapter.getTemplateFileName(),
532566
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "HttpInterfacesAbstractConfigurator.java"));
533567
writePropertyBack(USE_BEANVALIDATION, false);
534568
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) ({{{generatorVersion}}}).
3+
* https://openapi-generator.tech
4+
* Do not edit the class manually.
5+
*/
6+
package {{configPackage}};
7+
8+
{{#apiInfo}}
9+
{{#apis}}
10+
import {{apiPackage}}.{{classname}};
11+
{{/apis}}
12+
{{/apiInfo}}
13+
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.web.client.RestClient;
16+
import org.springframework.web.client.support.RestClientAdapter;
17+
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
18+
19+
public abstract class HttpInterfacesAbstractConfigurator {
20+
21+
private final RestClient restClient;
22+
23+
public HttpInterfacesAbstractConfigurator(final RestClient restClient) {
24+
this.restClient = restClient;
25+
}
26+
27+
{{#apiInfo}}
28+
{{#apis}}
29+
@Bean(name = "{{configPackage}}.HttpInterfacesAbstractConfigurator.{{classVarName}}")
30+
{{classname}} {{classVarName}}HttpProxy() {
31+
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(RestClientAdapter.create(restClient)).build();
32+
return factory.createClient({{classname}}.class);
33+
}
34+
35+
{{/apis}}
36+
{{/apiInfo}}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) ({{{generatorVersion}}}).
3+
* https://openapi-generator.tech
4+
* Do not edit the class manually.
5+
*/
6+
package {{configPackage}};
7+
8+
{{#apiInfo}}
9+
{{#apis}}
10+
import {{apiPackage}}.{{classname}};
11+
{{/apis}}
12+
{{/apiInfo}}
13+
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.web.client.RestTemplate;
16+
import org.springframework.web.client.support.RestTemplateAdapter;
17+
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
18+
19+
public abstract class HttpInterfacesAbstractConfigurator {
20+
21+
private final RestTemplate restTemplate;
22+
23+
public HttpInterfacesAbstractConfigurator(final RestTemplate restTemplate) {
24+
this.restTemplate = restTemplate;
25+
}
26+
27+
{{#apiInfo}}
28+
{{#apis}}
29+
@Bean(name = "{{configPackage}}.HttpInterfacesAbstractConfigurator.{{classVarName}}")
30+
{{classname}} {{classVarName}}HttpProxy() {
31+
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(RestTemplateAdapter.create(restTemplate)).build();
32+
return factory.createClient({{classname}}.class);
33+
}
34+
35+
{{/apis}}
36+
{{/apiInfo}}
37+
38+
}

modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-http-interface/paramDoc.mustache

Whitespace-only changes.

0 commit comments

Comments
 (0)