Skip to content

Commit 48343d5

Browse files
committed
[java][microprofile] Support useOneOfInterfaces with JSON-B serialization
Add JSON-B coverage for the useOneOfInterfaces feature in the microprofile library. With serializationLibrary=jsonb and microprofileRestClientVersion=3.0, the generated oneOf interface carries JSON-B polymorphism (@JsonbTypeInfo / @JsonbSubtype) instead of the Jackson annotations, driven by the existing jsonbPolymorphism path in typeInfoAnnotation.mustache. No generator source change is required - this locks the behaviour in with a sample and a test. - new JavaClientCodegenTest#oneOfInterfaceMicroprofileJsonb asserting the interface emits @JsonbTypeInfo(key="petType") + @JsonbSubtype CAT/DOG and no Jackson annotations, and that children implement the interface. - new bin/configs/java-microprofile-oneof-interface-jsonb.yaml + generated microprofile-oneof-interface-jsonb sample (JSON-B, restClientVersion 3.0). - register the sample in the samples-java-client-jdk11 workflow so CI compiles it.
1 parent b87a351 commit 48343d5

28 files changed

Lines changed: 1377 additions & 0 deletions

File tree

.github/workflows/samples-java-client-jdk11.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ jobs:
8787
- samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml
8888
- samples/client/petstore/java/microprofile-rest-client-3.0-mutiny
8989
- samples/client/petstore/java/microprofile-rest-client-with-useSingleRequestParameter
90+
- samples/client/petstore/java/microprofile-oneof-interface-jsonb
9091
- samples/client/petstore/java/apache-httpclient
9192
- samples/client/petstore/java/feign
9293
- samples/client/petstore/java/feign-hc5
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
generatorName: java
2+
outputDir: samples/client/petstore/java/microprofile-oneof-interface-jsonb
3+
library: microprofile
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/java/oneof_interface_petstore.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/Java
6+
additionalProperties:
7+
serializationLibrary: jsonb
8+
artifactId: microprofile-oneof-interface-jsonb
9+
useOneOfInterfaces: "true"
10+
microprofileRestClientVersion: "3.0"
11+
hideGenerationTimestamp: true

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4219,6 +4219,38 @@ public void oneOfInterfaceMicroprofileJackson() {
42194219
assertFileNotContains(model.resolve("CatRequest.java"), "public final class");
42204220
}
42214221

4222+
@Test
4223+
public void oneOfInterfaceMicroprofileJsonb() {
4224+
final Path output = newTempFolder();
4225+
final CodegenConfigurator configurator = new CodegenConfigurator()
4226+
.setGeneratorName(JAVA_GENERATOR)
4227+
.setLibrary(MICROPROFILE)
4228+
.setAdditionalProperties(Map.of(
4229+
USE_ONE_OF_INTERFACES, "true",
4230+
CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_JSONB,
4231+
JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0"
4232+
))
4233+
.setInputSpec("src/test/resources/3_0/java/oneof_interface_petstore.yaml")
4234+
.setOutputDir(output.toString().replace("\\", "/"));
4235+
4236+
new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
4237+
4238+
final Path model = output.resolve("src/main/java/org/openapitools/client/model");
4239+
// oneOf schema rendered as an interface (not a pojo); the discriminator getter resolves to the enum
4240+
assertFileContains(model.resolve("PetRequest.java"), "public interface PetRequest {");
4241+
assertFileContains(model.resolve("PetRequest.java"), "public PetType getPetType();");
4242+
// JSON-B polymorphism is emitted on the interface (not Jackson), with the correct discriminator
4243+
// key and CAT/DOG alias -> subtype mapping
4244+
assertFileContains(model.resolve("PetRequest.java"), "@JsonbTypeInfo(key = \"petType\"");
4245+
assertFileContains(model.resolve("PetRequest.java"), "@JsonbSubtype(alias = \"CAT\", type = CatRequest.class)");
4246+
assertFileContains(model.resolve("PetRequest.java"), "@JsonbSubtype(alias = \"DOG\", type = DogRequest.class)");
4247+
assertFileNotContains(model.resolve("PetRequest.java"), "@JsonTypeInfo");
4248+
assertFileNotContains(model.resolve("PetRequest.java"), "@JsonSubTypes");
4249+
// children implement the interface
4250+
assertFileContains(model.resolve("CatRequest.java"), "implements PetRequest");
4251+
assertFileContains(model.resolve("DogRequest.java"), "implements PetRequest");
4252+
}
4253+
42224254
@DataProvider(name = "sealedInterfaceScenarios")
42234255
public static Object[][] sealedInterfaceScenarios() {
42244256
return new Object[][]{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.openapi-generator-ignore
2+
README.md
3+
docs/CatRequest.md
4+
docs/DefaultApi.md
5+
docs/DogRequest.md
6+
docs/PetBase.md
7+
docs/PetRequest.md
8+
docs/PetType.md
9+
pom.xml
10+
src/main/java/org/openapitools/client/api/ApiException.java
11+
src/main/java/org/openapitools/client/api/ApiExceptionMapper.java
12+
src/main/java/org/openapitools/client/api/DefaultApi.java
13+
src/main/java/org/openapitools/client/model/CatRequest.java
14+
src/main/java/org/openapitools/client/model/DogRequest.java
15+
src/main/java/org/openapitools/client/model/PetBase.java
16+
src/main/java/org/openapitools/client/model/PetRequest.java
17+
src/main/java/org/openapitools/client/model/PetType.java
18+
src/test/java/org/openapitools/client/api/DefaultApiTest.java
19+
src/test/java/org/openapitools/client/model/CatRequestTest.java
20+
src/test/java/org/openapitools/client/model/DogRequestTest.java
21+
src/test/java/org/openapitools/client/model/PetBaseTest.java
22+
src/test/java/org/openapitools/client/model/PetRequestTest.java
23+
src/test/java/org/openapitools/client/model/PetTypeTest.java
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.24.0-SNAPSHOT
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# oneOf interface petstore - MicroProfile Rest Client & MicroProfile Server
2+
3+
A minimal, self-consistent oneOf-interface spec for the Java (microprofile) client generator. The oneOf container and the shared base both declare the discriminator property so the interface getter resolves to the enum type. Children inherit the base via allOf and only implement the generated interface.
4+
5+
6+
## Overview
7+
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
8+
[MicroProfile Rest Client](https://github.com/eclipse/microprofile-rest-client) is a type-safe way of calling
9+
REST services. The generated client contains an interface which acts as the client, you can inject it into dependent classes.
10+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
# CatRequest
4+
5+
6+
## Properties
7+
8+
| Name | Type | Description | Notes |
9+
|------------ | ------------- | ------------- | -------------|
10+
|**petType** | **PetType** | | |
11+
|**name** | **String** | | |
12+
|**indoor** | **Boolean** | | |
13+
14+
15+
## Implemented Interfaces
16+
17+
* PetRequest
18+
19+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# DefaultApi
2+
3+
All URIs are relative to *http://localhost*
4+
5+
| Method | HTTP request | Description |
6+
|------------- | ------------- | -------------|
7+
| [**createPet**](DefaultApi.md#createPet) | **POST** /pets | |
8+
9+
10+
11+
## createPet
12+
13+
> PetRequest createPet(petRequest)
14+
15+
16+
17+
### Example
18+
19+
```java
20+
// Import classes:
21+
import org.openapitools.client.ApiClient;
22+
import org.openapitools.client.ApiException;
23+
import org.openapitools.client.Configuration;
24+
import org.openapitools.client.models.*;
25+
import org.openapitools.client.api.DefaultApi;
26+
27+
public class Example {
28+
public static void main(String[] args) {
29+
ApiClient defaultClient = Configuration.getDefaultApiClient();
30+
defaultClient.setBasePath("http://localhost");
31+
32+
DefaultApi apiInstance = new DefaultApi(defaultClient);
33+
PetRequest petRequest = new PetRequest(); // PetRequest |
34+
try {
35+
PetRequest result = apiInstance.createPet(petRequest);
36+
System.out.println(result);
37+
} catch (ApiException e) {
38+
System.err.println("Exception when calling DefaultApi#createPet");
39+
System.err.println("Status code: " + e.getCode());
40+
System.err.println("Reason: " + e.getResponseBody());
41+
System.err.println("Response headers: " + e.getResponseHeaders());
42+
e.printStackTrace();
43+
}
44+
}
45+
}
46+
```
47+
48+
### Parameters
49+
50+
51+
| Name | Type | Description | Notes |
52+
|------------- | ------------- | ------------- | -------------|
53+
| **petRequest** | [**PetRequest**](PetRequest.md)| | |
54+
55+
### Return type
56+
57+
[**PetRequest**](PetRequest.md)
58+
59+
### Authorization
60+
61+
No authorization required
62+
63+
### HTTP request headers
64+
65+
- **Content-Type**: application/json
66+
- **Accept**: application/json
67+
68+
69+
### HTTP response details
70+
| Status code | Description | Response headers |
71+
|-------------|-------------|------------------|
72+
| **200** | created | - |
73+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
# DogRequest
4+
5+
6+
## Properties
7+
8+
| Name | Type | Description | Notes |
9+
|------------ | ------------- | ------------- | -------------|
10+
|**petType** | **PetType** | | |
11+
|**name** | **String** | | |
12+
|**trained** | **Boolean** | | |
13+
14+
15+
## Implemented Interfaces
16+
17+
* PetRequest
18+
19+

0 commit comments

Comments
 (0)