Skip to content

Commit 96fad5e

Browse files
authored
[csharp] Fix getModelFromParameter (#18137)
* fix getModelFromParameter * minor fix
1 parent 12849cc commit 96fad5e

41 files changed

Lines changed: 1050 additions & 31 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ protected void patchVendorExtensionNullableValueType(CodegenParameter parameter)
11221122
* Returns the model related to the given parameter
11231123
*/
11241124
private CodegenModel getModelFromParameter(List<ModelMap> allModels, CodegenParameter parameter) {
1125-
return parameter.isModel
1125+
return parameter.isModel || parameter.getIsEnumOrRef()
11261126
? allModels.stream().map(m -> m.getModel()).filter(m -> m.getClassname().equals(parameter.dataType)).findFirst().orElse(null)
11271127
: null;
11281128
}

modules/openapi-generator/src/test/resources/3_0/aspnetcore/petstore.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ tags:
1818
- name: user
1919
description: Operations about user
2020
paths:
21+
/test:
22+
get:
23+
summary: Test API
24+
parameters:
25+
- in: query
26+
name: testQuery
27+
schema:
28+
$ref: '#/components/schemas/TestEnum'
29+
required: false
30+
responses:
31+
'200':
32+
description: OK
2133
/pet:
2234
post:
2335
tags:
@@ -620,6 +632,11 @@ components:
620632
name: api_key
621633
in: header
622634
schemas:
635+
TestEnum:
636+
type: string
637+
enum:
638+
- A
639+
- B
623640
Order:
624641
title: Pet Order
625642
description: An order for a pets from the pet store

samples/client/echo_api/csharp-restsharp/docs/PathApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace Example
3434
var pathString = "pathString_example"; // string |
3535
var pathInteger = 56; // int |
3636
var enumNonrefStringPath = "success"; // string |
37-
var enumRefStringPath = new StringEnumRef(); // StringEnumRef |
37+
var enumRefStringPath = (StringEnumRef) "success"; // StringEnumRef |
3838
3939
try
4040
{
@@ -80,7 +80,7 @@ catch (ApiException e)
8080
| **pathString** | **string** | | |
8181
| **pathInteger** | **int** | | |
8282
| **enumNonrefStringPath** | **string** | | |
83-
| **enumRefStringPath** | [**StringEnumRef**](StringEnumRef.md) | | |
83+
| **enumRefStringPath** | **StringEnumRef** | | |
8484

8585
### Return type
8686

samples/client/echo_api/csharp-restsharp/src/Org.OpenAPITools/Api/PathApi.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,6 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestsPathStringPathStringInte
256256
throw new Org.OpenAPITools.Client.ApiException(400, "Missing required parameter 'enumNonrefStringPath' when calling PathApi->TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPath");
257257
}
258258

259-
// verify the required parameter 'enumRefStringPath' is set
260-
if (enumRefStringPath == null)
261-
{
262-
throw new Org.OpenAPITools.Client.ApiException(400, "Missing required parameter 'enumRefStringPath' when calling PathApi->TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPath");
263-
}
264-
265259
Org.OpenAPITools.Client.RequestOptions localVarRequestOptions = new Org.OpenAPITools.Client.RequestOptions();
266260

267261
string[] _contentTypes = new string[] {
@@ -349,12 +343,6 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestsPathStringPathStringInte
349343
throw new Org.OpenAPITools.Client.ApiException(400, "Missing required parameter 'enumNonrefStringPath' when calling PathApi->TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPath");
350344
}
351345

352-
// verify the required parameter 'enumRefStringPath' is set
353-
if (enumRefStringPath == null)
354-
{
355-
throw new Org.OpenAPITools.Client.ApiException(400, "Missing required parameter 'enumRefStringPath' when calling PathApi->TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPath");
356-
}
357-
358346

359347
Org.OpenAPITools.Client.RequestOptions localVarRequestOptions = new Org.OpenAPITools.Client.RequestOptions();
360348

samples/client/others/csharp-complex-files/docs/MultipartApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ namespace Example
118118
Configuration config = new Configuration();
119119
config.BasePath = "http://localhost";
120120
var apiInstance = new MultipartApi(config);
121-
var status = new MultipartMixedStatus(); // MultipartMixedStatus |
121+
var status = (MultipartMixedStatus) "ALLOWED"; // MultipartMixedStatus |
122122
var file = new System.IO.MemoryStream(System.IO.File.ReadAllBytes("/path/to/file.txt")); // System.IO.Stream | a file
123123
var marker = new MultipartMixedRequestMarker(); // MultipartMixedRequestMarker | (optional)
124124
var statusArray = new List<MultipartMixedStatus>(); // List<MultipartMixedStatus> | (optional)
@@ -158,7 +158,7 @@ catch (ApiException e)
158158

159159
| Name | Type | Description | Notes |
160160
|------|------|-------------|-------|
161-
| **status** | [**MultipartMixedStatus**](MultipartMixedStatus.md) | | |
161+
| **status** | **MultipartMixedStatus** | | |
162162
| **file** | **System.IO.Stream****System.IO.Stream** | a file | |
163163
| **marker** | [**MultipartMixedRequestMarker**](MultipartMixedRequestMarker.md) | | [optional] |
164164
| **statusArray** | [**List&lt;MultipartMixedStatus&gt;**](MultipartMixedStatus.md) | | [optional] |

samples/client/others/csharp-complex-files/src/Org.OpenAPITools/Api/MultipartApi.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,6 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
480480
/// <returns>ApiResponse of Object(void)</returns>
481481
public Org.OpenAPITools.Client.ApiResponse<Object> MultipartMixedWithHttpInfo(MultipartMixedStatus status, System.IO.Stream file, MultipartMixedRequestMarker marker = default(MultipartMixedRequestMarker), List<MultipartMixedStatus> statusArray = default(List<MultipartMixedStatus>), int operationIndex = 0)
482482
{
483-
// verify the required parameter 'status' is set
484-
if (status == null)
485-
{
486-
throw new Org.OpenAPITools.Client.ApiException(400, "Missing required parameter 'status' when calling MultipartApi->MultipartMixed");
487-
}
488-
489483
// verify the required parameter 'file' is set
490484
if (file == null)
491485
{
@@ -514,7 +508,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
514508
localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept);
515509
}
516510

517-
localVarRequestOptions.FormParameters.Add("status", Org.OpenAPITools.Client.ClientUtils.Serialize(status)); // form parameter
511+
localVarRequestOptions.FormParameters.Add("status", Org.OpenAPITools.Client.ClientUtils.ParameterToString(status)); // form parameter
518512
if (marker != null)
519513
{
520514
localVarRequestOptions.FormParameters.Add("marker", Org.OpenAPITools.Client.ClientUtils.Serialize(marker)); // form parameter
@@ -572,12 +566,6 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
572566
/// <returns>Task of ApiResponse</returns>
573567
public async System.Threading.Tasks.Task<Org.OpenAPITools.Client.ApiResponse<Object>> MultipartMixedWithHttpInfoAsync(MultipartMixedStatus status, System.IO.Stream file, MultipartMixedRequestMarker marker = default(MultipartMixedRequestMarker), List<MultipartMixedStatus> statusArray = default(List<MultipartMixedStatus>), int operationIndex = 0, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
574568
{
575-
// verify the required parameter 'status' is set
576-
if (status == null)
577-
{
578-
throw new Org.OpenAPITools.Client.ApiException(400, "Missing required parameter 'status' when calling MultipartApi->MultipartMixed");
579-
}
580-
581569
// verify the required parameter 'file' is set
582570
if (file == null)
583571
{
@@ -607,7 +595,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
607595
localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept);
608596
}
609597

610-
localVarRequestOptions.FormParameters.Add("status", Org.OpenAPITools.Client.ClientUtils.Serialize(status)); // form parameter
598+
localVarRequestOptions.FormParameters.Add("status", Org.OpenAPITools.Client.ClientUtils.ParameterToString(status)); // form parameter
611599
if (marker != null)
612600
{
613601
localVarRequestOptions.FormParameters.Add("marker", Org.OpenAPITools.Client.ClientUtils.Serialize(marker)); // form parameter

samples/server/petstore/aspnetcore-3.0/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ build.sh
55
src/Org.OpenAPITools/.gitignore
66
src/Org.OpenAPITools/Attributes/ValidateModelStateAttribute.cs
77
src/Org.OpenAPITools/Authentication/ApiAuthentication.cs
8+
src/Org.OpenAPITools/Controllers/DefaultApi.cs
89
src/Org.OpenAPITools/Controllers/FakeApi.cs
910
src/Org.OpenAPITools/Controllers/PetApi.cs
1011
src/Org.OpenAPITools/Controllers/StoreApi.cs
@@ -22,6 +23,7 @@ src/Org.OpenAPITools/Models/Dog.cs
2223
src/Org.OpenAPITools/Models/Order.cs
2324
src/Org.OpenAPITools/Models/Pet.cs
2425
src/Org.OpenAPITools/Models/Tag.cs
26+
src/Org.OpenAPITools/Models/TestEnum.cs
2527
src/Org.OpenAPITools/Models/User.cs
2628
src/Org.OpenAPITools/OpenApi/TypeExtensions.cs
2729
src/Org.OpenAPITools/Program.cs
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.ComponentModel.DataAnnotations;
14+
using Microsoft.AspNetCore.Authorization;
15+
using Microsoft.AspNetCore.Mvc;
16+
using Microsoft.AspNetCore.Http;
17+
using Swashbuckle.AspNetCore.Annotations;
18+
using Swashbuckle.AspNetCore.SwaggerGen;
19+
using Newtonsoft.Json;
20+
using Org.OpenAPITools.Attributes;
21+
using Org.OpenAPITools.Models;
22+
23+
namespace Org.OpenAPITools.Controllers
24+
{
25+
/// <summary>
26+
///
27+
/// </summary>
28+
[ApiController]
29+
public class DefaultApiController : ControllerBase
30+
{
31+
/// <summary>
32+
/// Test API
33+
/// </summary>
34+
/// <param name="testQuery"></param>
35+
/// <response code="200">OK</response>
36+
[HttpGet]
37+
[Route("/v2/test")]
38+
[ValidateModelState]
39+
[SwaggerOperation("TestGet")]
40+
public virtual IActionResult TestGet([FromQuery (Name = "testQuery")]TestEnum? testQuery)
41+
{
42+
43+
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
44+
// return StatusCode(200);
45+
46+
throw new NotImplementedException();
47+
}
48+
}
49+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
using System;
12+
using System.Linq;
13+
using System.Text;
14+
using System.Collections.Generic;
15+
using System.ComponentModel;
16+
using System.ComponentModel.DataAnnotations;
17+
using System.Runtime.Serialization;
18+
using Newtonsoft.Json;
19+
using Org.OpenAPITools.Converters;
20+
21+
namespace Org.OpenAPITools.Models
22+
{
23+
/// <summary>
24+
/// Gets or Sets TestEnum
25+
/// </summary>
26+
[TypeConverter(typeof(CustomEnumConverter<TestEnum>))]
27+
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
28+
public enum TestEnum
29+
{
30+
31+
/// <summary>
32+
/// Enum AEnum for A
33+
/// </summary>
34+
[EnumMember(Value = "A")]
35+
AEnum = 1,
36+
37+
/// <summary>
38+
/// Enum BEnum for B
39+
/// </summary>
40+
[EnumMember(Value = "B")]
41+
BEnum = 2
42+
}
43+
}

samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/wwwroot/openapi-original.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@
2727
"name" : "user"
2828
} ],
2929
"paths" : {
30+
"/test" : {
31+
"get" : {
32+
"parameters" : [ {
33+
"explode" : true,
34+
"in" : "query",
35+
"name" : "testQuery",
36+
"required" : false,
37+
"schema" : {
38+
"$ref" : "#/components/schemas/TestEnum"
39+
},
40+
"style" : "form"
41+
} ],
42+
"responses" : {
43+
"200" : {
44+
"description" : "OK"
45+
}
46+
},
47+
"summary" : "Test API"
48+
}
49+
},
3050
"/pet" : {
3151
"post" : {
3252
"description" : "",
@@ -828,6 +848,10 @@
828848
}
829849
},
830850
"schemas" : {
851+
"TestEnum" : {
852+
"enum" : [ "A", "B" ],
853+
"type" : "string"
854+
},
831855
"Order" : {
832856
"description" : "An order for a pets from the pet store",
833857
"example" : {

0 commit comments

Comments
 (0)