Skip to content

Commit 5221682

Browse files
[Java][Client] Support annotationLibrary=none to remove swagger annotations (OpenAPITools#13869)
* Support annotationLibrary=none in JavaClientCodegen * Add example using annotationLibrary=swagger1 * Support annotationLibrary=none in libraries * Fix missing curly brace. * fix if statement condition * Support {{#swagger1AnnotationLibrary}} in java/rest-assured * Adopt JavaModelTest * Generate docs * Generate samples * clean up java feign files * clean up feign samples * fix resttemplate, native * fix resttemplate withXml * fix webclient * fix java-jersey2, vertix * fix googleapi client * fix rest assured * fix rest assured * update apache-httpclient * fix jersey2 special character * fix resteasy * fix jersey2 * update samples * fix jersey2, okhttp streaming * update okhttp-gson * update samples Co-authored-by: William Cheng <[email protected]>
1 parent c3abdb6 commit 5221682

File tree

2,318 files changed

+2126
-17418
lines changed

Some content is hidden

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

2,318 files changed

+2126
-17418
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
generatorName: java
2+
outputDir: samples/client/petstore/java/resttemplate
3+
library: resttemplate
4+
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/Java
6+
additionalProperties:
7+
artifactId: petstore-resttemplate
8+
hideGenerationTimestamp: "true"
9+
annotationLibrary: "swagger1"
10+
java8: true

docs/generators/java.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2121
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations)| |null|
2222
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2323
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
24+
|annotationLibrary|Select the complementary documentation annotation library.|<dl><dt>**none**</dt><dd>Do not annotate Model and Api with complementary annotations.</dd><dt>**swagger1**</dt><dd>Annotate Model and Api using the Swagger Annotations 1.x library.</dd></dl>|none|
2425
|apiPackage|package for generated api classes| |org.openapitools.client.api|
2526
|artifactDescription|artifact description in generated pom.xml| |OpenAPI Java|
2627
|artifactId|artifactId in generated pom.xml. This also becomes part of the generated library's filename| |openapi-java-client|
@@ -40,6 +41,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4041
|disableHtmlEscaping|Disable HTML escaping of JSON strings when using gson (needed to avoid problems with byte[] fields)| |false|
4142
|disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
4243
|discriminatorCaseSensitive|Whether the discriminator value lookup should be case-sensitive or not. This option only works for Java API client| |true|
44+
|documentationProvider|Select the OpenAPI documentation provider.|<dl><dt>**none**</dt><dd>Do not publish an OpenAPI specification.</dd><dt>**source**</dt><dd>Publish the original input OpenAPI specification.</dd></dl>|source|
4345
|dynamicOperations|Generate operations dynamically at runtime from an OAS| |false|
4446
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
4547
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|

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

+29
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ public MpRestClientVersion(String rootPackage, String pomTemplate) {
137137
}
138138
}
139139

140+
@Override
141+
public DocumentationProvider defaultDocumentationProvider() {
142+
return DocumentationProvider.SOURCE;
143+
}
144+
145+
@Override
146+
public List<DocumentationProvider> supportedDocumentationProvider() {
147+
List<DocumentationProvider> documentationProviders = new ArrayList<>();
148+
documentationProviders.add(DocumentationProvider.NONE);
149+
documentationProviders.add(DocumentationProvider.SOURCE);
150+
return documentationProviders;
151+
}
152+
153+
@Override
154+
public List<AnnotationLibrary> supportedAnnotationLibraries() {
155+
List<AnnotationLibrary> annotationLibraries = new ArrayList<>();
156+
annotationLibraries.add(AnnotationLibrary.NONE);
157+
annotationLibraries.add(AnnotationLibrary.SWAGGER1);
158+
return annotationLibraries;
159+
}
160+
140161
public JavaClientCodegen() {
141162
super();
142163

@@ -886,6 +907,14 @@ public CodegenModel fromModel(String name, Schema model) {
886907
codegenModel.imports.remove("ApiModel");
887908
}
888909
}
910+
911+
// TODO: inverse logic. Do not add the imports unconditionally in the first place.
912+
if (! AnnotationLibrary.SWAGGER1.equals(getAnnotationLibrary())) {
913+
// Remove io.swagger.annotations.* imports
914+
codegenModel.imports.remove("ApiModel");
915+
codegenModel.imports.remove("ApiModelProperty");
916+
}
917+
889918
return codegenModel;
890919
}
891920

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,13 @@
214214
</profiles>
215215

216216
<dependencies>
217+
{{#swagger1AnnotationLibrary}}
217218
<dependency>
218219
<groupId>io.swagger</groupId>
219220
<artifactId>swagger-annotations</artifactId>
220221
<version>${swagger-annotations-version}</version>
221222
</dependency>
223+
{{/swagger1AnnotationLibrary}}
222224

223225
<!-- @Nullable annotation -->
224226
<dependency>
@@ -325,7 +327,7 @@
325327
</dependencies>
326328
<properties>
327329
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
328-
<swagger-annotations-version>1.5.21</swagger-annotations-version>
330+
<swagger-annotations-version>1.6.6</swagger-annotations-version>
329331
<httpclient-version>4.5.13</httpclient-version>
330332
<jackson-version>2.13.4</jackson-version>
331333
<jackson-databind-version>2.13.4.2</jackson-databind-version>

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,13 @@
213213
</profiles>
214214

215215
<dependencies>
216+
{{#swagger1AnnotationLibrary}}
216217
<dependency>
217218
<groupId>io.swagger</groupId>
218219
<artifactId>swagger-annotations</artifactId>
219220
<version>${swagger-annotations-version}</version>
220221
</dependency>
222+
{{/swagger1AnnotationLibrary}}
221223

222224
<!-- @Nullable annotation -->
223225
<dependency>
@@ -351,7 +353,7 @@
351353
<java.version>1.8</java.version>
352354
<maven.compiler.source>${java.version}</maven.compiler.source>
353355
<maven.compiler.target>${java.version}</maven.compiler.target>
354-
<swagger-annotations-version>1.5.24</swagger-annotations-version>
356+
<swagger-annotations-version>1.6.6</swagger-annotations-version>
355357
<feign-version>10.11</feign-version>
356358
<feign-form-version>3.8.0</feign-form-version>
357359
<jackson-version>2.13.4</jackson-version>

modules/openapi-generator/src/main/resources/Java/libraries/google-api-client/pom.mustache

+3-1
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@
206206
</profiles>
207207

208208
<dependencies>
209+
{{#swagger1AnnotationLibrary}}
209210
<dependency>
210211
<groupId>io.swagger</groupId>
211212
<artifactId>swagger-annotations</artifactId>
212213
<version>${swagger-annotations-version}</version>
213214
</dependency>
215+
{{/swagger1AnnotationLibrary}}
214216
<!-- @Nullable annotation -->
215217
<dependency>
216218
<groupId>com.google.code.findbugs</groupId>
@@ -294,7 +296,7 @@
294296
</dependencies>
295297
<properties>
296298
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
297-
<swagger-annotations-version>1.5.22</swagger-annotations-version>
299+
<swagger-annotations-version>1.6.6</swagger-annotations-version>
298300
<google-api-client-version>1.32.2</google-api-client-version>
299301
<jersey-common-version>2.25.1</jersey-common-version>
300302
<jackson-version>2.13.4</jackson-version>

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
* {{description}}{{^description}}{{classname}}{{/description}}{{#isDeprecated}}
33
* @deprecated{{/isDeprecated}}
44
*/{{#isDeprecated}}
5-
@Deprecated{{/isDeprecated}}{{#description}}
6-
@ApiModel(description = "{{{.}}}"){{/description}}
5+
@Deprecated{{/isDeprecated}}
6+
{{#swagger1AnnotationLibrary}}
7+
{{#description}}
8+
@ApiModel(description = "{{{.}}}")
9+
{{/description}}
10+
{{/swagger1AnnotationLibrary}}
711
{{#jackson}}
812
@JsonPropertyOrder({
913
{{#vars}}
@@ -211,7 +215,12 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
211215
{{^required}}
212216
@javax.annotation.Nullable
213217
{{/required}}
214-
{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}} @ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
218+
{{#useBeanValidation}}
219+
{{>beanValidation}}
220+
{{/useBeanValidation}}
221+
{{#swagger1AnnotationLibrary}}
222+
@ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
223+
{{/swagger1AnnotationLibrary}}
215224
{{#vendorExtensions.x-extra-annotation}}
216225
{{{vendorExtensions.x-extra-annotation}}}
217226
{{/vendorExtensions.x-extra-annotation}}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,13 @@
258258
</profiles>
259259
260260
<dependencies>
261+
{{#swagger1AnnotationLibrary}}
261262
<dependency>
262263
<groupId>io.swagger</groupId>
263264
<artifactId>swagger-annotations</artifactId>
264265
<version>${swagger-annotations-version}</version>
265266
</dependency>
267+
{{/swagger1AnnotationLibrary}}
266268
267269
<!-- @Nullable annotation -->
268270
<dependency>
@@ -380,7 +382,7 @@
380382
</dependencies>
381383
<properties>
382384
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
383-
<swagger-annotations-version>1.6.5</swagger-annotations-version>
385+
<swagger-annotations-version>1.6.6</swagger-annotations-version>
384386
<jersey-version>2.35</jersey-version>
385387
<jackson-version>2.13.4</jackson-version>
386388
<jackson-databind-version>2.13.4.2</jackson-databind-version>

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
* {{description}}{{^description}}{{classname}}{{/description}}{{#isDeprecated}}
33
* @deprecated{{/isDeprecated}}
44
*/{{#isDeprecated}}
5-
@Deprecated{{/isDeprecated}}{{#description}}
6-
@ApiModel(description = "{{{.}}}"){{/description}}
5+
@Deprecated{{/isDeprecated}}
6+
{{#swagger1AnnotationLibrary}}
7+
{{#description}}
8+
@ApiModel(description = "{{{.}}}")
9+
{{/description}}
10+
{{/swagger1AnnotationLibrary}}
711
{{#jackson}}
812
@JsonPropertyOrder({
913
{{#vars}}
@@ -211,7 +215,12 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
211215
{{^required}}
212216
@jakarta.annotation.Nullable
213217
{{/required}}
214-
{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}} @ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
218+
{{#useBeanValidation}}
219+
{{>beanValidation}}
220+
{{/useBeanValidation}}
221+
{{#swagger1AnnotationLibrary}}
222+
@ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
223+
{{/swagger1AnnotationLibrary}}
215224
{{#vendorExtensions.x-extra-annotation}}
216225
{{{vendorExtensions.x-extra-annotation}}}
217226
{{/vendorExtensions.x-extra-annotation}}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,13 @@
258258
</profiles>
259259
260260
<dependencies>
261+
{{#swagger1AnnotationLibrary}}
261262
<dependency>
262263
<groupId>io.swagger</groupId>
263264
<artifactId>swagger-annotations</artifactId>
264265
<version>${swagger-annotations-version}</version>
265266
</dependency>
267+
{{/swagger1AnnotationLibrary}}
266268
267269
<!-- @Nullable annotation -->
268270
<dependency>
@@ -380,7 +382,7 @@
380382
</dependencies>
381383
<properties>
382384
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
383-
<swagger-annotations-version>1.6.5</swagger-annotations-version>
385+
<swagger-annotations-version>1.6.6</swagger-annotations-version>
384386
<jersey-version>3.0.4</jersey-version>
385387
<jackson-version>2.13.4</jackson-version>
386388
<jackson-databind-version>2.13.4.2</jackson-databind-version>

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import {{invokerPackage}}.JSON;
55
* {{description}}{{^description}}{{classname}}{{/description}}{{#isDeprecated}}
66
* @deprecated{{/isDeprecated}}
77
*/{{#isDeprecated}}
8-
@Deprecated{{/isDeprecated}}{{#description}}
9-
@ApiModel(description = "{{{.}}}"){{/description}}
8+
@Deprecated{{/isDeprecated}}
9+
{{#swagger1AnnotationLibrary}}
10+
{{#description}}
11+
@ApiModel(description = "{{{.}}}")
12+
{{/description}}
13+
{{/swagger1AnnotationLibrary}}
1014
{{#jackson}}
1115
@JsonPropertyOrder({
1216
{{#vars}}
@@ -211,7 +215,12 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
211215
{{^required}}
212216
@javax.annotation.Nullable
213217
{{/required}}
214-
{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}} @ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
218+
{{#useBeanValidation}}
219+
{{>beanValidation}}
220+
{{/useBeanValidation}}
221+
{{#swagger1AnnotationLibrary}}
222+
@ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
223+
{{/swagger1AnnotationLibrary}}
215224
{{#vendorExtensions.x-extra-annotation}}
216225
{{{vendorExtensions.x-extra-annotation}}}
217226
{{/vendorExtensions.x-extra-annotation}}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,13 @@
161161
</profiles>
162162

163163
<dependencies>
164+
{{#swagger1AnnotationLibrary}}
164165
<dependency>
165166
<groupId>io.swagger</groupId>
166167
<artifactId>swagger-annotations</artifactId>
167168
<version>${swagger-annotations-version}</version>
168169
</dependency>
170+
{{/swagger1AnnotationLibrary}}
169171

170172
<!-- JSON processing: jackson -->
171173
<dependency>
@@ -218,7 +220,7 @@
218220

219221
<properties>
220222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
221-
<swagger-annotations-version>1.5.22</swagger-annotations-version>
223+
<swagger-annotations-version>1.6.6</swagger-annotations-version>
222224
<maven.compiler.source>11</maven.compiler.source>
223225
<maven.compiler.target>11</maven.compiler.target>
224226
<jackson-version>2.13.4</jackson-version>

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ import {{invokerPackage}}.JSON;
2323
* {{description}}{{^description}}{{classname}}{{/description}}{{#isDeprecated}}
2424
* @deprecated{{/isDeprecated}}
2525
*/{{#isDeprecated}}
26-
@Deprecated{{/isDeprecated}}{{#description}}
27-
@ApiModel(description = "{{{.}}}"){{/description}}
26+
@Deprecated{{/isDeprecated}}
27+
{{#swagger1AnnotationLibrary}}
28+
{{#description}}
29+
@ApiModel(description = "{{{.}}}")
30+
{{/description}}
31+
{{/swagger1AnnotationLibrary}}
2832
{{#jackson}}
2933
@JsonPropertyOrder({
3034
{{#vars}}
@@ -231,7 +235,12 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
231235
{{#jsonb}}
232236
@JsonbProperty("{{baseName}}")
233237
{{/jsonb}}
234-
{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}} @ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
238+
{{#useBeanValidation}}
239+
{{>beanValidation}}
240+
{{/useBeanValidation}}
241+
{{#swagger1AnnotationLibrary}}
242+
@ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
243+
{{/swagger1AnnotationLibrary}}
235244
{{#vendorExtensions.x-extra-annotation}}
236245
{{{vendorExtensions.x-extra-annotation}}}
237246
{{/vendorExtensions.x-extra-annotation}}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,13 @@
265265
</profiles>
266266
267267
<dependencies>
268+
{{#swagger1AnnotationLibrary}}
268269
<dependency>
269270
<groupId>io.swagger</groupId>
270271
<artifactId>swagger-annotations</artifactId>
271-
<version>${swagger-core-version}</version>
272+
<version>${swagger-annotations-version}</version>
272273
</dependency>
274+
{{/swagger1AnnotationLibrary}}
273275
<!-- @Nullable annotation -->
274276
<dependency>
275277
<groupId>com.google.code.findbugs</groupId>
@@ -401,7 +403,7 @@
401403
<maven.compiler.source>${java.version}</maven.compiler.source>
402404
<maven.compiler.target>${java.version}</maven.compiler.target>
403405
<gson-fire-version>1.8.5</gson-fire-version>
404-
<swagger-core-version>1.6.5</swagger-core-version>
406+
<swagger-annotations-version>1.6.6</swagger-annotations-version>
405407
<okhttp-version>4.10.0</okhttp-version>
406408
<gson-version>2.9.1</gson-version>
407409
<commons-lang3-version>3.12.0</commons-lang3-version>

modules/openapi-generator/src/main/resources/Java/libraries/rest-assured/api.mustache

+6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import io.restassured.common.mapper.TypeRef;
2222
{{/jackson}}
2323
import io.restassured.http.Method;
2424
import io.restassured.response.Response;
25+
{{#swagger1AnnotationLibrary}}
2526
import io.swagger.annotations.*;
27+
{{/swagger1AnnotationLibrary}}
2628

2729
import java.lang.reflect.Type;
2830
import java.util.function.Consumer;
@@ -34,7 +36,9 @@ import {{invokerPackage}}.JSON;
3436
{{/gson}}
3537
import static io.restassured.http.Method.*;
3638

39+
{{#swagger1AnnotationLibrary}}
3740
@Api(value = "{{{baseName}}}")
41+
{{/swagger1AnnotationLibrary}}
3842
public class {{classname}} {
3943
4044
private Supplier<RequestSpecBuilder> reqSpecSupplier;
@@ -68,12 +72,14 @@ public class {{classname}} {
6872
{{#operations}}
6973
{{#operation}}
7074

75+
{{#swagger1AnnotationLibrary}}
7176
@ApiOperation(value = "{{{summary}}}",
7277
notes = "{{{notes}}}",
7378
nickname = "{{{operationId}}}",
7479
tags = { {{#tags}}{{#name}}"{{{.}}}"{{/name}}{{^-last}}, {{/-last}}{{/tags}} })
7580
@ApiResponses(value = { {{#responses}}
7681
@ApiResponse(code = {{{code}}}, message = "{{{message}}}") {{^-last}},{{/-last}}{{/responses}} })
82+
{{/swagger1AnnotationLibrary}}
7783
{{#isDeprecated}}
7884
@Deprecated
7985
{{/isDeprecated}}

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

+2
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,13 @@
219219
{{/jackson}}
220220

221221
<dependencies>
222+
{{#swagger1AnnotationLibrary}}
222223
<dependency>
223224
<groupId>io.swagger</groupId>
224225
<artifactId>swagger-annotations</artifactId>
225226
<version>${swagger-annotations-version}</version>
226227
</dependency>
228+
{{/swagger1AnnotationLibrary}}
227229
<!-- @Nullable annotation -->
228230
<dependency>
229231
<groupId>com.google.code.findbugs</groupId>

0 commit comments

Comments
 (0)