Skip to content

Commit

Permalink
[Microprofile] Add option to use tags a client keys and server genera…
Browse files Browse the repository at this point in the history
…tor (#16673)

* [Java/Microprofile] Add support for Jackson serialization & async interfaces using Mutiny in Java Microprofile library

* Regenerate samples & docs

* Add server generator

* Update client to set configKey by classname

* Remove debug remains and comments

* Adapt method override to upstream changes

* Regenerate samples

* Revert "Regenerate samples"

This reverts commit b5bcbde.

* Move additional 2xx response to dedicated OpenAPI document

Some other generators than those for Micronaut don't seem to be able to
handle this case, so we don't add it to the general pet store document.

* Make filename consistent with other files in the folder

* Regenerate Microprofile client and server samples

* Generate samples

* Update documentation

* Generate samples

* Remove left-over `*.orig` files from Git merges

* Regenerate samples

* Regenerated samples

* changed generator name to "java-microprofile"

* added the new folder to .github/workflows/samples-java-server-jdk8.yaml so that CI will test it moving forward

* Renamed JavaMicroprofileServerCodegen.java

* regenerated samples

* only enable configKeyFromClassName if configKey is not set

* Updated documentation

* Change samples to use junit 4

* Fix junit 4 test classes

* run ensure up-to-date script

* fix kotlin test errors

---------

Co-authored-by: pravussum <[email protected]>
Co-authored-by: frank <[email protected]>
Co-authored-by: Oscar <[email protected]>
Co-authored-by: oscarobr <[email protected]>
  • Loading branch information
5 people authored May 7, 2024
1 parent dc63444 commit dec8a43
Show file tree
Hide file tree
Showing 120 changed files with 7,336 additions and 66 deletions.
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.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ The following generators are available:
* [java-helidon-server (beta)](generators/java-helidon-server.md)
* [java-inflector](generators/java-inflector.md)
* [java-micronaut-server (beta)](generators/java-micronaut-server.md)
* [java-microprofile](generators/java-microprofile.md)
* [java-msf4j](generators/java-msf4j.md)
* [java-pkmst](generators/java-pkmst.md)
* [java-play-framework](generators/java-play-framework.md)
Expand Down
364 changes: 364 additions & 0 deletions docs/generators/java-microprofile.md

Large diffs are not rendered by default.

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
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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 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 @@ -385,6 +388,8 @@ public void processOpts() {

if (additionalProperties.containsKey(CONFIG_KEY)) {
this.setConfigKey(additionalProperties.get(CONFIG_KEY).toString());
} else if (additionalProperties.containsKey(CONFIG_KEY_FROM_CLASS_NAME)) {
this.setConfigKeyFromClassName(Boolean.parseBoolean(additionalProperties.get(CONFIG_KEY_FROM_CLASS_NAME).toString()));
}

if (additionalProperties.containsKey(ASYNC_NATIVE)) {
Expand Down Expand Up @@ -904,6 +909,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 @@ -1318,6 +1331,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,71 @@
package org.openapitools.codegen.languages;

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

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;

public class JavaMicroprofileServerCodegen extends JavaClientCodegen {
public static final String PROJECT_NAME = "projectName";
public static final String MICROPROFILE_SERVER = "microprofileServer";
protected boolean microprofileServer = true;

public JavaMicroprofileServerCodegen() {
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.JavaMicroprofileServerCodegen
org.openapitools.codegen.languages.MysqlSchemaCodegen
org.openapitools.codegen.languages.N4jsClientCodegen
org.openapitools.codegen.languages.NimClientCodegen
Expand Down
Loading

0 comments on commit dec8a43

Please sign in to comment.