Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Microprofile] Add option to use tags a client keys and server generator #16673

Merged
merged 41 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
3abefee
[Java/Microprofile] Add support for Jackson serialization & async int…
pravussum Feb 2, 2022
c5de775
Merge remote-tracking branch 'openapi-generator/master' into java-mic…
pravussum Feb 17, 2022
7983f0d
Regenerate samples & docs
pravussum Feb 17, 2022
e41a2ff
Add server generator
Jun 14, 2022
09e441f
Update client to set configKey by classname
Jun 14, 2022
a7b4f70
Remove debug remains and comments
kiwi-oss Jul 31, 2023
9ef4f6a
Merge remote-tracking branch 'upstream/master' into microprofile-serv…
kiwi-oss Aug 1, 2023
7e8fd05
Adapt method override to upstream changes
kiwi-oss Aug 2, 2023
b5bcbde
Regenerate samples
kiwi-oss Aug 2, 2023
5429144
Merge remote-tracking branch 'upstream/master' into microprofile-serv…
kiwi-oss Sep 8, 2023
095d22f
Revert "Regenerate samples"
kiwi-oss Sep 12, 2023
60475f0
Move additional 2xx response to dedicated OpenAPI document
kiwi-oss Sep 12, 2023
58655da
Make filename consistent with other files in the folder
kiwi-oss Sep 12, 2023
f9138a2
Regenerate Microprofile client and server samples
kiwi-oss Sep 12, 2023
6219586
Merge remote-tracking branch 'upstream/master' into microprofile-serv…
kiwi-oss Sep 12, 2023
e66f96b
Merge remote-tracking branch 'upstream/master' into microprofile-serv…
kiwi-oss Sep 21, 2023
aa89d82
Merge remote-tracking branch 'upstream/master' into microprofile-serv…
kiwi-oss Sep 26, 2023
160831e
Generate samples
kiwi-oss Sep 26, 2023
b72f09b
Update documentation
kiwi-oss Sep 26, 2023
9db4a4e
Merge remote-tracking branch 'upstream/master' into microprofile-serv…
kiwi-oss Oct 9, 2023
ce52f7c
Generate samples
kiwi-oss Oct 9, 2023
ac0d143
Remove left-over `*.orig` files from Git merges
kiwi-oss Oct 18, 2023
f2284bd
Merge remote-tracking branch 'upstream/master' into microprofile-serv…
kiwi-oss Oct 18, 2023
c508b3c
Merge remote-tracking branch 'upstream/master' into microprofile-serv…
kiwi-oss Nov 20, 2023
e425da1
Regenerate samples
kiwi-oss Nov 20, 2023
a6f4d57
Merge remote-tracking branch 'origin/master' into microprofile-server…
oscarobr Mar 5, 2024
a58b998
Merge branch 'OpenAPITools:master' into microprofile-server-generator
oscarobr Apr 24, 2024
f16fba2
Merge branch 'OpenAPITools:master' into microprofile-server-generator
oscarobr Apr 24, 2024
e564440
Regenerated samples
oscarobr Apr 24, 2024
707545b
changed generator name to "java-microprofile"
oscarobr Apr 25, 2024
8e25428
added the new folder to .github/workflows/samples-java-server-jdk8.ya…
oscarobr Apr 25, 2024
db381f1
Renamed JavaMicroprofileServerCodegen.java
oscarobr Apr 25, 2024
74f6b04
regenerated samples
oscarobr Apr 25, 2024
2b6f9b3
only enable configKeyFromClassName if configKey is not set
oscarobr Apr 29, 2024
f123bab
Merge branch 'OpenAPITools:master' into microprofile-server-generator
oscarobr Apr 29, 2024
a066de5
Updated documentation
oscarobr Apr 30, 2024
e82cac2
Change samples to use junit 4
oscarobr Apr 30, 2024
1fa87ec
Fix junit 4 test classes
oscarobr May 6, 2024
f9ce785
Merge branch 'OpenAPITools:master' into microprofile-server-generator
oscarobr May 6, 2024
31f8340
run ensure up-to-date script
oscarobr May 6, 2024
f8c20b6
fix kotlin test errors
oscarobr May 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/samples-java-server-jdk8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ on:
- 'samples/server/petstore/java-pkmst/**'
# test in circleci instead
- 'samples/server/petstore/java-undertow/**'
- 'samples/server/petstore/java-microprofile/**'
pull_request:
paths:
#- 'samples/server/petstore/java-camel/**'
- 'samples/server/petstore/java-vertx-web/**'
- 'samples/server/petstore/java-inflector/**'
- 'samples/server/petstore/java-pkmst/**'
#- 'samples/server/petstore/java-undertow/**'
- 'samples/server/petstore/java-microprofile/**'
jobs:
build:
name: Build Java Server
Expand All @@ -31,6 +33,7 @@ jobs:
- samples/server/petstore/java-inflector/
- samples/server/petstore/java-pkmst/
#- samples/server/petstore/java-undertow/
- samples/server/petstore/java-microprofile/
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
Expand Down
4 changes: 2 additions & 2 deletions bin/configs/java-microprofile-rest-client.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
generatorName: java
outputDir: samples/client/petstore/java/microprofile-rest-client
library: microprofile
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/Java
additionalProperties:
artifactId: microprofile-rest-client
configKey: petstore
configKeyFromClassName: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: java-microprofile
outputDir: samples/server/petstore/java-microprofile
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-multiple-2xx-responses.yaml
templateDir: modules/openapi-generator/src/main/resources/Java
additionalProperties:
hideGenerationTimestamp: "true"
microprofileMutiny: "true"
serializationLibrary: "jackson"
1 change: 1 addition & 0 deletions docs/generators/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|camelCaseDollarSign|Fix camelCase when starting with $ sign. when true : $Value when false : $value| |false|
|caseInsensitiveResponseHeaders|Make API response's headers case-insensitive. Available on okhttp-gson, jersey2 libraries| |false|
|configKey|Config key in @RegisterRestClient. Default to none. Only `microprofile` supports this option.| |null|
|configKeyFromClassName|If true, set tag as key in @RegisterRestClient. Default to false. Only `microprofile` supports this option.| |null|
|containerDefaultToNull|Set containers (array, set, map) default to null| |false|
|dateLibrary|Option. Date library to use|<dl><dt>**joda**</dt><dd>Joda (for legacy app only)</dd><dt>**legacy**</dt><dd>Legacy java.util.Date</dd><dt>**java8-localdatetime**</dt><dd>Java 8 using LocalDateTime (for legacy app only)</dd><dt>**java8**</dt><dd>Java 8 native JSR310 (preferred for jdk 1.8+)</dd></dl>|java8|
|developerEmail|developer email in generated pom.xml| |[email protected]|
Expand Down
361 changes: 361 additions & 0 deletions docs/generators/microprofile.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
public static final String USE_PLAY_WS = "usePlayWS";
public static final String ASYNC_NATIVE = "asyncNative";
public static final String CONFIG_KEY = "configKey";
public static final String CONFIG_KEY_FROM_CLASS_NAME = "configKeyFromClassName";
public static final String PARCELABLE_MODEL = "parcelableModel";
public static final String USE_RUNTIME_EXCEPTION = "useRuntimeException";
public static final String USE_REFLECTION_EQUALS_HASHCODE = "useReflectionEqualsHashCode";
Expand Down Expand Up @@ -114,6 +115,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
protected String microprofileFramework = MICROPROFILE_DEFAULT;
protected boolean microprofileMutiny = false;
protected String configKey = null;
protected boolean configKeyFromClassName = false;

protected boolean asyncNative = false;
protected boolean parcelableModel = false;
Expand Down Expand Up @@ -224,6 +226,7 @@ public JavaClientCodegen() {
cliOptions.add(CliOption.newString(GRADLE_PROPERTIES, "Append additional Gradle properties to the gradle.properties file"));
cliOptions.add(CliOption.newString(ERROR_OBJECT_TYPE, "Error Object type. (This option is for okhttp-gson-next-gen only)"));
cliOptions.add(CliOption.newString(CONFIG_KEY, "Config key in @RegisterRestClient. Default to none. Only `microprofile` supports this option."));
cliOptions.add(CliOption.newString(CONFIG_KEY_FROM_CLASS_NAME, "If true, set tag as key in @RegisterRestClient. Default to false. Only `microprofile` supports this option."));
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC + " Only jersey2, jersey3, native, okhttp-gson support this option."));
cliOptions.add(CliOption.newString(MICROPROFILE_REST_CLIENT_VERSION, "Version of MicroProfile Rest Client API."));
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter. ONLY jersey2, jersey3, okhttp-gson, microprofile support this option."));
Expand Down Expand Up @@ -386,6 +389,11 @@ public void processOpts() {
this.setConfigKey(additionalProperties.get(CONFIG_KEY).toString());
}

if(additionalProperties.containsKey(CONFIG_KEY_FROM_CLASS_NAME)) {
this.setConfigKey(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if configKeyFromClassName is set to false? Then the expectation is to still use the value of configKey, but it's set to null here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated this to be conditional on CONFIG_KEY being null. So that the CONFIG_KEY option takes precedence

this.setConfigKeyFromClassName(Boolean.parseBoolean(additionalProperties.get(CONFIG_KEY_FROM_CLASS_NAME).toString()));
}

if (additionalProperties.containsKey(ASYNC_NATIVE)) {
this.setAsyncNative(convertPropertyToBooleanAndWriteBack(ASYNC_NATIVE));
}
Expand Down Expand Up @@ -902,6 +910,14 @@ public int compare(CodegenParameter one, CodegenParameter another) {

if (MICROPROFILE.equals(getLibrary())) {
objs = AbstractJavaJAXRSServerCodegen.jaxrsPostProcessOperations(objs);
if (configKeyFromClassName) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
String configKeyFromClassName = operations.get("classname")
.toString()
.replaceFirst("Api", "")
.toLowerCase(Locale.ROOT).concat("-api");
operations.put("configKey", configKeyFromClassName);
}
}

if (WEBCLIENT.equals(getLibrary())) {
Expand Down Expand Up @@ -1307,6 +1323,10 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
return super.postProcessSupportingFileData(objs);
}

private void setConfigKeyFromClassName(boolean configKeyFromClassName) {
this.configKeyFromClassName = configKeyFromClassName;
}

@Override
public String toApiVarName(String name) {
String apiVarName = super.toApiVarName(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.openapitools.codegen.languages;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenResponse;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MicroprofileServerCodegen extends JavaClientCodegen {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally I would suggest NOT to extend a java server with JavaClientCodegen as most options available in JavaClientCodegen won't make sense in the server generator if I understand it correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The java generator, using the microprofile library, generates an interface that could be used for either client or server.

This is what is being extended. I understand that it seems odd to have a server code generator inherit from a client generator. But we would have to re write the functionality of the java generator.

Do you have a suggestion of how to proceed with this?
Or perhaps it could be listed as a MicroprofileClientCodegen because it could be used for either client of server

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right seems odd but to me but it's not a showstopper.

if this way is a good starting point to add the server generator for microprofile, i'm totally ok with that.

public static final String PROJECT_NAME = "projectName";
public static final String MICROPROFILE_SERVER = "microprofileServer";
protected boolean microprofileServer = true;

public MicroprofileServerCodegen() {
super();

outputFolder = "generated-code" + File.separator + "microprofile";
invokerPackage = "org.openapitools.server";
artifactId = "openapi-microprofile-server";
apiPackage = "org.openapitools.server.api";
modelPackage = "org.openapitools.server.model";
setLibrary("microprofile");
}

public CodegenType getTag() {
return CodegenType.SERVER;
}

public String getName() {
return "java-microprofile";
}

public String getHelp() {
return "Generates a microprofile server.";
}
@Override
public void processOpts() {
super.processOpts();
additionalProperties.put(MICROPROFILE_SERVER, microprofileServer);
}

@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
super.postProcessOperationsWithModels(objs, allModels);
OperationMap operations = objs.getOperations();
if (operations != null) {
List<CodegenOperation> ops = operations.getOperation();
for (CodegenOperation operation : ops) {
List<CodegenResponse> responses = operation.responses;
if (multiple2xxResponsesSpecified(responses)) {
operation.vendorExtensions.put("x-multiple-2xx-response-operation", true);
}
}
}
return objs;
}

private boolean multiple2xxResponsesSpecified(List<CodegenResponse> responses) {
int responseIs2xxCount = 0;
for (CodegenResponse response : responses) {
if (response.is2xx) {
responseIs2xxCount++;
}
}
return responseIs2xxCount > 1;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# {{appName}} - MicroProfile Rest Client
# {{appName}} - MicroProfile Rest Client & MicroProfile Server

{{#appDescriptionWithNewLines}}
{{{.}}}

{{/appDescriptionWithNewLines}}
{{^microprofileServer}}
## Overview
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
[MicroProfile Rest Client](https://github.com/eclipse/microprofile-rest-client) is a type-safe way of calling
REST services. The generated client contains an interface which acts as the client, you can inject it into dependent classes.
{{/microprofileServer}}

{{#microprofileServer}}
## Overview
This server was generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
The generated server contains an interface which acts as the server, you can inject it into the controller class.
This module is intended to provide additional server features, like accessing an operations response object, when multiple responses where specified.
{{/microprofileServer}}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
*/
{{/appName}}

@RegisterRestClient{{#configKey}}(configKey="{{configKey}}"){{/configKey}}
{{^microprofileServer}}
@RegisterRestClient{{#configKey}}(configKey="{{configKey}}"){{/configKey}}{{#configKeyFromClassName}}{{#operations}}(configKey="{{configKey}}"){{/operations}}{{/configKeyFromClassName}}
{{/microprofileServer}}
@RegisterProvider(ApiExceptionMapper.class)
@Path("{{#useAnnotatedBasePath}}{{contextPath}}{{/useAnnotatedBasePath}}{{commonPath}}")
public interface {{classname}} {
Expand Down Expand Up @@ -70,7 +72,7 @@ public interface {{classname}} {
@Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} })
{{/hasProduces}}
{{^useSingleRequestParameter}}
{{^vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<{{{returnType}}}>{{/microprofileMutiny}}{{^microprofileMutiny}}{{{returnType}}}{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}}{{#vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<Void>{{/microprofileMutiny}}{{^microprofileMutiny}}void{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException, ProcessingException;
{{^vendorExtensions.x-java-is-response-void}}{{#microprofileServer}}{{> server_operation}}{{/microprofileServer}}{{^microprofileServer}}{{> client_operation}}{{/microprofileServer}}{{/vendorExtensions.x-java-is-response-void}}{{#vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<Void>{{/microprofileMutiny}}{{^microprofileMutiny}}void{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException, ProcessingException;
{{/useSingleRequestParameter}}
{{#useSingleRequestParameter}}
{{^vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<{{{returnType}}}>{{/microprofileMutiny}}{{^microprofileMutiny}}{{{returnType}}}{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}}{{#vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<Void>{{/microprofileMutiny}}{{^microprofileMutiny}}void{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}} {{nickname}}({{#hasNonBodyParams}}@BeanParam {{operationIdCamelCase}}Request request{{/hasNonBodyParams}}{{#bodyParams}}{{#hasNonBodyParams}}, {{/hasNonBodyParams}}{{>bodyParams}}{{/bodyParams}}) throws ApiException, ProcessingException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#microprofileMutiny}}Uni<{{{returnType}}}>{{/microprofileMutiny}}{{^microprofileMutiny}}{{{returnType}}}{{/microprofileMutiny}}
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,4 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}}{{#vendorExtensi
}
return o.toString().replace("\n", "\n ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#vendorExtensions.x-multiple-2xx-response-operation}}{{#microprofileMutiny}}Uni<Response>{{/microprofileMutiny}}{{^microprofileMutiny}}Response{{/microprofileMutiny}}{{/vendorExtensions.x-multiple-2xx-response-operation}}{{^vendorExtensions.x-multiple-2xx-response-operation}}{{#microprofileMutiny}}Uni<{{{returnType}}}>{{/microprofileMutiny}}{{^microprofileMutiny}}{{{returnType}}}{{/microprofileMutiny}}{{/vendorExtensions.x-multiple-2xx-response-operation}}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ org.openapitools.codegen.languages.JuliaServerCodegen
org.openapitools.codegen.languages.K6ClientCodegen
org.openapitools.codegen.languages.LuaClientCodegen
org.openapitools.codegen.languages.MarkdownDocumentationCodegen
org.openapitools.codegen.languages.MicroprofileServerCodegen
wing328 marked this conversation as resolved.
Show resolved Hide resolved
org.openapitools.codegen.languages.MysqlSchemaCodegen
org.openapitools.codegen.languages.N4jsClientCodegen
org.openapitools.codegen.languages.NimClientCodegen
Expand Down
Loading