Skip to content

Commit 1dd8452

Browse files
Add setting for generating the oneOf interfaces as sealed interfaces
1 parent f632ab7 commit 1dd8452

11 files changed

Lines changed: 144 additions & 37 deletions

File tree

.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/petstore/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/petstore/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/petstore/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/petstore/java/webclient-sealedInterface
3+
library: webclient
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.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.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
9898
|useJakartaEe|whether to use Jakarta EE namespace instead of javax| |false|
9999
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped. Only jersey2, jersey3, native, okhttp-gson support this option.| |false|
100100
|useOneOfInterfaces|whether to use a java interface to describe a set of oneOf options, where each option is a class that implements the interface| |false|
101+
|useSealedOneOfInterfaces|Generate the oneOf interfaces as sealed interfaces. Only supported for WebClient and RestClient.| |false|
101102
|usePlayWS|Use Play! Async HTTP client (Play WS API)| |false|
102103
|useReflectionEqualsHashCode|Use org.apache.commons.lang3.builder for equals and hashCode in the models. WARNING: This will fail under a security manager, unless the appropriate permissions are set up correctly and also there's potential performance impact.| |false|
103104
|useRuntimeException|Use RuntimeException instead of Exception. Only jersey2, jersey3, okhttp-gson, vertx, microprofile support this option.| |false|

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ 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+
// TODO attempt to implement the setting similar to how it is done here https://github.com/OpenAPITools/openapi-generator/pull/20590/files
113+
public static final String JAVA_17 = "java17";
107114

108115
public static final String SERIALIZATION_LIBRARY_GSON = "gson";
109116
public static final String SERIALIZATION_LIBRARY_JACKSON = "jackson";
@@ -142,6 +149,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
142149
@Setter protected String errorObjectType;
143150
@Getter @Setter protected boolean failOnUnknownProperties = false;
144151
@Setter protected boolean supportVertxFuture = false;
152+
@Setter protected boolean useSealedOneOfInterfaces = false;
145153
protected String authFolder;
146154
/**
147155
* Serialization library.
@@ -252,6 +260,7 @@ public JavaClientCodegen() {
252260
cliOptions.add(CliOption.newBoolean(USE_ENUM_CASE_INSENSITIVE, "Use `equalsIgnoreCase` when String for enum comparison", useEnumCaseInsensitive));
253261
cliOptions.add(CliOption.newBoolean(FAIL_ON_UNKNOWN_PROPERTIES, "Fail Jackson de-serialization on unknown properties", this.failOnUnknownProperties));
254262
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));
263+
cliOptions.add(CliOption.newBoolean(USE_SEALED_ONE_OF_INTERFACES, "Generate the oneOf interfaces as sealed interfaces", this.useSealedOneOfInterfaces));
255264

256265
supportedLibraries.put(JERSEY2, "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.17.1");
257266
supportedLibraries.put(JERSEY3, "HTTP client: Jersey client 3.1.1. JSON processing: Jackson 2.17.1");
@@ -367,8 +376,9 @@ public void processOpts() {
367376
convertPropertyToBooleanAndWriteBack(USE_RX_JAVA2, this::setUseRxJava2);
368377
}
369378
convertPropertyToStringAndWriteBack(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, this::setUseSingleRequestParameter);
370-
writePropertyBack("singleRequestParameter", getSingleRequestParameter());
371-
writePropertyBack("staticRequest", getStaticRequest());
379+
convertPropertyToBooleanAndWriteBack(USE_SEALED_ONE_OF_INTERFACES, this::setUseSealedOneOfInterfaces);
380+
writePropertyBack(SINGLE_REQUEST_PARAMETER, getSingleRequestParameter());
381+
writePropertyBack(STATIC_REQUEST, getStaticRequest());
372382

373383
if (!useRxJava && !useRxJava2 && !useRxJava3) {
374384
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/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}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#useSealedOneOfInterfaces}}{{#vendorExtensions.x-is-one-of-interface}}{{#permits}}{{#-first}}permits {{/-first}}{{{.}}}{{^-last}}, {{/-last}}{{#-last}} {{/-last}}{{/permits}}{{/vendorExtensions.x-is-one-of-interface}}{{/useSealedOneOfInterfaces}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#useSealedOneOfInterfaces}}{{#permits.0}}sealed {{/permits.0}}{{^permits.0}}{{^vendorExtensions.x-is-one-of-interface}}{{#vendorExtensions.x-implements}}final {{/vendorExtensions.x-implements}}{{/vendorExtensions.x-is-one-of-interface}}{{/permits.0}}{{/useSealedOneOfInterfaces}}

0 commit comments

Comments
 (0)