Skip to content

Commit f02acaf

Browse files
[cxf] add support for Swagger2 (#22010)
* [cxf] add support for Swagger2 Closes #3668 * regen samples * regen docs * unconditionally include parameter imports * Add swagger2 update samples --------- Co-authored-by: Jens Kleine-Herzbruch <[email protected]>
1 parent 75804e4 commit f02acaf

File tree

102 files changed

+599
-228
lines changed

Some content is hidden

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

102 files changed

+599
-228
lines changed

docs/generators/jaxrs-cxf-client.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2222
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2323
|additionalOneOfTypeAnnotations|Additional annotations for oneOf interfaces(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2424
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
25+
|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><dt>**swagger2**</dt><dd>Annotate Model and Api using the Swagger Annotations 2.x library.</dd></dl>|swagger1|
2526
|apiPackage|package for generated api classes| |org.openapitools.api|
2627
|artifactDescription|artifact description in generated pom.xml| |OpenAPI Java|
2728
|artifactId|artifactId in generated pom.xml. This also becomes part of the generated library's filename| |openapi-jaxrs-client|
@@ -39,6 +40,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3940
|disableHtmlEscaping|Disable HTML escaping of JSON strings when using gson (needed to avoid problems with byte[] fields)| |false|
4041
|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|
4142
|discriminatorCaseSensitive|Whether the discriminator value lookup should be case-sensitive or not. This option only works for Java API client| |true|
43+
|documentationProvider|Select the OpenAPI documentation provider.|<dl><dt>**none**</dt><dd>Do not publish an OpenAPI specification.</dd><dt>**swagger1**</dt><dd>Generate an OpenAPI 2 (fka Swagger RESTful API Documentation Specification) specification using Swagger-Core 1.x.</dd><dt>**swagger2**</dt><dd>Generate an OpenAPI 3 specification using Swagger-Core 2.x.</dd></dl>|swagger1|
4244
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
4345
|enumPropertyNaming|Naming convention for enum properties: 'MACRO_CASE', 'legacy' and 'original'| |MACRO_CASE|
4446
|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|

docs/generators/jaxrs-cxf-extended.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2323
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2424
|additionalOneOfTypeAnnotations|Additional annotations for oneOf interfaces(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2525
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
26+
|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><dt>**swagger2**</dt><dd>Annotate Model and Api using the Swagger Annotations 2.x library.</dd></dl>|swagger1|
2627
|apiPackage|package for generated api classes| |org.openapitools.api|
2728
|artifactDescription|artifact description in generated pom.xml| |OpenAPI Java|
2829
|artifactId|artifactId in generated pom.xml. This also becomes part of the generated library's filename| |openapi-cxf-server|
@@ -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>**swagger1**</dt><dd>Generate an OpenAPI 2 (fka Swagger RESTful API Documentation Specification) specification using Swagger-Core 1.x.</dd><dt>**swagger2**</dt><dd>Generate an OpenAPI 3 specification using Swagger-Core 2.x.</dd></dl>|swagger1|
4345
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
4446
|enumPropertyNaming|Naming convention for enum properties: 'MACRO_CASE', 'legacy' and 'original'| |MACRO_CASE|
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|

docs/generators/jaxrs-cxf.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2323
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2424
|additionalOneOfTypeAnnotations|Additional annotations for oneOf interfaces(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2525
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
26+
|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><dt>**swagger2**</dt><dd>Annotate Model and Api using the Swagger Annotations 2.x library.</dd></dl>|swagger1|
2627
|apiPackage|package for generated api classes| |org.openapitools.api|
2728
|artifactDescription|artifact description in generated pom.xml| |OpenAPI Java|
2829
|artifactId|artifactId in generated pom.xml. This also becomes part of the generated library's filename| |openapi-cxf-server|
@@ -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>**swagger1**</dt><dd>Generate an OpenAPI 2 (fka Swagger RESTful API Documentation Specification) specification using Swagger-Core 1.x.</dd><dt>**swagger2**</dt><dd>Generate an OpenAPI 3 specification using Swagger-Core 2.x.</dd></dl>|swagger1|
4345
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
4446
|enumPropertyNaming|Naming convention for enum properties: 'MACRO_CASE', 'legacy' and 'original'| |MACRO_CASE|
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/JavaCXFClientCodegen.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openapitools.codegen.languages;
1919

2020
import io.swagger.v3.oas.models.Operation;
21+
import io.swagger.v3.oas.models.media.Schema;
2122
import lombok.Getter;
2223
import lombok.Setter;
2324
import org.openapitools.codegen.*;
@@ -115,6 +116,20 @@ public String getName() {
115116
return "jaxrs-cxf-client";
116117
}
117118

119+
@Override
120+
public List<DocumentationProvider> supportedDocumentationProvider() {
121+
return List.of(DocumentationProvider.NONE, DocumentationProvider.SWAGGER1, DocumentationProvider.SWAGGER2);
122+
}
123+
124+
@Override
125+
public List<AnnotationLibrary> supportedAnnotationLibraries() {
126+
return List.of(AnnotationLibrary.NONE, AnnotationLibrary.SWAGGER1, AnnotationLibrary.SWAGGER2);
127+
}
128+
129+
@Override
130+
public DocumentationProvider defaultDocumentationProvider() {
131+
return DocumentationProvider.SWAGGER1;
132+
}
118133

119134
@Override
120135
public CodegenType getTag() {
@@ -127,11 +142,19 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
127142
co.subresourceOperation = !co.path.isEmpty();
128143
}
129144

145+
@Override
146+
public CodegenModel fromModel(String name, Schema model) {
147+
CodegenModel m = super.fromModel(name, model);
148+
m.imports.remove("ApiModel");
149+
return m;
150+
}
151+
130152
@Override
131153
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
132154
super.postProcessModelProperty(model, property);
133155
model.imports.remove("ApiModelProperty");
134156
model.imports.remove("ApiModel");
157+
model.imports.remove("JsonTypeName");
135158

136159
if (jackson) {
137160
//Add jackson imports when model has inner enum

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openapitools.codegen.languages;
1919

2020
import lombok.Setter;
21+
import io.swagger.v3.oas.models.media.Schema;
2122
import org.openapitools.codegen.*;
2223
import org.openapitools.codegen.languages.features.CXFServerFeatures;
2324
import org.openapitools.codegen.languages.features.GzipTestFeatures;
@@ -222,12 +223,35 @@ public String getName() {
222223
return "jaxrs-cxf";
223224
}
224225

226+
@Override
227+
public List<DocumentationProvider> supportedDocumentationProvider() {
228+
return List.of(DocumentationProvider.NONE, DocumentationProvider.SWAGGER1, DocumentationProvider.SWAGGER2);
229+
}
230+
231+
@Override
232+
public List<AnnotationLibrary> supportedAnnotationLibraries() {
233+
return List.of(AnnotationLibrary.NONE, AnnotationLibrary.SWAGGER1, AnnotationLibrary.SWAGGER2);
234+
}
235+
236+
@Override
237+
public DocumentationProvider defaultDocumentationProvider() {
238+
return DocumentationProvider.SWAGGER1;
239+
}
240+
241+
@Override
242+
public CodegenModel fromModel(String name, Schema model) {
243+
CodegenModel m = super.fromModel(name, model);
244+
m.imports.remove("ApiModel");
245+
return m;
246+
}
247+
225248
@Override
226249
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
227250
super.postProcessModelProperty(model, property);
228251
model.imports.remove("ApiModelProperty");
229252
model.imports.remove("ApiModel");
230253
model.imports.remove("JsonFormat");
254+
model.imports.remove("JsonTypeName");
231255

232256
//Add imports for Jackson when model has inner enum
233257
if (isJackson()) {

modules/openapi-generator/src/main/resources/JavaJaxRS/cxf/api.mustache

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,40 @@ package {{package}};
33
{{#imports}}import {{import}};
44
{{/imports}}
55

6+
{{#useAbstractionForFiles}}
67
import java.io.InputStream;
7-
import java.io.OutputStream;
8+
{{/useAbstractionForFiles}}
89
import java.util.List;
910
import java.util.Map;
1011
import {{javaxPackage}}.ws.rs.*;
12+
{{#useGenericResponse}}
1113
import {{javaxPackage}}.ws.rs.core.Response;
14+
{{/useGenericResponse}}
15+
{{#addConsumesProducesJson}}
1216
import {{javaxPackage}}.ws.rs.core.MediaType;
17+
{{/addConsumesProducesJson}}
1318
import org.apache.cxf.jaxrs.ext.multipart.*;
1419

20+
{{#swagger1AnnotationLibrary}}
1521
import io.swagger.annotations.Api;
1622
import io.swagger.annotations.ApiOperation;
1723
import io.swagger.annotations.ApiResponses;
1824
import io.swagger.annotations.ApiResponse;
1925
import io.swagger.jaxrs.PATCH;
26+
{{/swagger1AnnotationLibrary}}
27+
{{#swagger2AnnotationLibrary}}
28+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
29+
import io.swagger.v3.oas.annotations.Operation;
30+
import io.swagger.v3.oas.annotations.Parameter;
31+
import io.swagger.v3.oas.annotations.Parameters;
32+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
33+
import io.swagger.v3.oas.annotations.info.Info;
34+
import io.swagger.v3.oas.annotations.media.ArraySchema;
35+
import io.swagger.v3.oas.annotations.media.Content;
36+
import io.swagger.v3.oas.annotations.media.Schema;
37+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
38+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
39+
{{/swagger2AnnotationLibrary}}
2040
{{#useBeanValidation}}
2141
import {{javaxPackage}}.validation.constraints.*;
2242
import {{javaxPackage}}.validation.Valid;
@@ -33,7 +53,18 @@ import {{javaxPackage}}.validation.Valid;
3353
*/
3454
{{/appName}}
3555
@Path("{{#useAnnotatedBasePath}}{{contextPath}}{{/useAnnotatedBasePath}}{{commonPath}}")
56+
{{#swagger1AnnotationLibrary}}
3657
@Api(value = "/", description = "{{description}}")
58+
{{/swagger1AnnotationLibrary}}
59+
{{#swagger2AnnotationLibrary}}
60+
@OpenAPIDefinition(
61+
info = @Info(
62+
{{#appName}}title = "{{{appName}}}",{{/appName}}
63+
{{#appDescription}}description = "{{{appDescription}}}",{{/appDescription}}
64+
version = "{{{appVersion}}}"
65+
)
66+
)
67+
{{/swagger2AnnotationLibrary}}
3768
{{#addConsumesProducesJson}}
3869
@Consumes(MediaType.APPLICATION_JSON)
3970
@Produces(MediaType.APPLICATION_JSON)
@@ -60,6 +91,7 @@ public interface {{classname}} {
6091
{{#hasProduces}}
6192
@Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} })
6293
{{/hasProduces}}
94+
{{#swagger1AnnotationLibrary}}
6395
@ApiOperation(value = "{{{summary}}}", tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-tags}} })
6496
{{#implicitHeadersParams.0}}
6597
@io.swagger.annotations.ApiImplicitParams({
@@ -70,6 +102,19 @@ public interface {{classname}} {
70102
{{/implicitHeadersParams.0}}
71103
@ApiResponses(value = { {{#responses}}
72104
@ApiResponse(code = {{{code}}}, message = "{{{message}}}"{{^vendorExtensions.x-java-is-response-void}}, response = {{#isFile}}{{#useAbstractionForFiles}}InputStream{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{baseType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{baseType}}}{{/isFile}}.class{{#containerType}}, responseContainer = "{{{.}}}"{{/containerType}}{{/vendorExtensions.x-java-is-response-void}}){{^-last}},{{/-last}}{{/responses}} })
105+
{{/swagger1AnnotationLibrary}}
106+
{{#swagger2AnnotationLibrary}}
107+
@Operation(operationId = "{{{operationIdOriginal}}}"{{#summary}}, summary = "{{{.}}}"{{/summary}}, {{#isDeprecated}}deprecated = true, {{/isDeprecated}} tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-tags}} })
108+
@ApiResponses(value = { {{#responses}}
109+
@ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{^vendorExtensions.x-java-is-response-void}}, content = @Content({{#containerType}}array = @ArraySchema({{/containerType}}schema = @Schema(implementation = {{#isFile}}{{#useAbstractionForFiles}}InputStream{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{baseType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{baseType}}}{{/isFile}}.class){{#containerType}}){{/containerType}}){{/vendorExtensions.x-java-is-response-void}}){{^-last}},{{/-last}}{{/responses}} })
110+
{{#implicitHeadersParams.0}}
111+
@Parameters({
112+
{{#implicitHeadersParams}}
113+
{{>paramDoc}}{{^-last}},{{/-last}}
114+
{{/implicitHeadersParams}}
115+
})
116+
{{/implicitHeadersParams.0}}
117+
{{/swagger2AnnotationLibrary}}
73118
public {{>returnTypes}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}});
74119
{{/operation}}
75120
}

modules/openapi-generator/src/main/resources/JavaJaxRS/cxf/apiServiceImpl.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import org.apache.cxf.jaxrs.model.wadl.DocTarget;
1515

1616
import org.apache.cxf.jaxrs.ext.multipart.*;
1717

18-
import io.swagger.annotations.Api;
1918
{{#useSpringAnnotationConfig}}
2019
import org.springframework.stereotype.Service;
2120
{{/useSpringAnnotationConfig}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@Parameter(name = "{{{baseName}}}"{{#isDeprecated}}, deprecated = true{{/isDeprecated}}, description = "{{{description}}}"{{#required}}, required = true{{/required}}{{#isPathParam}}, in = ParameterIn.PATH{{/isPathParam}}{{#isQueryParam}}, in = ParameterIn.QUERY{{/isQueryParam}}{{#isCookieParam}}, in = ParameterIn.COOKIE{{/isCookieParam}}{{#isHeaderParam}}, in = ParameterIn.HEADER{{/isHeaderParam}})

0 commit comments

Comments
 (0)