better npe handling when processing openapi 3.1 spec

This commit is contained in:
William Cheng
2024-01-13 11:21:08 +08:00
parent 75ef9e0b02
commit c6efe8810d
6 changed files with 445 additions and 3 deletions

View File

@@ -933,6 +933,11 @@ public class OpenAPINormalizer {
return new Schema(); return new Schema();
} }
// return schema if nothing in 3.1 spec types to normalize
if (schema.getTypes() == null) {
return schema;
}
// process null // process null
if (schema.getTypes().contains("null")) { if (schema.getTypes().contains("null")) {
schema.setNullable(true); schema.setNullable(true);

View File

@@ -609,6 +609,44 @@ paths:
$ref: '#/components/responses/ref' $ref: '#/components/responses/ref'
parameters: parameters:
- $ref: '#/components/parameters/ref_to_uuid' - $ref: '#/components/parameters/ref_to_uuid'
"/fake/api/changeowner":
post:
summary: op1
operationId: op1
tags:
- fake
parameters: []
responses:
'201':
description: Successful Response
content:
application/json:
schema: {}
'422':
description: Validation Error
content:
application/json:
schema:
"$ref": "#/components/schemas/HTTPValidationError"
"/fake/api/changename":
post:
summary: op2
operationId: op2
tags:
- fake
parameters: []
responses:
'201':
description: Successful Response
content:
application/json:
schema: {}
'422':
description: Validation Error
content:
application/json:
schema:
"$ref": "#/components/schemas/HTTPValidationError"
externalDocs: externalDocs:
description: Find out more about Swagger description: Find out more about Swagger
url: 'http://swagger.io' url: 'http://swagger.io'
@@ -847,3 +885,7 @@ components:
description: test array in 3.1 spec description: test array in 3.1 spec
items: items:
type: string type: string
HTTPValidationError:
properties: {}
type: object
title: HTTPValidationError

View File

@@ -91,12 +91,11 @@ public class Example {
defaultClient.setBasePath("http://petstore.swagger.io/v2"); defaultClient.setBasePath("http://petstore.swagger.io/v2");
FakeApi apiInstance = new FakeApi(defaultClient); FakeApi apiInstance = new FakeApi(defaultClient);
UUID refToUuid = UUID.fromString("61864654-6e6b-4152-a62f-795fdd606bc2"); // UUID | to test ref to parameter (uuid)
try { try {
String result = apiInstance.refToRefParameter(refToUuid); Object result = apiInstance.op1();
System.out.println(result); System.out.println(result);
} catch (ApiException e) { } catch (ApiException e) {
System.err.println("Exception when calling FakeApi#refToRefParameter"); System.err.println("Exception when calling FakeApi#op1");
System.err.println("Status code: " + e.getCode()); System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody()); System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders()); System.err.println("Response headers: " + e.getResponseHeaders());
@@ -113,6 +112,8 @@ All URIs are relative to *http://petstore.swagger.io/v2*
Class | Method | HTTP request | Description Class | Method | HTTP request | Description
------------ | ------------- | ------------- | ------------- ------------ | ------------- | ------------- | -------------
*FakeApi* | [**op1**](docs/FakeApi.md#op1) | **POST** /fake/api/changeowner | op1
*FakeApi* | [**op2**](docs/FakeApi.md#op2) | **POST** /fake/api/changename | op2
*FakeApi* | [**refToRefParameter**](docs/FakeApi.md#refToRefParameter) | **GET** /ref/ref_to_parameter | *FakeApi* | [**refToRefParameter**](docs/FakeApi.md#refToRefParameter) | **GET** /ref/ref_to_parameter |
*FakeApi* | [**responseNoRef**](docs/FakeApi.md#responseNoRef) | **GET** /no_ref | *FakeApi* | [**responseNoRef**](docs/FakeApi.md#responseNoRef) | **GET** /no_ref |
*FakeApi* | [**responseRefToNoRef**](docs/FakeApi.md#responseRefToNoRef) | **GET** /ref/no_ref | *FakeApi* | [**responseRefToNoRef**](docs/FakeApi.md#responseRefToNoRef) | **GET** /ref/no_ref |

View File

@@ -654,6 +654,46 @@ paths:
x-accepts: text/plain x-accepts: text/plain
parameters: parameters:
- $ref: '#/components/parameters/ref_to_uuid' - $ref: '#/components/parameters/ref_to_uuid'
/fake/api/changeowner:
post:
operationId: op1
parameters: []
responses:
"201":
content:
application/json:
schema: {}
description: Successful Response
"422":
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
description: Validation Error
summary: op1
tags:
- fake
x-accepts: application/json
/fake/api/changename:
post:
operationId: op2
parameters: []
responses:
"201":
content:
application/json:
schema: {}
description: Successful Response
"422":
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
description: Validation Error
summary: op2
tags:
- fake
x-accepts: application/json
components: components:
parameters: parameters:
ref_to_uuid: ref_to_uuid:
@@ -912,6 +952,10 @@ components:
items: items:
type: string type: string
type: array type: array
HTTPValidationError:
properties: {}
title: HTTPValidationError
type: object
updatePetWithForm_request: updatePetWithForm_request:
properties: properties:
name: name:

View File

@@ -4,12 +4,128 @@ All URIs are relative to *http://petstore.swagger.io/v2*
| Method | HTTP request | Description | | Method | HTTP request | Description |
|------------- | ------------- | -------------| |------------- | ------------- | -------------|
| [**op1**](FakeApi.md#op1) | **POST** /fake/api/changeowner | op1 |
| [**op2**](FakeApi.md#op2) | **POST** /fake/api/changename | op2 |
| [**refToRefParameter**](FakeApi.md#refToRefParameter) | **GET** /ref/ref_to_parameter | | | [**refToRefParameter**](FakeApi.md#refToRefParameter) | **GET** /ref/ref_to_parameter | |
| [**responseNoRef**](FakeApi.md#responseNoRef) | **GET** /no_ref | | | [**responseNoRef**](FakeApi.md#responseNoRef) | **GET** /no_ref | |
| [**responseRefToNoRef**](FakeApi.md#responseRefToNoRef) | **GET** /ref/no_ref | | | [**responseRefToNoRef**](FakeApi.md#responseRefToNoRef) | **GET** /ref/no_ref | |
| [**responseRefToRef**](FakeApi.md#responseRefToRef) | **GET** /ref/ref | | | [**responseRefToRef**](FakeApi.md#responseRefToRef) | **GET** /ref/ref | |
<a id="op1"></a>
# **op1**
> Object op1()
op1
### 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/v2");
FakeApi apiInstance = new FakeApi(defaultClient);
try {
Object result = apiInstance.op1();
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling FakeApi#op1");
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
**Object**
### Authorization
No authorization required
### HTTP request headers
- **Content-Type**: Not defined
- **Accept**: application/json
### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
| **201** | Successful Response | - |
| **422** | Validation Error | - |
<a id="op2"></a>
# **op2**
> Object op2()
op2
### 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/v2");
FakeApi apiInstance = new FakeApi(defaultClient);
try {
Object result = apiInstance.op2();
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling FakeApi#op2");
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
**Object**
### Authorization
No authorization required
### HTTP request headers
- **Content-Type**: Not defined
- **Accept**: application/json
### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
| **201** | Successful Response | - |
| **422** | Validation Error | - |
<a id="refToRefParameter"></a> <a id="refToRefParameter"></a>
# **refToRefParameter** # **refToRefParameter**
> String refToRefParameter(refToUuid) > String refToRefParameter(refToUuid)

View File

@@ -72,6 +72,240 @@ public class FakeApi {
this.localCustomBaseUrl = customBaseUrl; this.localCustomBaseUrl = customBaseUrl;
} }
/**
* Build call for op1
* @param _callback Callback for upload/download progress
* @return Call to execute
* @throws ApiException If fail to serialize the request body object
* @http.response.details
<table summary="Response Details" border="1">
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
<tr><td> 201 </td><td> Successful Response </td><td> - </td></tr>
<tr><td> 422 </td><td> Validation Error </td><td> - </td></tr>
</table>
*/
public okhttp3.Call op1Call(final ApiCallback _callback) throws ApiException {
String basePath = null;
// Operation Servers
String[] localBasePaths = new String[] { };
// Determine Base Path to Use
if (localCustomBaseUrl != null){
basePath = localCustomBaseUrl;
} else if ( localBasePaths.length > 0 ) {
basePath = localBasePaths[localHostIndex];
} else {
basePath = null;
}
Object localVarPostBody = null;
// create path and map variables
String localVarPath = "/fake/api/changeowner";
List<Pair> localVarQueryParams = new ArrayList<Pair>();
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
final String[] localVarAccepts = {
"application/json"
};
final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
if (localVarAccept != null) {
localVarHeaderParams.put("Accept", localVarAccept);
}
final String[] localVarContentTypes = {
};
final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
if (localVarContentType != null) {
localVarHeaderParams.put("Content-Type", localVarContentType);
}
String[] localVarAuthNames = new String[] { };
return localVarApiClient.buildCall(basePath, localVarPath, "POST", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback);
}
@SuppressWarnings("rawtypes")
private okhttp3.Call op1ValidateBeforeCall(final ApiCallback _callback) throws ApiException {
return op1Call(_callback);
}
/**
* op1
*
* @return Object
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
* @http.response.details
<table summary="Response Details" border="1">
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
<tr><td> 201 </td><td> Successful Response </td><td> - </td></tr>
<tr><td> 422 </td><td> Validation Error </td><td> - </td></tr>
</table>
*/
public Object op1() throws ApiException {
ApiResponse<Object> localVarResp = op1WithHttpInfo();
return localVarResp.getData();
}
/**
* op1
*
* @return ApiResponse&lt;Object&gt;
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
* @http.response.details
<table summary="Response Details" border="1">
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
<tr><td> 201 </td><td> Successful Response </td><td> - </td></tr>
<tr><td> 422 </td><td> Validation Error </td><td> - </td></tr>
</table>
*/
public ApiResponse<Object> op1WithHttpInfo() throws ApiException {
okhttp3.Call localVarCall = op1ValidateBeforeCall(null);
Type localVarReturnType = new TypeToken<Object>(){}.getType();
return localVarApiClient.execute(localVarCall, localVarReturnType);
}
/**
* op1 (asynchronously)
*
* @param _callback The callback to be executed when the API call finishes
* @return The request call
* @throws ApiException If fail to process the API call, e.g. serializing the request body object
* @http.response.details
<table summary="Response Details" border="1">
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
<tr><td> 201 </td><td> Successful Response </td><td> - </td></tr>
<tr><td> 422 </td><td> Validation Error </td><td> - </td></tr>
</table>
*/
public okhttp3.Call op1Async(final ApiCallback<Object> _callback) throws ApiException {
okhttp3.Call localVarCall = op1ValidateBeforeCall(_callback);
Type localVarReturnType = new TypeToken<Object>(){}.getType();
localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
return localVarCall;
}
/**
* Build call for op2
* @param _callback Callback for upload/download progress
* @return Call to execute
* @throws ApiException If fail to serialize the request body object
* @http.response.details
<table summary="Response Details" border="1">
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
<tr><td> 201 </td><td> Successful Response </td><td> - </td></tr>
<tr><td> 422 </td><td> Validation Error </td><td> - </td></tr>
</table>
*/
public okhttp3.Call op2Call(final ApiCallback _callback) throws ApiException {
String basePath = null;
// Operation Servers
String[] localBasePaths = new String[] { };
// Determine Base Path to Use
if (localCustomBaseUrl != null){
basePath = localCustomBaseUrl;
} else if ( localBasePaths.length > 0 ) {
basePath = localBasePaths[localHostIndex];
} else {
basePath = null;
}
Object localVarPostBody = null;
// create path and map variables
String localVarPath = "/fake/api/changename";
List<Pair> localVarQueryParams = new ArrayList<Pair>();
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
final String[] localVarAccepts = {
"application/json"
};
final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
if (localVarAccept != null) {
localVarHeaderParams.put("Accept", localVarAccept);
}
final String[] localVarContentTypes = {
};
final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
if (localVarContentType != null) {
localVarHeaderParams.put("Content-Type", localVarContentType);
}
String[] localVarAuthNames = new String[] { };
return localVarApiClient.buildCall(basePath, localVarPath, "POST", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback);
}
@SuppressWarnings("rawtypes")
private okhttp3.Call op2ValidateBeforeCall(final ApiCallback _callback) throws ApiException {
return op2Call(_callback);
}
/**
* op2
*
* @return Object
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
* @http.response.details
<table summary="Response Details" border="1">
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
<tr><td> 201 </td><td> Successful Response </td><td> - </td></tr>
<tr><td> 422 </td><td> Validation Error </td><td> - </td></tr>
</table>
*/
public Object op2() throws ApiException {
ApiResponse<Object> localVarResp = op2WithHttpInfo();
return localVarResp.getData();
}
/**
* op2
*
* @return ApiResponse&lt;Object&gt;
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
* @http.response.details
<table summary="Response Details" border="1">
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
<tr><td> 201 </td><td> Successful Response </td><td> - </td></tr>
<tr><td> 422 </td><td> Validation Error </td><td> - </td></tr>
</table>
*/
public ApiResponse<Object> op2WithHttpInfo() throws ApiException {
okhttp3.Call localVarCall = op2ValidateBeforeCall(null);
Type localVarReturnType = new TypeToken<Object>(){}.getType();
return localVarApiClient.execute(localVarCall, localVarReturnType);
}
/**
* op2 (asynchronously)
*
* @param _callback The callback to be executed when the API call finishes
* @return The request call
* @throws ApiException If fail to process the API call, e.g. serializing the request body object
* @http.response.details
<table summary="Response Details" border="1">
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
<tr><td> 201 </td><td> Successful Response </td><td> - </td></tr>
<tr><td> 422 </td><td> Validation Error </td><td> - </td></tr>
</table>
*/
public okhttp3.Call op2Async(final ApiCallback<Object> _callback) throws ApiException {
okhttp3.Call localVarCall = op2ValidateBeforeCall(_callback);
Type localVarReturnType = new TypeToken<Object>(){}.getType();
localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
return localVarCall;
}
/** /**
* Build call for refToRefParameter * Build call for refToRefParameter
* @param refToUuid to test ref to parameter (uuid) (required) * @param refToUuid to test ref to parameter (uuid) (required)