Skip to content

Commit b4bf157

Browse files
committed
add logic to enable additional property
1 parent f08b021 commit b4bf157

2 files changed

Lines changed: 71 additions & 42 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public class CodegenConstants {
156156
public static final String USE_DATETIME_OFFSET = "useDateTimeOffset";
157157
public static final String USE_DATETIME_OFFSET_DESC = "Use DateTimeOffset to model date-time properties";
158158

159+
// public static final String INCLUDE_HTTP_REQUEST_CONTEXT = "includeHttpRequestContext";
160+
// public static final String INCLUDE_HTTP_REQUEST_CONTEXT_DESC = "Include HttpServletRequest (blocking) or ServerWebExchange (reactive) as additional parameter in generated methods";
161+
159162
public static final String USE_DATETIME_FOR_DATE = "useDateTimeForDate";
160163
public static final String USE_DATETIME_FOR_DATE_DESC = "Use DateTime to model date properties even if DateOnly supported. (.net 6.0+ only)";
161164

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

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
8686
public static final String BEAN_QUALIFIERS = "beanQualifiers";
8787

8888
public static final String USE_SPRING_BOOT3 = "useSpringBoot3";
89+
public static final String INCLUDE_HTTP_REQUEST_CONTEXT = "includeHttpRequestContext";
8990
public static final String USE_FLOW_FOR_ARRAY_RETURN_TYPE = "useFlowForArrayReturnType";
9091
public static final String REQUEST_MAPPING_OPTION = "requestMappingMode";
9192
public static final String USE_REQUEST_MAPPING_ON_CONTROLLER = "useRequestMappingOnController";
@@ -107,36 +108,58 @@ public String getDescription() {
107108
}
108109
}
109110

110-
@Getter @Setter
111+
@Getter
112+
@Setter
111113
private String basePackage;
112-
@Getter @Setter
114+
@Getter
115+
@Setter
113116
protected String configPackage;
114-
@Getter @Setter
117+
@Getter
118+
@Setter
115119
private String invokerPackage;
116-
@Getter @Setter
120+
@Getter
121+
@Setter
117122
private String serverPort = "8080";
118123
private String title = "OpenAPI Kotlin Spring";
119124
private boolean useBeanValidation = true;
120-
@Setter private boolean skipDefaultInterface = false;
121-
@Setter private boolean skipDefaultApiInterface = false;
122-
@Setter private boolean skipDefaultDelegateInterface = false;
123-
@Setter private boolean exceptionHandler = true;
124-
@Setter private boolean gradleBuildFile = true;
125+
@Setter
126+
private boolean skipDefaultInterface = false;
127+
@Setter
128+
private boolean skipDefaultApiInterface = false;
129+
@Setter
130+
private boolean includeHttpRequestContext = false;
131+
@Setter
132+
private boolean skipDefaultDelegateInterface = false;
133+
@Setter
134+
private boolean exceptionHandler = true;
135+
@Setter
136+
private boolean gradleBuildFile = true;
125137
private boolean useSwaggerUI = true;
126-
@Setter private boolean serviceInterface = false;
127-
@Setter private boolean serviceImplementation = false;
128-
@Getter @Setter
138+
@Setter
139+
private boolean serviceInterface = false;
140+
@Setter
141+
private boolean serviceImplementation = false;
142+
@Getter
143+
@Setter
129144
private boolean reactive = false;
130-
@Getter @Setter
145+
@Getter
146+
@Setter
131147
private boolean useFlowForArrayReturnType = true;
132-
@Setter private boolean interfaceOnly = false;
133-
@Setter protected boolean useFeignClientUrl = true;
134-
@Setter protected boolean useFeignClient = false;
135-
@Setter private boolean delegatePattern = false;
136-
@Setter protected boolean useTags = false;
137-
@Setter private boolean beanQualifiers = false;
138-
139-
@Getter @Setter
148+
@Setter
149+
private boolean interfaceOnly = false;
150+
@Setter
151+
protected boolean useFeignClientUrl = true;
152+
@Setter
153+
protected boolean useFeignClient = false;
154+
@Setter
155+
private boolean delegatePattern = false;
156+
@Setter
157+
protected boolean useTags = false;
158+
@Setter
159+
private boolean beanQualifiers = false;
160+
161+
@Getter
162+
@Setter
140163
protected boolean useSpringBoot3 = false;
141164
protected RequestMappingMode requestMappingMode = RequestMappingMode.controller;
142165
private DocumentationProvider documentationProvider;
@@ -204,10 +227,10 @@ public KotlinSpringServerCodegen() {
204227
addSwitch(GRADLE_BUILD_FILE, "generate a gradle build file using the Kotlin DSL", gradleBuildFile);
205228
addSwitch(USE_SWAGGER_UI, "Open the OpenApi specification in swagger-ui. Will also import and configure needed dependencies", useSwaggerUI);
206229
addSwitch(SERVICE_INTERFACE, "generate service interfaces to go alongside controllers. In most " +
207-
"cases this option would be used to update an existing project, so not to override implementations. " +
208-
"Useful to help facilitate the generation gap pattern", serviceInterface);
230+
"cases this option would be used to update an existing project, so not to override implementations. " +
231+
"Useful to help facilitate the generation gap pattern", serviceInterface);
209232
addSwitch(SERVICE_IMPLEMENTATION, "generate stub service implementations that extends service " +
210-
"interfaces. If this is set to true service interfaces will also be generated", serviceImplementation);
233+
"interfaces. If this is set to true service interfaces will also be generated", serviceImplementation);
211234
addSwitch(USE_BEANVALIDATION, "Use BeanValidation API annotations to validate data types", useBeanValidation);
212235
addSwitch(SKIP_DEFAULT_INTERFACE, "Whether to skip generation of default implementations for interfaces (Api interfaces or Delegate interfaces depending on the delegatePattern option)", skipDefaultInterface);
213236
addSwitch(REACTIVE, "use coroutines for reactive behavior", reactive);
@@ -220,6 +243,7 @@ public KotlinSpringServerCodegen() {
220243
" (contexts) added to single project.", beanQualifiers);
221244
addSwitch(USE_SPRING_BOOT3, "Generate code and provide dependencies for use with Spring Boot 3.x. (Use jakarta instead of javax in imports). Enabling this option will also enable `useJakartaEe`.", useSpringBoot3);
222245
addSwitch(USE_FLOW_FOR_ARRAY_RETURN_TYPE, "Whether to use Flow for array/collection return types when reactive is enabled. If false, will use List instead.", useFlowForArrayReturnType);
246+
addSwitch(INCLUDE_HTTP_REQUEST_CONTEXT, "Whether to include HttpServletRequest (blocking) or ServerWebExchange (reactive) as additional parameter in generated methods.", includeHttpRequestContext);
223247
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
224248
supportedLibraries.put(SPRING_CLOUD_LIBRARY,
225249
"Spring-Cloud-Feign client with Spring-Boot auto-configured settings.");
@@ -307,7 +331,7 @@ public List<AnnotationLibrary> supportedAnnotationLibraries() {
307331
*/
308332
private boolean selectedDocumentationProviderRequiresSwaggerUiBootstrap() {
309333
return getDocumentationProvider().equals(DocumentationProvider.SPRINGFOX) ||
310-
getDocumentationProvider().equals(DocumentationProvider.SOURCE);
334+
getDocumentationProvider().equals(DocumentationProvider.SOURCE);
311335
}
312336

313337
public boolean getExceptionHandler() {
@@ -435,7 +459,7 @@ public void processOpts() {
435459

436460
// Set basePackage from invokerPackage
437461
if (!additionalProperties.containsKey(BASE_PACKAGE)
438-
&& additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
462+
&& additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
439463
this.setBasePackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
440464
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
441465
additionalProperties.put(BASE_PACKAGE, basePackage);
@@ -564,6 +588,9 @@ public void processOpts() {
564588
if (additionalProperties.containsKey(USE_SPRING_BOOT3)) {
565589
this.setUseSpringBoot3(convertPropertyToBoolean(USE_SPRING_BOOT3));
566590
}
591+
if (additionalProperties.containsKey(INCLUDE_HTTP_REQUEST_CONTEXT)) {
592+
this.setIncludeHttpRequestContext(convertPropertyToBoolean(INCLUDE_HTTP_REQUEST_CONTEXT));
593+
}
567594
if (isUseSpringBoot3()) {
568595
if (DocumentationProvider.SPRINGFOX.equals(getDocumentationProvider())) {
569596
throw new IllegalArgumentException(DocumentationProvider.SPRINGFOX.getPropertyName() + " is not supported with Spring Boot > 3.x");
@@ -605,7 +632,6 @@ public void processOpts() {
605632

606633
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
607634

608-
609635
if (this.exceptionHandler && !library.equals(SPRING_CLOUD_LIBRARY)) {
610636
supportingFiles.add(new SupportingFile("exceptions.mustache",
611637
sanitizeDirectory(sourceFolder + File.separator + apiPackage), "Exceptions.kt"));
@@ -713,16 +739,16 @@ public void processOpts() {
713739
}
714740

715741
switch (getRequestMappingMode()) {
716-
case api_interface:
717-
additionalProperties.put(USE_REQUEST_MAPPING_ON_INTERFACE, true);
718-
break;
719-
case controller:
720-
additionalProperties.put(USE_REQUEST_MAPPING_ON_CONTROLLER, true);
721-
break;
722-
case none:
723-
additionalProperties.put(USE_REQUEST_MAPPING_ON_INTERFACE, false);
724-
additionalProperties.put(USE_REQUEST_MAPPING_ON_CONTROLLER, false);
725-
break;
742+
case api_interface:
743+
additionalProperties.put(USE_REQUEST_MAPPING_ON_INTERFACE, true);
744+
break;
745+
case controller:
746+
additionalProperties.put(USE_REQUEST_MAPPING_ON_CONTROLLER, true);
747+
break;
748+
case none:
749+
additionalProperties.put(USE_REQUEST_MAPPING_ON_INTERFACE, false);
750+
additionalProperties.put(USE_REQUEST_MAPPING_ON_CONTROLLER, false);
751+
break;
726752
}
727753

728754
// spring uses the jackson lib, and we disallow configuration.
@@ -772,7 +798,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
772798

773799
if (SPRING_BOOT.equals(library) && ModelUtils.containsEnums(this.openAPI)) {
774800
supportingFiles.add(new SupportingFile("converter.mustache",
775-
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "EnumConverterConfiguration.kt"));
801+
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "EnumConverterConfiguration.kt"));
776802
}
777803

778804
if (!additionalProperties.containsKey(TITLE)) {
@@ -924,9 +950,9 @@ public void setReturnContainer(final String returnContainer) {
924950
operation.returnContainer = returnContainer;
925951
}
926952
});
927-
// if(implicitHeaders){
928-
// removeHeadersFromAllParams(operation.allParams);
929-
// }
953+
// if(implicitHeaders){
954+
// removeHeadersFromAllParams(operation.allParams);
955+
// }
930956
});
931957
}
932958

@@ -939,7 +965,6 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
939965
return objs;
940966
}
941967

942-
943968
private String getNonMutableContainerTypeIfNeeded(String type) {
944969
if (type != null && type.contains("kotlin.collections.Mutable")) {
945970
return type.replaceAll("kotlin\\.collections\\.Mutable", "kotlin.collections.");
@@ -953,6 +978,7 @@ private static String sanitizeDirectory(String in) {
953978

954979
// TODO could probably be made more generic, and moved to the `mustache` package if required by other components.
955980
private static class EscapeLambda implements Mustache.Lambda {
981+
956982
private final String from;
957983
private final String to;
958984

0 commit comments

Comments
 (0)