Skip to content

Commit

Permalink
[Java][Microprofile] Fix #17526: enumOuterClass missing JSON-B (de)se…
Browse files Browse the repository at this point in the history
…rializer (#18951)

* GH-17526/microprofile: Fix enumOuterClass missing JSON-B (de)serializer

* Add microprofile-rest-client-outer-enum to jdk17 samples workflow

* Regenerate samples
  • Loading branch information
DielN authored Jul 8, 2024
1 parent 0820ede commit a3912b7
Show file tree
Hide file tree
Showing 23 changed files with 1,181 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/samples-jdk17.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- samples/client/petstore/spring-http-interface-reactive/**
- samples/client/petstore/spring-http-interface/**
- samples/client/petstore/java/webclient-jakarta/**
- samples/client/petstore/java/microprofile-rest-client-outer-enum/**
# servers
- samples/openapi3/server/petstore/springboot-3/**
- samples/server/petstore/java-camel/**
Expand All @@ -23,6 +24,7 @@ on:
- samples/client/petstore/spring-http-interface-reactive/**
- samples/client/petstore/spring-http-interface/**
- samples/client/petstore/java/webclient-jakarta/**
- samples/client/petstore/java/microprofile-rest-client-outer-enum/**
# servers
- samples/openapi3/server/petstore/springboot-3/**
- samples/server/petstore/java-camel/**
Expand All @@ -43,6 +45,7 @@ jobs:
- samples/client/petstore/spring-http-interface-reactive
- samples/client/petstore/spring-http-interface
- samples/client/petstore/java/webclient-jakarta
- samples/client/petstore/java/microprofile-rest-client-outer-enum
# servers
- samples/openapi3/server/petstore/springboot-3
- samples/server/petstore/java-camel/
Expand Down
8 changes: 8 additions & 0 deletions bin/configs/java-microprofile-rest-client-outer-enum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: java
outputDir: samples/client/petstore/java/microprofile-rest-client-outer-enum
library: microprofile
inputSpec: modules/openapi-generator/src/test/resources/3_0/outerEnum.yaml
templateDir: modules/openapi-generator/src/main/resources/Java
additionalProperties:
artifactId: microprofile-rest-client
configKeyFromClassName: true
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import java.net.URI;
/**
* {{description}}{{^description}}Gets or Sets {{{name}}}{{/description}}
*/
{{#jsonb}}
@JsonbTypeSerializer({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.Serializer.class)
@JsonbTypeDeserializer({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.Deserializer.class)
{{/jsonb}}
{{>additionalEnumTypeAnnotations}}public enum {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
{{#gson}}
{{#allowableValues}}{{#enumVars}}
Expand Down Expand Up @@ -36,6 +40,22 @@ import java.net.URI;
return String.valueOf(value);
}

{{#jsonb}}
public static final class Deserializer implements JsonbDeserializer<{{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}> {
@Override
public {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
return fromValue(parser.getString());
}
}

public static final class Serializer implements JsonbSerializer<{{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}> {
@Override
public void serialize({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} obj, JsonGenerator generator, SerializationContext ctx) {
generator.write(obj.value{{#isUri}}.toASCIIString(){{/isUri}});
}
}

{{/jsonb}}
{{#jackson}}
@JsonCreator
{{/jackson}}
Expand All @@ -47,5 +67,4 @@ import java.net.URI;
}
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}}
}

}
68 changes: 68 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/outerEnum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
openapi: 3.0.0
info:
title: Petstore API
description: API for managing pets in a pet store
version: 1.0.0
paths:
/dogs:
get:
summary: List all dogs
operationId: listDogs
responses:
'200':
description: A list of dogs
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Dog'
/cats:
get:
summary: List all cats
operationId: listCats
responses:
'200':
description: A list of cats
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Cat'
components:
schemas:
Dog:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
status:
$ref: '#/components/schemas/Status'
required:
- id
- name
- status
Cat:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
status:
$ref: '#/components/schemas/Status'
required:
- id
- name
- status
Status:
type: string
enum:
- available
- pending
- sold
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
README.md
docs/Cat.md
docs/DefaultApi.md
docs/Dog.md
docs/Status.md
pom.xml
src/main/java/org/openapitools/client/api/ApiException.java
src/main/java/org/openapitools/client/api/ApiExceptionMapper.java
src/main/java/org/openapitools/client/api/DefaultApi.java
src/main/java/org/openapitools/client/model/Cat.java
src/main/java/org/openapitools/client/model/Dog.java
src/main/java/org/openapitools/client/model/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.8.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Petstore API - MicroProfile Rest Client & MicroProfile Server

API for managing pets in a pet store

## 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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


# Cat


## Properties

| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**id** | **Long** | | |
|**name** | **String** | | |
|**status** | **Status** | | |



Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# DefaultApi

All URIs are relative to *http://localhost*

| Method | HTTP request | Description |
|------------- | ------------- | -------------|
| [**listCats**](DefaultApi.md#listCats) | **GET** /cats | List all cats |
| [**listDogs**](DefaultApi.md#listDogs) | **GET** /dogs | List all dogs |



## listCats

> List&lt;Cat&gt; listCats()
List all cats

### Example

```java
// Import classes:
import org.openapitools.client.ApiClient;
import org.openapitools.client.ApiException;
import org.openapitools.client.Configuration;
import org.openapitools.client.models.*;
import org.openapitools.client.api.DefaultApi;

public class Example {
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("http://localhost");

DefaultApi apiInstance = new DefaultApi(defaultClient);
try {
List<Cat> result = apiInstance.listCats();
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling DefaultApi#listCats");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}
}
```

### Parameters

This endpoint does not need any parameter.

### Return type

[**List&lt;Cat&gt;**](Cat.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: application/json


### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
| **200** | A list of cats | - |


## listDogs

> List&lt;Dog&gt; listDogs()
List all dogs

### Example

```java
// Import classes:
import org.openapitools.client.ApiClient;
import org.openapitools.client.ApiException;
import org.openapitools.client.Configuration;
import org.openapitools.client.models.*;
import org.openapitools.client.api.DefaultApi;

public class Example {
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("http://localhost");

DefaultApi apiInstance = new DefaultApi(defaultClient);
try {
List<Dog> result = apiInstance.listDogs();
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling DefaultApi#listDogs");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}
}
```

### Parameters

This endpoint does not need any parameter.

### Return type

[**List&lt;Dog&gt;**](Dog.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: application/json


### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
| **200** | A list of dogs | - |

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


# Dog


## Properties

| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**id** | **Long** | | |
|**name** | **String** | | |
|**status** | **Status** | | |



Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


# Status

## Enum


* `AVAILABLE` (value: `"available"`)

* `PENDING` (value: `"pending"`)

* `SOLD` (value: `"sold"`)



Loading

0 comments on commit a3912b7

Please sign in to comment.