Skip to content

Commit 7c4ada3

Browse files
[Java] [SpringClient] Introduce setting for sealed oneOf interfaces for Spring clients (#21586)
* Add setting for generating the oneOf interfaces as sealed interfaces * Generate samples * Add internal java17 additionalProperty setting * Move samples to highlight that they do not use the petstore * Align documentation * Update samples * Align documentation * Update mustache files and samples to change gradle settings to java17
1 parent 3453c7b commit 7c4ada3

File tree

104 files changed

+7403
-45
lines changed

Some content is hidden

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

104 files changed

+7403
-45
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ on:
66
- samples/client/petstore/java/resttemplate-jakarta/**
77
- samples/client/petstore/java/webclient-jakarta/**
88
- samples/client/petstore/java/restclient-*/**
9+
- samples/client/others/java/webclient-sealedInterface/**
910
- samples/client/petstore/java/webclient-useSingleRequestParameter/**
1011
- samples/client/others/java/restclient-enum-in-multipart/**
1112
pull_request:
1213
paths:
1314
- samples/client/petstore/java/resttemplate-jakarta/**
1415
- samples/client/petstore/java/webclient-jakarta/**
1516
- samples/client/petstore/java/restclient-*/**
17+
- samples/client/others/java/webclient-sealedInterface/**
1618
- samples/client/petstore/java/webclient-useSingleRequestParameter/**
1719
- samples/client/others/java/restclient-enum-in-multipart/**
1820
jobs:
@@ -31,6 +33,7 @@ jobs:
3133
- samples/client/petstore/java/restclient-swagger2
3234
- samples/client/petstore/java/restclient-useSingleRequestParameter
3335
- samples/client/petstore/java/restclient-useSingleRequestParameter-static
36+
- samples/client/others/java/webclient-sealedInterface
3437
- samples/client/petstore/java/webclient-useSingleRequestParameter
3538
- samples/client/others/java/restclient-enum-in-multipart
3639
steps:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
generatorName: java
2+
outputDir: samples/client/others/java/webclient-sealedInterface
3+
library: webclient
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/oneof_polymorphism_and_inheritance.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/Java
6+
additionalProperties:
7+
artifactId: sealed-interface-webclient
8+
hideGenerationTimestamp: "true"
9+
useOneOfInterfaces: true
10+
useSealedOneOfInterfaces: true

docs/generators/java-microprofile.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
103103
|useRuntimeException|Use RuntimeException instead of Exception. Only jersey2, jersey3, okhttp-gson, vertx, microprofile support this option.| |false|
104104
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
105105
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
106+
|useSealedOneOfInterfaces|Generate the oneOf interfaces as sealed interfaces. Only supported for WebClient and RestClient.| |false|
106107
|useSingleRequestParameter|Setting this property to "true" will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter. ONLY native, jersey2, jersey3, okhttp-gson, microprofile, Spring RestClient, Spring WebClient support this option. Setting this property to "static" does the same as "true", but also makes the generated arguments class static with single parameter instantiation.| |false|
107108
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
108109
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|

docs/generators/java.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
103103
|useRuntimeException|Use RuntimeException instead of Exception. Only jersey2, jersey3, okhttp-gson, vertx, microprofile support this option.| |false|
104104
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
105105
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
106+
|useSealedOneOfInterfaces|Generate the oneOf interfaces as sealed interfaces. Only supported for WebClient and RestClient.| |false|
106107
|useSingleRequestParameter|Setting this property to "true" will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter. ONLY native, jersey2, jersey3, okhttp-gson, microprofile, Spring RestClient, Spring WebClient support this option. Setting this property to "static" does the same as "true", but also makes the generated arguments class static with single parameter instantiation.| |false|
107108
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
108109
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public class JavaClientCodegen extends AbstractJavaCodegen
104104
public static final String USE_ENUM_CASE_INSENSITIVE = "useEnumCaseInsensitive";
105105
public static final String FAIL_ON_UNKNOWN_PROPERTIES = "failOnUnknownProperties";
106106
public static final String SUPPORT_VERTX_FUTURE = "supportVertxFuture";
107+
public static final String USE_SEALED_ONE_OF_INTERFACES = "useSealedOneOfInterfaces";
108+
109+
// Internal configurations
110+
public static final String SINGLE_REQUEST_PARAMETER = "singleRequestParameter";
111+
public static final String STATIC_REQUEST = "staticRequest";
112+
public static final String JAVA_17 = "java17";
107113

108114
public static final String SERIALIZATION_LIBRARY_GSON = "gson";
109115
public static final String SERIALIZATION_LIBRARY_JACKSON = "jackson";
@@ -142,6 +148,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
142148
@Setter protected String errorObjectType;
143149
@Getter @Setter protected boolean failOnUnknownProperties = false;
144150
@Setter protected boolean supportVertxFuture = false;
151+
@Setter protected boolean useSealedOneOfInterfaces = false;
145152
protected String authFolder;
146153
/**
147154
* Serialization library.
@@ -252,6 +259,7 @@ public JavaClientCodegen() {
252259
cliOptions.add(CliOption.newBoolean(USE_ENUM_CASE_INSENSITIVE, "Use `equalsIgnoreCase` when String for enum comparison", useEnumCaseInsensitive));
253260
cliOptions.add(CliOption.newBoolean(FAIL_ON_UNKNOWN_PROPERTIES, "Fail Jackson de-serialization on unknown properties", this.failOnUnknownProperties));
254261
cliOptions.add(CliOption.newBoolean(SUPPORT_VERTX_FUTURE, "Also generate api methods that return a vertx Future instead of taking a callback. Only `vertx` supports this option. Requires vertx 4 or greater.", this.supportVertxFuture));
262+
cliOptions.add(CliOption.newBoolean(USE_SEALED_ONE_OF_INTERFACES, "Generate the oneOf interfaces as sealed interfaces. Only supported for WebClient and RestClient.", this.useSealedOneOfInterfaces));
255263

256264
supportedLibraries.put(JERSEY2, "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.17.1");
257265
supportedLibraries.put(JERSEY3, "HTTP client: Jersey client 3.1.1. JSON processing: Jackson 2.17.1");
@@ -367,8 +375,13 @@ public void processOpts() {
367375
convertPropertyToBooleanAndWriteBack(USE_RX_JAVA2, this::setUseRxJava2);
368376
}
369377
convertPropertyToStringAndWriteBack(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, this::setUseSingleRequestParameter);
370-
writePropertyBack("singleRequestParameter", getSingleRequestParameter());
371-
writePropertyBack("staticRequest", getStaticRequest());
378+
convertPropertyToBooleanAndWriteBack(USE_SEALED_ONE_OF_INTERFACES, this::setUseSealedOneOfInterfaces);
379+
writePropertyBack(SINGLE_REQUEST_PARAMETER, getSingleRequestParameter());
380+
writePropertyBack(STATIC_REQUEST, getStaticRequest());
381+
382+
if (libWebClient && (useSealedOneOfInterfaces || useJakartaEe)) {
383+
writePropertyBack(JAVA_17, true);
384+
}
372385

373386
if (!useRxJava && !useRxJava2 && !useRxJava3) {
374387
additionalProperties.put(DO_NOT_USE_RX, true);

modules/openapi-generator/src/main/resources/Java/libraries/restclient/pojo.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
{{#vendorExtensions.x-class-extra-annotation}}
3030
{{{vendorExtensions.x-class-extra-annotation}}}
3131
{{/vendorExtensions.x-class-extra-annotation}}
32-
public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtensions.x-implements}}{{#-first}}implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{
32+
public {{>sealed}}class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtensions.x-implements}}{{#-first}}implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{{>permits}}{
3333
{{#serializableModel}}
3434
private static final long serialVersionUID = 1L;
3535

modules/openapi-generator/src/main/resources/Java/libraries/webclient/build.gradle.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ if(hasProperty('target') && target == 'android') {
3232
}
3333

3434
compileOptions {
35-
{{#useJakartaEe}}
35+
{{#java17}}
3636
sourceCompatibility JavaVersion.VERSION_17
3737
targetCompatibility JavaVersion.VERSION_17
38-
{{/useJakartaEe}}
39-
{{^useJakartaEe}}
38+
{{/java17}}
39+
{{^java17}}
4040
sourceCompatibility JavaVersion.VERSION_1_8
4141
targetCompatibility JavaVersion.VERSION_1_8
42-
{{/useJakartaEe}}
42+
{{/java17}}
4343
}
4444

4545
// Rename the aar correctly
@@ -84,14 +84,14 @@ if(hasProperty('target') && target == 'android') {
8484
apply plugin: 'java'
8585
apply plugin: 'maven-publish'
8686
87-
{{#useJakartaEe}}
87+
{{#java17}}
8888
sourceCompatibility = JavaVersion.VERSION_17
8989
targetCompatibility = JavaVersion.VERSION_17
90-
{{/useJakartaEe}}
91-
{{^useJakartaEe}}
90+
{{/java17}}
91+
{{^java17}}
9292
sourceCompatibility = JavaVersion.VERSION_1_8
9393
targetCompatibility = JavaVersion.VERSION_1_8
94-
{{/useJakartaEe}}
94+
{{/java17}}
9595

9696
publishing {
9797
publications {

modules/openapi-generator/src/main/resources/Java/libraries/webclient/pojo.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
{{#vendorExtensions.x-class-extra-annotation}}
3030
{{{vendorExtensions.x-class-extra-annotation}}}
3131
{{/vendorExtensions.x-class-extra-annotation}}
32-
public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtensions.x-implements}}{{#-first}}implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{
32+
public {{>sealed}}class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtensions.x-implements}}{{#-first}}implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{{>permits}}{
3333
{{#serializableModel}}
3434
private static final long serialVersionUID = 1L;
3535

modules/openapi-generator/src/main/resources/Java/libraries/webclient/pom.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
<artifactId>maven-compiler-plugin</artifactId>
4646
<version>3.13.0</version>
4747
<configuration>
48-
{{#useJakartaEe}}
48+
{{#java17}}
4949
<source>17</source>
5050
<target>17</target>
51-
{{/useJakartaEe}}
52-
{{^useJakartaEe}}
51+
{{/java17}}
52+
{{^java17}}
5353
<source>1.8</source>
5454
<target>1.8</target>
55-
{{/useJakartaEe}}
55+
{{/java17}}
5656
</configuration>
5757
</plugin>
5858
<plugin>

modules/openapi-generator/src/main/resources/Java/oneof_interface.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{>additionalOneOfTypeAnnotations}}{{>generatedAnnotation}}{{>typeInfoAnnotation}}{{>xmlAnnotation}}
2-
public interface {{classname}} {{#vendorExtensions.x-implements}}{{#-first}}extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {
2+
public {{>sealed}}interface {{classname}} {{#vendorExtensions.x-implements}}{{#-first}}extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}}{{>permits}}{
33
{{#discriminator}}
44
public {{propertyType}} {{propertyGetter}}();
55
{{/discriminator}}

0 commit comments

Comments
 (0)