Skip to content

Commit a6ba995

Browse files
committed
add includeHttpRequestContext additional property defaulting to "true" for reactive and "false" for blocking. Implement tests and add samples to check compilation success.
1 parent aaa3500 commit a6ba995

318 files changed

Lines changed: 18565 additions & 386 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/samples-jdk17.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ on:
1414
- samples/client/petstore/java/microprofile-rest-client-outer-enum/**
1515
# servers
1616
- samples/openapi3/server/petstore/springboot-3/**
17+
- samples/openapi3/server/petstore/springboot-3-include-http-request-context/**
1718
- samples/server/petstore/java-camel/**
1819
- samples/server/petstore/java-helidon-server/v3/mp/**
1920
- samples/server/petstore/java-helidon-server/v3/se/**
@@ -31,6 +32,7 @@ on:
3132
- samples/client/petstore/java/microprofile-rest-client-outer-enum/**
3233
# servers
3334
- samples/openapi3/server/petstore/springboot-3/**
35+
- samples/openapi3/server/petstore/springboot-3-include-http-request-context/**
3436
- samples/server/petstore/java-camel/**
3537
- samples/server/petstore/java-helidon-server/v3/mp/**
3638
- samples/server/petstore/java-helidon-server/v3/se/**
@@ -54,6 +56,7 @@ jobs:
5456
- samples/client/petstore/java/microprofile-rest-client-outer-enum
5557
# servers
5658
- samples/openapi3/server/petstore/springboot-3
59+
- samples/openapi3/server/petstore/springboot-3-include-http-request-context
5760
- samples/server/petstore/java-camel/
5861
- samples/server/petstore/java-helidon-server/v3/mp/
5962
- samples/server/petstore/java-helidon-server/v3/se

.github/workflows/samples-spring.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
- samples/server/petstore/spring-boot-nullable-set
4545
- samples/server/petstore/spring-boot-defaultInterface-unhandledExcp
4646
- samples/server/petstore/springboot
47+
- samples/server/petstore/springboot-include-http-request-context
4748
- samples/server/petstore/springboot-beanvalidation
4849
- samples/server/petstore/springboot-builtin-validation
4950
- samples/server/petstore/springboot-delegate
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
generatorName: spring
2+
outputDir: samples/openapi3/server/petstore/springboot-3-include-http-request-context
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
5+
additionalProperties:
6+
groupId: org.openapitools.openapi3
7+
documentationProvider: springdoc
8+
artifactId: springboot
9+
snapshotVersion: "true"
10+
useSpringBoot3: true
11+
useBeanValidation: true
12+
withXml: true
13+
hideGenerationTimestamp: "true"
14+
generateConstructorWithAllArgs: true
15+
generateBuilders: true
16+
includeHttpRequestContext: "true"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generatorName: spring
2+
outputDir: samples/server/petstore/springboot-include-http-request-context
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/petstore-with-fake-endpoints-models-for-testing.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
5+
additionalProperties:
6+
documentationProvider: springfox
7+
artifactId: springboot
8+
snapshotVersion: "true"
9+
hideGenerationTimestamp: "true"
10+
camelCaseDollarSign: "true"
11+
modelNameSuffix: 'Dto'
12+
includeHttpRequestContext: "true"

bin/configs/spring-boot-reactive-noResponseEntity.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ additionalProperties:
1010
hideGenerationTimestamp: "true"
1111
delegatePattern: "true"
1212
useResponseEntity: "false"
13+
includeHttpRequestContext: "false"

docs/generators/java-camel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
6868
|ignoreAnyOfInEnum|Ignore anyOf keyword in enum| |false|
6969
|implicitHeaders|Skip header parameters in the generated API methods using @ApiImplicitParams annotation.| |false|
7070
|implicitHeadersRegex|Skip header parameters that matches given regex in the generated API methods using @ApiImplicitParams annotation. Note: this parameter is ignored when implicitHeaders=true| |null|
71+
|includeHttpRequestContext|Whether to include HttpServletRequest (blocking) or ServerWebExchange (reactive) as additional parameter in generated methods. Defaults to 'true' for reactive and 'false' for blocking.| |false|
7172
|interfaceOnly|Whether to generate only API interface stubs without the server files.| |false|
7273
|invokerPackage|root package for generated code| |org.openapitools.api|
7374
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|false|

docs/generators/spring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
6161
|ignoreAnyOfInEnum|Ignore anyOf keyword in enum| |false|
6262
|implicitHeaders|Skip header parameters in the generated API methods using @ApiImplicitParams annotation.| |false|
6363
|implicitHeadersRegex|Skip header parameters that matches given regex in the generated API methods using @ApiImplicitParams annotation. Note: this parameter is ignored when implicitHeaders=true| |null|
64+
|includeHttpRequestContext|Whether to include HttpServletRequest (blocking) or ServerWebExchange (reactive) as additional parameter in generated methods. Defaults to 'true' for reactive and 'false' for blocking.| |false|
6465
|interfaceOnly|Whether to generate only API interface stubs without the server files.| |false|
6566
|invokerPackage|root package for generated code| |org.openapitools.api|
6667
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|false|

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public class SpringCodegen extends AbstractJavaCodegen
9393
public static final String GENERATE_GENERIC_RESPONSE_ENTITY = "generateGenericResponseEntity";
9494
public static final String USE_ENUM_CASE_INSENSITIVE = "useEnumCaseInsensitive";
9595
public static final String USE_SPRING_BOOT3 = "useSpringBoot3";
96+
public static final String INCLUDE_HTTP_REQUEST_CONTEXT = "includeHttpRequestContext";
9697
public static final String REQUEST_MAPPING_OPTION = "requestMappingMode";
9798
public static final String USE_REQUEST_MAPPING_ON_CONTROLLER = "useRequestMappingOnController";
9899
public static final String USE_REQUEST_MAPPING_ON_INTERFACE = "useRequestMappingOnInterface";
@@ -154,6 +155,12 @@ public enum RequestMappingMode {
154155
@Setter protected boolean useEnumCaseInsensitive = false;
155156
@Getter @Setter
156157
protected boolean useSpringBoot3 = false;
158+
@Getter @Setter
159+
private Boolean includeHttpRequestContext = null;
160+
@Getter
161+
private final boolean defaultIncludeHttpRequestContextForReactive = true;
162+
@Getter
163+
private final boolean defaultIncludeHttpRequestContextForBlocking = false;
157164
protected boolean generatedConstructorWithRequiredArgs = true;
158165
@Getter @Setter
159166
protected RequestMappingMode requestMappingMode = RequestMappingMode.controller;
@@ -278,6 +285,9 @@ public SpringCodegen() {
278285
cliOptions.add(CliOption.newBoolean(USE_SPRING_BOOT3,
279286
"Generate code and provide dependencies for use with Spring Boot ≥ 3 (use jakarta instead of javax in imports). Enabling this option will also enable `useJakartaEe`.",
280287
useSpringBoot3));
288+
cliOptions.add(CliOption.newBoolean(INCLUDE_HTTP_REQUEST_CONTEXT,
289+
"Whether to include HttpServletRequest (blocking) or ServerWebExchange (reactive) as additional parameter in generated methods. Defaults to 'true' for reactive and 'false' for blocking.",
290+
defaultIncludeHttpRequestContextForBlocking));
281291
cliOptions.add(CliOption.newBoolean(GENERATE_CONSTRUCTOR_WITH_REQUIRED_ARGS,
282292
"Whether to generate constructors with required args for models",
283293
generatedConstructorWithRequiredArgs));
@@ -429,6 +439,22 @@ public void processOpts() {
429439
convertPropertyToBooleanAndWriteBack(REACTIVE, this::setReactive);
430440
convertPropertyToBooleanAndWriteBack(SSE, this::setSse);
431441
}
442+
if (additionalProperties.containsKey(INCLUDE_HTTP_REQUEST_CONTEXT)) {
443+
convertPropertyToBooleanAndWriteBack(INCLUDE_HTTP_REQUEST_CONTEXT, this::setIncludeHttpRequestContext);
444+
}
445+
//set default value for includeHttpRequestContext based on reactive/blocking
446+
if (includeHttpRequestContext == null) {
447+
if (this.reactive) {
448+
//default to true for reactive
449+
this.setIncludeHttpRequestContext(this.isDefaultIncludeHttpRequestContextForReactive());
450+
LOGGER.info("Defaulting {} to '{}' for reactive", INCLUDE_HTTP_REQUEST_CONTEXT, this.isDefaultIncludeHttpRequestContextForReactive());
451+
} else {
452+
//default to false for blocking
453+
this.setIncludeHttpRequestContext(this.isDefaultIncludeHttpRequestContextForBlocking());
454+
LOGGER.info("Defaulting {} to '{}' for blocking", INCLUDE_HTTP_REQUEST_CONTEXT, this.isDefaultIncludeHttpRequestContextForBlocking());
455+
}
456+
additionalProperties.put(INCLUDE_HTTP_REQUEST_CONTEXT, this.getIncludeHttpRequestContext());
457+
}
432458

433459
convertPropertyToStringAndWriteBack(RESPONSE_WRAPPER, this::setResponseWrapper);
434460
convertPropertyToBooleanAndWriteBack(USE_TAGS, this::setUseTags);

modules/openapi-generator/src/main/resources/JavaSpring/api.mustache

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import io.swagger.v3.oas.annotations.media.ExampleObject;
2424
{{#swagger1AnnotationLibrary}}
2525
import io.swagger.annotations.*;
2626
{{/swagger1AnnotationLibrary}}
27+
{{#springFoxDocumentationProvider}}{{#includeHttpRequestContext}}
28+
import springfox.documentation.annotations.ApiIgnore;
29+
{{/includeHttpRequestContext}}{{/springFoxDocumentationProvider}}
2730
{{^isDelegate}}
2831
{{#jdk8-default-interface}}
2932
{{#virtualService}}
@@ -58,8 +61,15 @@ import org.springframework.web.context.request.NativeWebRequest;
5861
{{/isDelegate}}
5962
{{/jdk8-default-interface}}
6063
import org.springframework.web.multipart.MultipartFile;
64+
{{#includeHttpRequestContext}}
6165
{{#reactive}}
6266
import org.springframework.web.server.ServerWebExchange;
67+
{{/reactive}}
68+
{{^reactive}}
69+
import {{javaxPackage}}.servlet.http.HttpServletRequest;
70+
{{/reactive}}
71+
{{/includeHttpRequestContext}}
72+
{{#reactive}}
6373
import reactor.core.publisher.Flux;
6474
import reactor.core.publisher.Mono;
6575
import org.springframework.http.codec.multipart.Part;
@@ -270,24 +280,24 @@ public interface {{classname}} {
270280
{{/vendorExtensions.x-sse}}
271281
{{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{>responseType}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}(
272282
{{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}},
273-
{{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}},
274-
{{/hasParams}}{{#swagger2AnnotationLibrary}}@Parameter(hidden = true){{/swagger2AnnotationLibrary}}{{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}} final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}{{#hasParams}},
283+
{{/-last}}{{/allParams}}{{#includeHttpRequestContext}}{{#hasParams}},
284+
{{/hasParams}}{{#swagger2AnnotationLibrary}}@Parameter(hidden = true){{/swagger2AnnotationLibrary}}{{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}} final {{#reactive}}ServerWebExchange exchange{{/reactive}}{{^reactive}}HttpServletRequest servletRequest{{/reactive}}{{/includeHttpRequestContext}}{{#vendorExtensions.x-spring-paginated}}{{#hasParams}},
275285
{{/hasParams}}{{^hasParams}}{{#reactive}},{{/reactive}}{{/hasParams}}{{#springFoxDocumentationProvider}}@ApiIgnore {{/springFoxDocumentationProvider}}{{#springDocDocumentationProvider}}@ParameterObject {{/springDocDocumentationProvider}}final Pageable pageable{{/vendorExtensions.x-spring-paginated}}{{#vendorExtensions.x-spring-provide-args}}{{#hasParams}},
276286
{{/hasParams}}{{^hasParams}}{{#reactive}},{{/reactive}}{{/hasParams}}{{#swagger2AnnotationLibrary}}@Parameter(hidden = true){{/swagger2AnnotationLibrary}}{{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}} {{{.}}}{{^hasParams}}{{^-last}}{{^reactive}},{{/reactive}}
277287
{{/-last}}{{/hasParams}}{{/vendorExtensions.x-spring-provide-args}}
278288
){{#unhandledException}} throws Exception{{/unhandledException}}{{^jdk8-default-interface}};{{/jdk8-default-interface}}{{#jdk8-default-interface}} {
279289
{{#delegate-method}}
280-
{{^isVoid}}return {{/isVoid}}{{#isVoid}}{{#useResponseEntity}}return {{/useResponseEntity}}{{^useResponseEntity}}{{#reactive}}return {{/reactive}}{{/useResponseEntity}}{{/isVoid}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}{{#hasParams}}, {{/hasParams}}{{^hasParams}}{{#reactive}}, {{/reactive}}{{/hasParams}}pageable{{/vendorExtensions.x-spring-paginated}});
290+
{{^isVoid}}return {{/isVoid}}{{#isVoid}}{{#useResponseEntity}}return {{/useResponseEntity}}{{^useResponseEntity}}{{#reactive}}return {{/reactive}}{{/useResponseEntity}}{{/isVoid}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#includeHttpRequestContext}}{{#hasParams}}, {{/hasParams}}{{#reactive}}exchange{{/reactive}}{{^reactive}}servletRequest{{/reactive}}{{/includeHttpRequestContext}}{{#vendorExtensions.x-spring-paginated}}{{#hasParams}}, {{/hasParams}}{{^hasParams}}{{#reactive}}, {{/reactive}}{{/hasParams}}pageable{{/vendorExtensions.x-spring-paginated}});
281291
}
282292

283293
// Override this method
284-
{{#jdk8-default-interface}}default {{/jdk8-default-interface}} {{>responseType}} {{operationId}}({{#allParams}}{{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}Mono<{{{dataType}}}>{{/isArray}}{{#isArray}}Flux<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#reactive}}{{#isArray}}Flux<{{/isArray}}Part{{#isArray}}>{{/isArray}}{{/reactive}}{{^reactive}}{{#isArray}}List<{{/isArray}}MultipartFile{{#isArray}}>{{/isArray}}{{/reactive}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}{{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}} final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}{{#hasParams}}, {{/hasParams}}{{^hasParams}}{{#reactive}}, {{/reactive}}{{/hasParams}}{{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}}final Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{#unhandledException}} throws Exception{{/unhandledException}} {
294+
{{#jdk8-default-interface}}default {{/jdk8-default-interface}} {{>responseType}} {{operationId}}({{#allParams}}{{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}Mono<{{{dataType}}}>{{/isArray}}{{#isArray}}Flux<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#reactive}}{{#isArray}}Flux<{{/isArray}}Part{{#isArray}}>{{/isArray}}{{/reactive}}{{^reactive}}{{#isArray}}List<{{/isArray}}MultipartFile{{#isArray}}>{{/isArray}}{{/reactive}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#includeHttpRequestContext}}{{#hasParams}}, {{/hasParams}}{{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}} final {{#reactive}}ServerWebExchange exchange{{/reactive}}{{^reactive}}HttpServletRequest servletRequest{{/reactive}}{{/includeHttpRequestContext}}{{#vendorExtensions.x-spring-paginated}}{{#hasParams}}, {{/hasParams}}{{^hasParams}}{{#reactive}}, {{/reactive}}{{/hasParams}}{{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}}final Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{#unhandledException}} throws Exception{{/unhandledException}} {
285295
{{/delegate-method}}
286296
{{^isDelegate}}
287297
{{>methodBody}}{{! prevent indent}}
288298
{{/isDelegate}}
289299
{{#isDelegate}}
290-
{{^isVoid}}return {{/isVoid}}{{#isVoid}}{{#useResponseEntity}}return {{/useResponseEntity}}{{^useResponseEntity}}{{#reactive}}return {{/reactive}}{{/useResponseEntity}}{{/isVoid}}getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}{{#hasParams}}, {{/hasParams}}{{^hasParams}}{{#reactive}}, {{/reactive}}{{/hasParams}}pageable{{/vendorExtensions.x-spring-paginated}});
300+
{{^isVoid}}return {{/isVoid}}{{#isVoid}}{{#useResponseEntity}}return {{/useResponseEntity}}{{^useResponseEntity}}{{#reactive}}return {{/reactive}}{{/useResponseEntity}}{{/isVoid}}getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#includeHttpRequestContext}}{{#hasParams}}, {{/hasParams}}{{#reactive}}exchange{{/reactive}}{{^reactive}}servletRequest{{/reactive}}{{/includeHttpRequestContext}}{{#vendorExtensions.x-spring-paginated}}{{#hasParams}}, {{/hasParams}}{{^hasParams}}{{#reactive}}, {{/reactive}}{{/hasParams}}pageable{{/vendorExtensions.x-spring-paginated}});
291301
{{/isDelegate}}
292302
}{{/jdk8-default-interface}}
293303

0 commit comments

Comments
 (0)