Skip to content

Commit da7105b

Browse files
authored
fix NPE in get type, add test (#19014)
1 parent 6aa825b commit da7105b

File tree

6 files changed

+602
-3
lines changed

6 files changed

+602
-3
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,7 @@ public static String getType(Schema schema) {
21282128
}
21292129

21302130
if (schema instanceof JsonSchema) {
2131-
if (schema.getTypes() != null) {
2131+
if (schema.getTypes() != null && !schema.getTypes().isEmpty()) {
21322132
return String.valueOf(schema.getTypes().iterator().next());
21332133
} else {
21342134
return null;

modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,47 @@ paths:
665665
responses:
666666
'200':
667667
description: Successful Response
668+
"/fake/inline/schema/anyof/path1":
669+
get:
670+
tags:
671+
- fake
672+
responses:
673+
'200':
674+
description: ''
675+
content:
676+
application/json:
677+
schema:
678+
anyOf:
679+
- type: 'null'
680+
- "$ref": "#/components/schemas/myObject"
681+
"/fake/inline/schema/anyof/path2":
682+
get:
683+
tags:
684+
- fake
685+
responses:
686+
'200':
687+
description: ''
688+
content:
689+
application/json:
690+
schema:
691+
anyOf:
692+
- "$ref": "#/components/schemas/myObject"
693+
- type: 'null'
694+
"/fake/inline/schema/anyof/path3":
695+
get:
696+
tags:
697+
- fake
698+
responses:
699+
'200':
700+
description: ''
701+
content:
702+
application/json:
703+
schema:
704+
anyOf:
705+
- type: array
706+
items:
707+
"$ref": "#/components/schemas/myObject"
708+
- type: 'null'
668709
externalDocs:
669710
description: Find out more about Swagger
670711
url: 'http://swagger.io'
@@ -981,3 +1022,5 @@ components:
9811022
AllOfSimpleModel:
9821023
allOf:
9831024
- $ref: '#/components/schemas/SimpleModelWithArrayProperty'
1025+
myObject:
1026+
type: object

samples/client/petstore/java/okhttp-gson-3.1/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ public class Example {
9393

9494
FakeApi apiInstance = new FakeApi(defaultClient);
9595
try {
96-
Object result = apiInstance.op1();
96+
Object result = apiInstance.fakeInlineSchemaAnyofPath1Get();
9797
System.out.println(result);
9898
} catch (ApiException e) {
99-
System.err.println("Exception when calling FakeApi#op1");
99+
System.err.println("Exception when calling FakeApi#fakeInlineSchemaAnyofPath1Get");
100100
System.err.println("Status code: " + e.getCode());
101101
System.err.println("Reason: " + e.getResponseBody());
102102
System.err.println("Response headers: " + e.getResponseHeaders());
@@ -113,6 +113,9 @@ All URIs are relative to *http://petstore.swagger.io/v2*
113113

114114
Class | Method | HTTP request | Description
115115
------------ | ------------- | ------------- | -------------
116+
*FakeApi* | [**fakeInlineSchemaAnyofPath1Get**](docs/FakeApi.md#fakeInlineSchemaAnyofPath1Get) | **GET** /fake/inline/schema/anyof/path1 |
117+
*FakeApi* | [**fakeInlineSchemaAnyofPath2Get**](docs/FakeApi.md#fakeInlineSchemaAnyofPath2Get) | **GET** /fake/inline/schema/anyof/path2 |
118+
*FakeApi* | [**fakeInlineSchemaAnyofPath3Get**](docs/FakeApi.md#fakeInlineSchemaAnyofPath3Get) | **GET** /fake/inline/schema/anyof/path3 |
116119
*FakeApi* | [**op1**](docs/FakeApi.md#op1) | **POST** /fake/api/changeowner | op1
117120
*FakeApi* | [**op2**](docs/FakeApi.md#op2) | **POST** /fake/api/changename | op2
118121
*FakeApi* | [**op3**](docs/FakeApi.md#op3) | **POST** /fake/api/query/enum | op3

samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,47 @@ paths:
751751
- fake
752752
x-accepts:
753753
- application/json
754+
/fake/inline/schema/anyof/path1:
755+
get:
756+
responses:
757+
"200":
758+
content:
759+
application/json:
760+
schema:
761+
$ref: '#/components/schemas/myObject'
762+
description: ""
763+
tags:
764+
- fake
765+
x-accepts:
766+
- application/json
767+
/fake/inline/schema/anyof/path2:
768+
get:
769+
responses:
770+
"200":
771+
content:
772+
application/json:
773+
schema:
774+
$ref: '#/components/schemas/myObject'
775+
description: ""
776+
tags:
777+
- fake
778+
x-accepts:
779+
- application/json
780+
/fake/inline/schema/anyof/path3:
781+
get:
782+
responses:
783+
"200":
784+
content:
785+
application/json:
786+
schema:
787+
items:
788+
$ref: '#/components/schemas/myObject'
789+
nullable: true
790+
description: ""
791+
tags:
792+
- fake
793+
x-accepts:
794+
- application/json
754795
components:
755796
parameters:
756797
ref_to_uuid:
@@ -1074,6 +1115,8 @@ components:
10741115
AllOfSimpleModel:
10751116
allOf:
10761117
- $ref: '#/components/schemas/SimpleModelWithArrayProperty'
1118+
myObject:
1119+
type: object
10771120
updatePetWithForm_request:
10781121
properties:
10791122
name:

samples/client/petstore/java/okhttp-gson-3.1/docs/FakeApi.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All URIs are relative to *http://petstore.swagger.io/v2*
44

55
| Method | HTTP request | Description |
66
|------------- | ------------- | -------------|
7+
| [**fakeInlineSchemaAnyofPath1Get**](FakeApi.md#fakeInlineSchemaAnyofPath1Get) | **GET** /fake/inline/schema/anyof/path1 | |
8+
| [**fakeInlineSchemaAnyofPath2Get**](FakeApi.md#fakeInlineSchemaAnyofPath2Get) | **GET** /fake/inline/schema/anyof/path2 | |
9+
| [**fakeInlineSchemaAnyofPath3Get**](FakeApi.md#fakeInlineSchemaAnyofPath3Get) | **GET** /fake/inline/schema/anyof/path3 | |
710
| [**op1**](FakeApi.md#op1) | **POST** /fake/api/changeowner | op1 |
811
| [**op2**](FakeApi.md#op2) | **POST** /fake/api/changename | op2 |
912
| [**op3**](FakeApi.md#op3) | **POST** /fake/api/query/enum | op3 |
@@ -13,6 +16,174 @@ All URIs are relative to *http://petstore.swagger.io/v2*
1316
| [**responseRefToRef**](FakeApi.md#responseRefToRef) | **GET** /ref/ref | |
1417

1518

19+
<a id="fakeInlineSchemaAnyofPath1Get"></a>
20+
# **fakeInlineSchemaAnyofPath1Get**
21+
> Object fakeInlineSchemaAnyofPath1Get()
22+
23+
24+
25+
### Example
26+
```java
27+
// Import classes:
28+
import org.openapitools.client.ApiClient;
29+
import org.openapitools.client.ApiException;
30+
import org.openapitools.client.Configuration;
31+
import org.openapitools.client.models.*;
32+
import org.openapitools.client.api.FakeApi;
33+
34+
public class Example {
35+
public static void main(String[] args) {
36+
ApiClient defaultClient = Configuration.getDefaultApiClient();
37+
defaultClient.setBasePath("http://petstore.swagger.io/v2");
38+
39+
FakeApi apiInstance = new FakeApi(defaultClient);
40+
try {
41+
Object result = apiInstance.fakeInlineSchemaAnyofPath1Get();
42+
System.out.println(result);
43+
} catch (ApiException e) {
44+
System.err.println("Exception when calling FakeApi#fakeInlineSchemaAnyofPath1Get");
45+
System.err.println("Status code: " + e.getCode());
46+
System.err.println("Reason: " + e.getResponseBody());
47+
System.err.println("Response headers: " + e.getResponseHeaders());
48+
e.printStackTrace();
49+
}
50+
}
51+
}
52+
```
53+
54+
### Parameters
55+
This endpoint does not need any parameter.
56+
57+
### Return type
58+
59+
**Object**
60+
61+
### Authorization
62+
63+
No authorization required
64+
65+
### HTTP request headers
66+
67+
- **Content-Type**: Not defined
68+
- **Accept**: application/json
69+
70+
### HTTP response details
71+
| Status code | Description | Response headers |
72+
|-------------|-------------|------------------|
73+
| **200** | | - |
74+
75+
<a id="fakeInlineSchemaAnyofPath2Get"></a>
76+
# **fakeInlineSchemaAnyofPath2Get**
77+
> Object fakeInlineSchemaAnyofPath2Get()
78+
79+
80+
81+
### Example
82+
```java
83+
// Import classes:
84+
import org.openapitools.client.ApiClient;
85+
import org.openapitools.client.ApiException;
86+
import org.openapitools.client.Configuration;
87+
import org.openapitools.client.models.*;
88+
import org.openapitools.client.api.FakeApi;
89+
90+
public class Example {
91+
public static void main(String[] args) {
92+
ApiClient defaultClient = Configuration.getDefaultApiClient();
93+
defaultClient.setBasePath("http://petstore.swagger.io/v2");
94+
95+
FakeApi apiInstance = new FakeApi(defaultClient);
96+
try {
97+
Object result = apiInstance.fakeInlineSchemaAnyofPath2Get();
98+
System.out.println(result);
99+
} catch (ApiException e) {
100+
System.err.println("Exception when calling FakeApi#fakeInlineSchemaAnyofPath2Get");
101+
System.err.println("Status code: " + e.getCode());
102+
System.err.println("Reason: " + e.getResponseBody());
103+
System.err.println("Response headers: " + e.getResponseHeaders());
104+
e.printStackTrace();
105+
}
106+
}
107+
}
108+
```
109+
110+
### Parameters
111+
This endpoint does not need any parameter.
112+
113+
### Return type
114+
115+
**Object**
116+
117+
### Authorization
118+
119+
No authorization required
120+
121+
### HTTP request headers
122+
123+
- **Content-Type**: Not defined
124+
- **Accept**: application/json
125+
126+
### HTTP response details
127+
| Status code | Description | Response headers |
128+
|-------------|-------------|------------------|
129+
| **200** | | - |
130+
131+
<a id="fakeInlineSchemaAnyofPath3Get"></a>
132+
# **fakeInlineSchemaAnyofPath3Get**
133+
> List&lt;Object&gt; fakeInlineSchemaAnyofPath3Get()
134+
135+
136+
137+
### Example
138+
```java
139+
// Import classes:
140+
import org.openapitools.client.ApiClient;
141+
import org.openapitools.client.ApiException;
142+
import org.openapitools.client.Configuration;
143+
import org.openapitools.client.models.*;
144+
import org.openapitools.client.api.FakeApi;
145+
146+
public class Example {
147+
public static void main(String[] args) {
148+
ApiClient defaultClient = Configuration.getDefaultApiClient();
149+
defaultClient.setBasePath("http://petstore.swagger.io/v2");
150+
151+
FakeApi apiInstance = new FakeApi(defaultClient);
152+
try {
153+
List<Object> result = apiInstance.fakeInlineSchemaAnyofPath3Get();
154+
System.out.println(result);
155+
} catch (ApiException e) {
156+
System.err.println("Exception when calling FakeApi#fakeInlineSchemaAnyofPath3Get");
157+
System.err.println("Status code: " + e.getCode());
158+
System.err.println("Reason: " + e.getResponseBody());
159+
System.err.println("Response headers: " + e.getResponseHeaders());
160+
e.printStackTrace();
161+
}
162+
}
163+
}
164+
```
165+
166+
### Parameters
167+
This endpoint does not need any parameter.
168+
169+
### Return type
170+
171+
**List&lt;Object&gt;**
172+
173+
### Authorization
174+
175+
No authorization required
176+
177+
### HTTP request headers
178+
179+
- **Content-Type**: Not defined
180+
- **Accept**: application/json
181+
182+
### HTTP response details
183+
| Status code | Description | Response headers |
184+
|-------------|-------------|------------------|
185+
| **200** | | - |
186+
16187
<a id="op1"></a>
17188
# **op1**
18189
> Object op1()

0 commit comments

Comments
 (0)