Skip to content

Commit 755b2ef

Browse files
authored
[csharp][generichost] Support response ranges (#19256)
* support response ranges * revert unintended change * update try deserialize methods
1 parent 2958107 commit 755b2ef

File tree

38 files changed

+457
-1
lines changed

38 files changed

+457
-1
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,26 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
997997
case "511":
998998
postProcessResponseCode(response, "NetworkAuthenticationRequired", httpStatusesWithReturn);
999999
break;
1000+
case "1xx":
1001+
response.vendorExtensions.put("x-http-status-range", 1);
1002+
postProcessResponseCode(response, "HttpStatusCode1XX", httpStatusesWithReturn);
1003+
break;
1004+
case "2xx":
1005+
response.vendorExtensions.put("x-http-status-range", 2);
1006+
postProcessResponseCode(response, "HttpStatusCode2XX", httpStatusesWithReturn);
1007+
break;
1008+
case "3xx":
1009+
response.vendorExtensions.put("x-http-status-range", 3);
1010+
postProcessResponseCode(response, "HttpStatusCode3XX", httpStatusesWithReturn);
1011+
break;
1012+
case "4xx":
1013+
response.vendorExtensions.put("x-http-status-range", 4);
1014+
postProcessResponseCode(response, "HttpStatusCode4XX", httpStatusesWithReturn);
1015+
break;
1016+
case "5xx":
1017+
response.vendorExtensions.put("x-http-status-range", 5);
1018+
postProcessResponseCode(response, "HttpStatusCode5XX", httpStatusesWithReturn);
1019+
break;
10001020
default:
10011021
postProcessResponseCode(response, "CustomHttpStatusCode" + code, httpStatusesWithReturn);
10021022
}

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,19 @@ namespace {{packageName}}.{{apiPackage}}
725725
/// Returns true if the response is {{code}} {{vendorExtensions.x-http-status}}
726726
/// </summary>
727727
/// <returns></returns>
728+
{{#vendorExtensions.x-http-status-range}}
729+
public bool Is{{vendorExtensions.x-http-status}}
730+
{
731+
get
732+
{
733+
int statusCode = (int)StatusCode;
734+
return {{vendorExtensions.x-http-status-range}}00 >= statusCode && {{vendorExtensions.x-http-status-range}}99 <= statusCode;
735+
}
736+
}
737+
{{/vendorExtensions.x-http-status-range}}
738+
{{^vendorExtensions.x-http-status-range}}
728739
public bool Is{{vendorExtensions.x-http-status}} => {{code}} == (int)StatusCode;
740+
{{/vendorExtensions.x-http-status-range}}
729741
{{/vendorExtensions.x-http-status-is-default}}
730742
{{#dataType}}
731743

@@ -756,7 +768,7 @@ namespace {{packageName}}.{{apiPackage}}
756768
result = {{vendorExtensions.x-http-status}}();
757769
} catch (Exception e)
758770
{
759-
OnDeserializationErrorDefaultImplementation(e, (HttpStatusCode){{code}});
771+
OnDeserializationErrorDefaultImplementation(e, (HttpStatusCode){{#vendorExtensions.x-http-status-range}}{{.}}{{/vendorExtensions.x-http-status-range}}{{^vendorExtensions.x-http-status-range}}{{code}}{{/vendorExtensions.x-http-status-range}});
760772
}
761773

762774
return result != null;

modules/openapi-generator/src/test/resources/3_0/csharp/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ paths:
120120
- sold
121121
default: available
122122
responses:
123+
'2XX':
124+
description: Anything within 200-299
123125
'200':
124126
description: successful operation
125127
content:
@@ -135,6 +137,8 @@ paths:
135137
$ref: '#/components/schemas/Pet'
136138
'400':
137139
description: Invalid status value
140+
'4XX':
141+
description: Anything within 400-499
138142
security:
139143
- http_signature_test: []
140144
- petstore_auth:

samples/client/petstore/csharp/generichost/net4.7/FormModels/api/openapi.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ paths:
133133
type: array
134134
style: form
135135
responses:
136+
"2XX":
137+
description: Anything within 200-299
136138
"200":
137139
content:
138140
application/xml:
@@ -148,6 +150,8 @@ paths:
148150
description: successful operation
149151
"400":
150152
description: Invalid status value
153+
"4XX":
154+
description: Anything within 400-499
151155
security:
152156
- http_signature_test: []
153157
- petstore_auth:

samples/client/petstore/csharp/generichost/net4.7/FormModels/docs/apis/PetApi.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ catch (ApiException e)
284284
|-------------|-------------|------------------|
285285
| **200** | successful operation | - |
286286
| **400** | Invalid status value | - |
287+
| **2XX** | Anything within 200-299 | - |
288+
| **4XX** | Anything within 400-499 | - |
287289

288290
[[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)
289291

samples/client/petstore/csharp/generichost/net4.7/FormModels/src/Org.OpenAPITools/Api/PetApi.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,18 @@ public interface IFindPetsByStatusApiResponse : Org.OpenAPITools.Client.IApiResp
297297
/// </summary>
298298
/// <returns></returns>
299299
bool IsBadRequest { get; }
300+
301+
/// <summary>
302+
/// Returns true if the response is 2XX HttpStatusCode2XX
303+
/// </summary>
304+
/// <returns></returns>
305+
bool IsHttpStatusCode2XX { get; }
306+
307+
/// <summary>
308+
/// Returns true if the response is 4XX HttpStatusCode4XX
309+
/// </summary>
310+
/// <returns></returns>
311+
bool IsHttpStatusCode4XX { get; }
300312
}
301313

302314
/// <summary>
@@ -1308,6 +1320,32 @@ public bool TryOk(out List<Pet> result)
13081320
/// <returns></returns>
13091321
public bool IsBadRequest => 400 == (int)StatusCode;
13101322

1323+
/// <summary>
1324+
/// Returns true if the response is 2XX HttpStatusCode2XX
1325+
/// </summary>
1326+
/// <returns></returns>
1327+
public bool IsHttpStatusCode2XX
1328+
{
1329+
get
1330+
{
1331+
int statusCode = (int)StatusCode;
1332+
return 200 >= statusCode && 299 <= statusCode;
1333+
}
1334+
}
1335+
1336+
/// <summary>
1337+
/// Returns true if the response is 4XX HttpStatusCode4XX
1338+
/// </summary>
1339+
/// <returns></returns>
1340+
public bool IsHttpStatusCode4XX
1341+
{
1342+
get
1343+
{
1344+
int statusCode = (int)StatusCode;
1345+
return 400 >= statusCode && 499 <= statusCode;
1346+
}
1347+
}
1348+
13111349
private void OnDeserializationErrorDefaultImplementation(Exception exception, HttpStatusCode httpStatusCode)
13121350
{
13131351
bool suppressDefaultLog = false;

samples/client/petstore/csharp/generichost/net4.7/Petstore/api/openapi.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ paths:
138138
type: array
139139
style: form
140140
responses:
141+
"2XX":
142+
description: Anything within 200-299
141143
"200":
142144
content:
143145
application/xml:
@@ -153,6 +155,8 @@ paths:
153155
description: successful operation
154156
"400":
155157
description: Invalid status value
158+
"4XX":
159+
description: Anything within 400-499
156160
security:
157161
- http_signature_test: []
158162
- petstore_auth:

samples/client/petstore/csharp/generichost/net4.7/Petstore/docs/apis/PetApi.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ catch (ApiException e)
284284
|-------------|-------------|------------------|
285285
| **200** | successful operation | - |
286286
| **400** | Invalid status value | - |
287+
| **2XX** | Anything within 200-299 | - |
288+
| **4XX** | Anything within 400-499 | - |
287289

288290
[[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)
289291

samples/client/petstore/csharp/generichost/net4.7/Petstore/src/Org.OpenAPITools/Api/PetApi.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,18 @@ public interface IFindPetsByStatusApiResponse : Org.OpenAPITools.Client.IApiResp
297297
/// </summary>
298298
/// <returns></returns>
299299
bool IsBadRequest { get; }
300+
301+
/// <summary>
302+
/// Returns true if the response is 2XX HttpStatusCode2XX
303+
/// </summary>
304+
/// <returns></returns>
305+
bool IsHttpStatusCode2XX { get; }
306+
307+
/// <summary>
308+
/// Returns true if the response is 4XX HttpStatusCode4XX
309+
/// </summary>
310+
/// <returns></returns>
311+
bool IsHttpStatusCode4XX { get; }
300312
}
301313

302314
/// <summary>
@@ -1308,6 +1320,32 @@ public bool TryOk(out List<Pet> result)
13081320
/// <returns></returns>
13091321
public bool IsBadRequest => 400 == (int)StatusCode;
13101322

1323+
/// <summary>
1324+
/// Returns true if the response is 2XX HttpStatusCode2XX
1325+
/// </summary>
1326+
/// <returns></returns>
1327+
public bool IsHttpStatusCode2XX
1328+
{
1329+
get
1330+
{
1331+
int statusCode = (int)StatusCode;
1332+
return 200 >= statusCode && 299 <= statusCode;
1333+
}
1334+
}
1335+
1336+
/// <summary>
1337+
/// Returns true if the response is 4XX HttpStatusCode4XX
1338+
/// </summary>
1339+
/// <returns></returns>
1340+
public bool IsHttpStatusCode4XX
1341+
{
1342+
get
1343+
{
1344+
int statusCode = (int)StatusCode;
1345+
return 400 >= statusCode && 499 <= statusCode;
1346+
}
1347+
}
1348+
13111349
private void OnDeserializationErrorDefaultImplementation(Exception exception, HttpStatusCode httpStatusCode)
13121350
{
13131351
bool suppressDefaultLog = false;

samples/client/petstore/csharp/generichost/net4.8/FormModels/api/openapi.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ paths:
133133
type: array
134134
style: form
135135
responses:
136+
"2XX":
137+
description: Anything within 200-299
136138
"200":
137139
content:
138140
application/xml:
@@ -148,6 +150,8 @@ paths:
148150
description: successful operation
149151
"400":
150152
description: Invalid status value
153+
"4XX":
154+
description: Anything within 400-499
151155
security:
152156
- http_signature_test: []
153157
- petstore_auth:

0 commit comments

Comments
 (0)