Skip to content

Commit

Permalink
Better handling of allOf in request body (#16991)
Browse files Browse the repository at this point in the history
* better handling of allOf in request body, add tests

* additional checks

* fix description
  • Loading branch information
wing328 authored Nov 5, 2023
1 parent 339596a commit 4920814
Show file tree
Hide file tree
Showing 41 changed files with 2,682 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7581,8 +7581,19 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
String name = null;
LOGGER.debug("Request body = {}", body);
Schema schema = ModelUtils.getSchemaFromRequestBody(body);
codegenParameter.setContent(getContent(body.getContent(), imports, "RequestBody"));
Schema original = null;
// check if it's allOf (only 1 sub schema) with or without default/nullable/etc set in the top level
if (ModelUtils.isAllOf(schema) && schema.getAllOf().size() == 1 &&
schema.getType() == null && schema.getTypes() == null) {
if (schema.getAllOf().get(0) instanceof Schema) {
original = schema;
schema = (Schema) schema.getAllOf().get(0);
} else {
LOGGER.error("Unknown type in allOf schema. Please report the issue via openapi-generator's Github issue tracker.");
}
}

codegenParameter.setContent(getContent(body.getContent(), imports, "RequestBody"));
if (StringUtils.isNotBlank(schema.get$ref())) {
name = ModelUtils.getSimpleRef(schema.get$ref());
}
Expand Down Expand Up @@ -7651,6 +7662,20 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
// should be overridden by lang codegen
setParameterExampleValue(codegenParameter, body);

// restore original schema with description, extensions etc
if (original != null) {
schema = original;
// evaluate common attributes such as description if defined in the top level
if (schema.getDescription() != null) {
codegenParameter.description = escapeText(schema.getDescription());
codegenParameter.unescapedDescription = schema.getDescription();
}

if (original.getExtensions() != null) {
codegenParameter.vendorExtensions.putAll(original.getExtensions());
}
}

return codegenParameter;
}

Expand Down
23 changes: 23 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/echo_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,22 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Pet'
/echo/body/allOf/Pet:
post:
tags:
- body
summary: Test body parameter(s)
description: Test body parameter(s)
operationId: test/echo/body/allOf/Pet
requestBody:
$ref: '#/components/requestBodies/AllOfPet'
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
/echo/body/Pet/response_string:
post:
tags:
Expand Down Expand Up @@ -554,6 +570,13 @@ components:
schema:
$ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
AllOfPet:
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
Tag:
content:
application/json:
Expand Down
1 change: 1 addition & 0 deletions samples/client/echo_api/csharp-restsharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Class | Method | HTTP request | Description
*BodyApi* | [**TestBinaryGif**](docs/BodyApi.md#testbinarygif) | **POST** /binary/gif | Test binary (gif) response body
*BodyApi* | [**TestBodyApplicationOctetstreamBinary**](docs/BodyApi.md#testbodyapplicationoctetstreambinary) | **POST** /body/application/octetstream/binary | Test body parameter(s)
*BodyApi* | [**TestBodyMultipartFormdataArrayOfBinary**](docs/BodyApi.md#testbodymultipartformdataarrayofbinary) | **POST** /body/application/octetstream/array_of_binary | Test array of binary in multipart mime
*BodyApi* | [**TestEchoBodyAllOfPet**](docs/BodyApi.md#testechobodyallofpet) | **POST** /echo/body/allOf/Pet | Test body parameter(s)
*BodyApi* | [**TestEchoBodyFreeFormObjectResponseString**](docs/BodyApi.md#testechobodyfreeformobjectresponsestring) | **POST** /echo/body/FreeFormObject/response_string | Test free form object
*BodyApi* | [**TestEchoBodyPet**](docs/BodyApi.md#testechobodypet) | **POST** /echo/body/Pet | Test body parameter(s)
*BodyApi* | [**TestEchoBodyPetResponseString**](docs/BodyApi.md#testechobodypetresponsestring) | **POST** /echo/body/Pet/response_string | Test empty response body
Expand Down
23 changes: 23 additions & 0 deletions samples/client/echo_api/csharp-restsharp/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,22 @@ paths:
summary: Test body parameter(s)
tags:
- body
/echo/body/allOf/Pet:
post:
description: Test body parameter(s)
operationId: test/echo/body/allOf/Pet
requestBody:
$ref: '#/components/requestBodies/AllOfPet'
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
description: Successful operation
summary: Test body parameter(s)
tags:
- body
/echo/body/Pet/response_string:
post:
description: Test empty response body
Expand Down Expand Up @@ -513,6 +529,13 @@ components:
schema:
$ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
AllOfPet:
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
Tag:
content:
application/json:
Expand Down
92 changes: 92 additions & 0 deletions samples/client/echo_api/csharp-restsharp/docs/BodyApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All URIs are relative to *http://localhost:3000*
| [**TestBinaryGif**](BodyApi.md#testbinarygif) | **POST** /binary/gif | Test binary (gif) response body |
| [**TestBodyApplicationOctetstreamBinary**](BodyApi.md#testbodyapplicationoctetstreambinary) | **POST** /body/application/octetstream/binary | Test body parameter(s) |
| [**TestBodyMultipartFormdataArrayOfBinary**](BodyApi.md#testbodymultipartformdataarrayofbinary) | **POST** /body/application/octetstream/array_of_binary | Test array of binary in multipart mime |
| [**TestEchoBodyAllOfPet**](BodyApi.md#testechobodyallofpet) | **POST** /echo/body/allOf/Pet | Test body parameter(s) |
| [**TestEchoBodyFreeFormObjectResponseString**](BodyApi.md#testechobodyfreeformobjectresponsestring) | **POST** /echo/body/FreeFormObject/response_string | Test free form object |
| [**TestEchoBodyPet**](BodyApi.md#testechobodypet) | **POST** /echo/body/Pet | Test body parameter(s) |
| [**TestEchoBodyPetResponseString**](BodyApi.md#testechobodypetresponsestring) | **POST** /echo/body/Pet/response_string | Test empty response body |
Expand Down Expand Up @@ -273,6 +274,97 @@ No authorization required
- **Accept**: text/plain


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

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

<a id="testechobodyallofpet"></a>
# **TestEchoBodyAllOfPet**
> Pet TestEchoBodyAllOfPet (Pet? pet = null)
Test body parameter(s)

Test body parameter(s)

### Example
```csharp
using System.Collections.Generic;
using System.Diagnostics;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;

namespace Example
{
public class TestEchoBodyAllOfPetExample
{
public static void Main()
{
Configuration config = new Configuration();
config.BasePath = "http://localhost:3000";
var apiInstance = new BodyApi(config);
var pet = new Pet?(); // Pet? | Pet object that needs to be added to the store (optional)
try
{
// Test body parameter(s)
Pet result = apiInstance.TestEchoBodyAllOfPet(pet);
Debug.WriteLine(result);
}
catch (ApiException e)
{
Debug.Print("Exception when calling BodyApi.TestEchoBodyAllOfPet: " + e.Message);
Debug.Print("Status Code: " + e.ErrorCode);
Debug.Print(e.StackTrace);
}
}
}
}
```

#### Using the TestEchoBodyAllOfPetWithHttpInfo variant
This returns an ApiResponse object which contains the response data, status code and headers.

```csharp
try
{
// Test body parameter(s)
ApiResponse<Pet> response = apiInstance.TestEchoBodyAllOfPetWithHttpInfo(pet);
Debug.Write("Status Code: " + response.StatusCode);
Debug.Write("Response Headers: " + response.Headers);
Debug.Write("Response Body: " + response.Data);
}
catch (ApiException e)
{
Debug.Print("Exception when calling BodyApi.TestEchoBodyAllOfPetWithHttpInfo: " + e.Message);
Debug.Print("Status Code: " + e.ErrorCode);
Debug.Print(e.StackTrace);
}
```

### Parameters

| Name | Type | Description | Notes |
|------|------|-------------|-------|
| **pet** | [**Pet?**](Pet?.md) | Pet object that needs to be added to the store | [optional] |

### Return type

[**Pet**](Pet.md)

### Authorization

No authorization required

### HTTP request headers

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


### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
Expand Down
Loading

0 comments on commit 4920814

Please sign in to comment.