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

[BUG] [Java] Invalid code generation for oneof types. #18544

Merged
merged 6 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,21 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{#composedSchemas}}
{{#oneOf}}
{{^isArray}}
{{^isMap}}
{{^vendorExtensions.x-duplicated-data-type}}
final TypeAdapter<{{{dataType}}}> adapter{{{dataType}}} = gson.getDelegateAdapter(this, TypeToken.get({{{dataType}}}.class));
{{/vendorExtensions.x-duplicated-data-type}}
{{/isMap}}
{{/isArray}}
{{#isArray}}

final Type typeInstance = new TypeToken<{{{dataType}}}>(){}.getType();
final TypeAdapter<{{{dataType}}}> adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = (TypeAdapter<{{{dataType}}}>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
{{/isArray}}
{{#isMap}}
final Type typeInstance = new TypeToken<{{{dataType}}}>(){}.getType();
final TypeAdapter<{{{dataType}}}> adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = (TypeAdapter<{{{dataType}}}>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance));
{{/isMap}}
{{/oneOf}}
{{/composedSchemas}}

Expand All @@ -72,11 +78,18 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{#oneOf}}
{{^vendorExtensions.x-duplicated-data-type}}
// check if the actual instance is of the type `{{{dataType}}}`
if (value.getActualInstance() instanceof {{#isArray}}List<?>{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
if (value.getActualInstance() instanceof {{#isArray}}List<?>{{/isArray}}{{#isMap}}Map<?, ?>{{/isMap}}{{^isMap}}{{^isArray}}{{{dataType}}}{{/isArray}}{{/isMap}}) {
{{#isPrimitiveType}}
{{^isMap}}
JsonPrimitive primitive = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonPrimitive();
elementAdapter.write(out, primitive);
return;
{{/isMap}}
{{#isMap}}
JsonObject object = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonObject();
elementAdapter.write(out, object);
return;
{{/isMap}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isArray}}
Expand All @@ -88,13 +101,15 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}
{{/isArray}}
{{/isPrimitiveType}}
{{^isMap}}
{{^isArray}}
{{^isPrimitiveType}}
JsonElement element = adapter{{{dataType}}}.toJsonTree(({{{dataType}}})value.getActualInstance());
elementAdapter.write(out, element);
return;
{{/isPrimitiveType}}
{{/isArray}}
{{/isMap}}
}
{{/vendorExtensions.x-duplicated-data-type}}
{{/oneOf}}
Expand Down Expand Up @@ -143,6 +158,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
try {
// validate the JSON object to see if any exception is thrown
{{^isArray}}
{{^isMap}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
Expand All @@ -163,6 +179,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isPrimitiveType}}
{{/isNumber}}
{{/isMap}}
{{/isArray}}
{{#isArray}}
if (!jsonElement.isJsonArray()) {
Expand Down Expand Up @@ -194,6 +211,38 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isArray}}
{{#isMap}}
if (!jsonElement.isJsonObject()) {
throw new IllegalArgumentException(String.format("Expected json element to be a object type in the JSON string but got `%s`", jsonElement.toString()));
}

{{^isFreeFormObject}}
Map<String, JsonElement> map = jsonElement.getAsJsonObject().asMap();
// validate map items
for(JsonElement element : map.values()) {
{{#items}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isNumber}}
{{^isNumber}}
{{#isPrimitiveType}}
if (!element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(element);
{{/isPrimitiveType}}
{{/isNumber}}
{{/items}}
}
{{/isFreeFormObject}}
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isMap}}
match++;
log.log(Level.FINER, "Input data matches schema '{{{dataType}}}'");
} catch (Exception e) {
Expand Down Expand Up @@ -280,7 +329,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{#composedSchemas}}
{{#oneOf}}
{{^vendorExtensions.x-duplicated-data-type}}
if (instance instanceof {{#isArray}}List<?>{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
if (instance instanceof {{#isArray}}List<?>{{/isArray}}{{#isMap}}Map<?, ?>{{/isMap}}{{^isMap}}{{^isArray}}{{{dataType}}}{{/isArray}}{{/isMap}}) {
{{#isArray}}
List<?> list = (List<?>) instance;
if (list.get(0) instanceof {{{items.dataType}}}) {
Expand Down Expand Up @@ -322,7 +371,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
* @return The actual instance of `{{{dataType}}}`
* @throws ClassCastException if the instance is not `{{{dataType}}}`
*/
public {{{dataType}}} get{{#isArray}}{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}() throws ClassCastException {
public {{{dataType}}} get{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}() throws ClassCastException {
return ({{{dataType}}})super.getActualInstance();
}
{{/vendorExtensions.x-duplicated-data-type}}
Expand All @@ -345,6 +394,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
// validate the json string with {{{dataType}}}
try {
{{^hasVars}}
{{^isMap}}
{{^isArray}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
Expand All @@ -364,6 +414,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/isPrimitiveType}}
{{/isNumber}}
{{/isArray}}
{{/isMap}}
{{#isArray}}
if (!jsonElement.isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()));
Expand Down Expand Up @@ -392,6 +443,37 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/items}}
}
{{/isArray}}
{{#isMap}}
if (!jsonElement.isJsonObject()) {
throw new IllegalArgumentException(String.format("Expected json element to be a object type in the JSON string but got `%s`", jsonElement.toString()));
}

{{^isFreeFormObject}}
Map<String, JsonElement> map = jsonElement.getAsJsonObject().asMap();
// validate map items
for(JsonElement element : map.values()) {
{{#items}}
{{#isNumber}}
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isNumber}}
{{^isNumber}}
{{#isPrimitiveType}}
if (!element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
throw new IllegalArgumentException(String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
}
{{/isPrimitiveType}}
{{/isNumber}}
{{^isNumber}}
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(element);
{{/isPrimitiveType}}
{{/isNumber}}
{{/items}}
}
{{/isFreeFormObject}}
{{/isMap}}
{{/hasVars}}
{{#hasVars}}
{{{.}}}.validateJsonElement(jsonElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,18 @@ paths:
schema:
$ref: '#/components/schemas/FileSchemaTestClass'
required: true
/fake/get-free-form-object:
get:
tags:
- fake
description: Get a free form object or Json string
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/FreeFormObjectTestClass'
/fake/test-query-parameters:
put:
tags:
Expand Down Expand Up @@ -2035,6 +2047,16 @@ components:
type: array
items:
$ref: '#/components/schemas/File'
FreeFormObjectTestClass:
type: object
properties:
name:
type: string
properties:
oneOf:
- type: string
- type: object
additionalProperties: true
File:
type: object
description: Must be named `File` for test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ docs/FileSchemaTestClass.md
docs/Foo.md
docs/FooGetDefaultResponse.md
docs/FormatTest.md
docs/FreeFormObjectTestClass.md
docs/FreeFormObjectTestClassProperties.md
docs/Fruit.md
docs/FruitReq.md
docs/GmFruit.md
Expand Down Expand Up @@ -187,6 +189,8 @@ src/main/java/org/openapitools/client/model/FileSchemaTestClass.java
src/main/java/org/openapitools/client/model/Foo.java
src/main/java/org/openapitools/client/model/FooGetDefaultResponse.java
src/main/java/org/openapitools/client/model/FormatTest.java
src/main/java/org/openapitools/client/model/FreeFormObjectTestClass.java
src/main/java/org/openapitools/client/model/FreeFormObjectTestClassProperties.java
src/main/java/org/openapitools/client/model/Fruit.java
src/main/java/org/openapitools/client/model/FruitReq.java
src/main/java/org/openapitools/client/model/GmFruit.java
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/java/okhttp-gson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Class | Method | HTTP request | Description
*AnotherFakeApi* | [**getParameterStringNumber**](docs/AnotherFakeApi.md#getParameterStringNumber) | **GET** /fake/parameter-string-number | parameter string number
*AnotherFakeApi* | [**nullRequestBody**](docs/AnotherFakeApi.md#nullRequestBody) | **GET** /fake/null-request-body | null request body
*DefaultApi* | [**fooGet**](docs/DefaultApi.md#fooGet) | **GET** /foo |
*FakeApi* | [**fakeGetFreeFormObjectGet**](docs/FakeApi.md#fakeGetFreeFormObjectGet) | **GET** /fake/get-free-form-object |
*FakeApi* | [**fakeOuterBooleanSerialize**](docs/FakeApi.md#fakeOuterBooleanSerialize) | **POST** /fake/outer/boolean |
*FakeApi* | [**fakeOuterCompositeSerialize**](docs/FakeApi.md#fakeOuterCompositeSerialize) | **POST** /fake/outer/composite |
*FakeApi* | [**fakeOuterNumberSerialize**](docs/FakeApi.md#fakeOuterNumberSerialize) | **POST** /fake/outer/number |
Expand Down Expand Up @@ -205,6 +206,8 @@ Class | Method | HTTP request | Description
- [Foo](docs/Foo.md)
- [FooGetDefaultResponse](docs/FooGetDefaultResponse.md)
- [FormatTest](docs/FormatTest.md)
- [FreeFormObjectTestClass](docs/FreeFormObjectTestClass.md)
- [FreeFormObjectTestClassProperties](docs/FreeFormObjectTestClassProperties.md)
- [Fruit](docs/Fruit.md)
- [FruitReq](docs/FruitReq.md)
- [GmFruit](docs/GmFruit.md)
Expand Down
29 changes: 29 additions & 0 deletions samples/client/petstore/java/okhttp-gson/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,20 @@ paths:
x-content-type: application/json
x-accepts:
- application/json
/fake/get-free-form-object:
get:
description: Get a free form object or Json string
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/FreeFormObjectTestClass'
description: Success
tags:
- fake
x-accepts:
- application/json
/fake/test-query-parameters:
put:
description: To test the collection format in query parameters
Expand Down Expand Up @@ -2134,6 +2148,16 @@ components:
$ref: '#/components/schemas/File'
type: array
type: object
FreeFormObjectTestClass:
example:
name: name
properties: FreeFormObjectTestClass_properties
properties:
name:
type: string
properties:
$ref: '#/components/schemas/FreeFormObjectTestClass_properties'
type: object
File:
description: Must be named `File` for test.
example:
Expand Down Expand Up @@ -2881,6 +2905,11 @@ components:
required:
- requiredFile
type: object
FreeFormObjectTestClass_properties:
oneOf:
- type: string
- additionalProperties: true
type: object
ArrayOfInlineAllOf_array_allof_dog_property_inner:
allOf:
- properties:
Expand Down
59 changes: 59 additions & 0 deletions samples/client/petstore/java/okhttp-gson/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*

| Method | HTTP request | Description |
|------------- | ------------- | -------------|
| [**fakeGetFreeFormObjectGet**](FakeApi.md#fakeGetFreeFormObjectGet) | **GET** /fake/get-free-form-object | |
| [**fakeOuterBooleanSerialize**](FakeApi.md#fakeOuterBooleanSerialize) | **POST** /fake/outer/boolean | |
| [**fakeOuterCompositeSerialize**](FakeApi.md#fakeOuterCompositeSerialize) | **POST** /fake/outer/composite | |
| [**fakeOuterNumberSerialize**](FakeApi.md#fakeOuterNumberSerialize) | **POST** /fake/outer/number | |
Expand All @@ -26,6 +27,64 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*
| [**testStringMapReference**](FakeApi.md#testStringMapReference) | **POST** /fake/stringMap-reference | test referenced string map |


<a id="fakeGetFreeFormObjectGet"></a>
# **fakeGetFreeFormObjectGet**
> FreeFormObjectTestClass fakeGetFreeFormObjectGet()



Get a free form object or Json string

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

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

FakeApi apiInstance = new FakeApi(defaultClient);
try {
FreeFormObjectTestClass result = apiInstance.fakeGetFreeFormObjectGet();
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling FakeApi#fakeGetFreeFormObjectGet");
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

[**FreeFormObjectTestClass**](FreeFormObjectTestClass.md)

### Authorization

No authorization required

### HTTP request headers

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

### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
| **200** | Success | - |

<a id="fakeOuterBooleanSerialize"></a>
# **fakeOuterBooleanSerialize**
> Boolean fakeOuterBooleanSerialize(body)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


# FreeFormObjectTestClass


## Properties

| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**name** | **String** | | [optional] |
|**properties** | [**FreeFormObjectTestClassProperties**](FreeFormObjectTestClassProperties.md) | | [optional] |