Skip to content

Commit 8874df4

Browse files
Codegen parameter for query json serialization (#21718)
* Add endpoints with query parameters that require Json-serialization * Add property for query json-serialization * Update samples * Adjust indentation for specification
1 parent 6ff9e67 commit 8874df4

File tree

75 files changed

+4461
-2
lines changed

Some content is hidden

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

75 files changed

+4461
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
4646
public boolean isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary,
4747
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword, isFreeFormObject, isAnyType, isShort, isUnboundedInteger;
4848
public boolean isArray, isMap;
49+
/**
50+
* If a query parameter should be serialized as json
51+
*/
52+
public boolean queryIsJsonMimeType;
4953
/**
5054
* datatype is the generic inner parameter of a std::optional for C++, or Optional (Java)
5155
*/
@@ -265,6 +269,7 @@ public CodegenParameter copy() {
265269
output.isAnyType = this.isAnyType;
266270
output.isArray = this.isArray;
267271
output.isMap = this.isMap;
272+
output.queryIsJsonMimeType = this.queryIsJsonMimeType;
268273
output.isOptional = this.isOptional;
269274
output.isExplode = this.isExplode;
270275
output.style = this.style;
@@ -289,7 +294,7 @@ public int hashCode() {
289294
isFormStyle, isSpaceDelimited, isPipeDelimited,
290295
jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal,
291296
isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword,
292-
isFreeFormObject, isAnyType, isArray, isMap, isOptional, isFile, isEnum, isEnumRef, _enum, allowableValues,
297+
isFreeFormObject, isAnyType, isArray, isMap, queryIsJsonMimeType, isOptional, isFile, isEnum, isEnumRef, _enum, allowableValues,
293298
items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation,
294299
getMaxProperties(), getMinProperties(), isNullable, isDeprecated, required, getMaximum(),
295300
getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(),
@@ -339,6 +344,7 @@ public boolean equals(Object o) {
339344
isAnyType == that.isAnyType &&
340345
isArray == that.isArray &&
341346
isMap == that.isMap &&
347+
queryIsJsonMimeType == that.queryIsJsonMimeType &&
342348
isOptional == that.isOptional &&
343349
isFile == that.isFile &&
344350
isEnum == that.isEnum &&
@@ -479,6 +485,7 @@ public String toString() {
479485
sb.append(", isAnyType=").append(isAnyType);
480486
sb.append(", isArray=").append(isArray);
481487
sb.append(", isMap=").append(isMap);
488+
sb.append(", queryIsJsonMimeType=").append(queryIsJsonMimeType);
482489
sb.append(", isOptional=").append(isOptional);
483490
sb.append(", isFile=").append(isFile);
484491
sb.append(", isEnum=").append(isEnum);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5359,6 +5359,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
53595359
if (parameter instanceof QueryParameter || "query".equalsIgnoreCase(parameter.getIn())) {
53605360
codegenParameter.isQueryParam = true;
53615361
codegenParameter.isAllowEmptyValue = parameter.getAllowEmptyValue() != null && parameter.getAllowEmptyValue();
5362+
codegenParameter.queryIsJsonMimeType = isJsonMimeType(codegenParameter.contentType);
53625363
} else if (parameter instanceof PathParameter || "path".equalsIgnoreCase(parameter.getIn())) {
53635364
codegenParameter.required = true;
53645365
codegenParameter.isPathParam = true;

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5006,4 +5006,15 @@ public void testSingleRequestParameter_hasSingleParamTrue() {
50065006
// When & Then
50075007
assertThat(codegenOperation.getHasSingleParam()).isTrue();
50085008
}
5009+
5010+
@Test
5011+
public void testQueryIsJsonMimeType() {
5012+
DefaultCodegen codegen = new DefaultCodegen();
5013+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/echo_api.yaml");
5014+
codegen.setOpenAPI(openAPI);
5015+
String path = "/query/style_jsonSerialization/object";
5016+
CodegenOperation codegenOperation = codegen.fromOperation(path, "GET", openAPI.getPaths().get(path).getGet(), null);
5017+
5018+
assertTrue(codegenOperation.queryParams.stream().allMatch(p -> p.queryIsJsonMimeType));
5019+
}
50095020
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,35 @@ paths:
466466
text/plain:
467467
schema:
468468
type: string
469+
/query/style_jsonSerialization/object:
470+
get:
471+
tags:
472+
- query
473+
summary: Test query parameter(s)
474+
description: Test query parameter(s)
475+
operationId: test/query/style_jsonSerialization/object
476+
parameters:
477+
- in: query
478+
name: json_serialized_object_ref_string_query
479+
content:
480+
application/json:
481+
schema:
482+
$ref: '#/components/schemas/Pet'
483+
- in: query
484+
name: json_serialized_object_array_ref_string_query
485+
content:
486+
application/json:
487+
schema:
488+
type: array
489+
items:
490+
$ref: '#/components/schemas/Pet'
491+
responses:
492+
'200':
493+
description: Successful operation
494+
content:
495+
text/plain:
496+
schema:
497+
type: string
469498
# body parameter tests
470499
/body/application/octetstream/binary:
471500
post:

samples/client/echo_api/csharp/restsharp/net8/EchoApi/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Class | Method | HTTP request | Description
144144
*QueryApi* | [**TestQueryStyleFormExplodeTrueArrayString**](docs/QueryApi.md#testquerystyleformexplodetruearraystring) | **GET** /query/style_form/explode_true/array_string | Test query parameter(s)
145145
*QueryApi* | [**TestQueryStyleFormExplodeTrueObject**](docs/QueryApi.md#testquerystyleformexplodetrueobject) | **GET** /query/style_form/explode_true/object | Test query parameter(s)
146146
*QueryApi* | [**TestQueryStyleFormExplodeTrueObjectAllOf**](docs/QueryApi.md#testquerystyleformexplodetrueobjectallof) | **GET** /query/style_form/explode_true/object/allOf | Test query parameter(s)
147+
*QueryApi* | [**TestQueryStyleJsonSerializationObject**](docs/QueryApi.md#testquerystylejsonserializationobject) | **GET** /query/style_jsonSerialization/object | Test query parameter(s)
147148

148149

149150
<a id="documentation-for-models"></a>

samples/client/echo_api/csharp/restsharp/net8/EchoApi/api/openapi.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,37 @@ paths:
434434
summary: Test query parameter(s)
435435
tags:
436436
- query
437+
/query/style_jsonSerialization/object:
438+
get:
439+
description: Test query parameter(s)
440+
operationId: test/query/style_jsonSerialization/object
441+
parameters:
442+
- content:
443+
application/json:
444+
schema:
445+
$ref: "#/components/schemas/Pet"
446+
in: query
447+
name: json_serialized_object_ref_string_query
448+
required: false
449+
- content:
450+
application/json:
451+
schema:
452+
items:
453+
$ref: "#/components/schemas/Pet"
454+
type: array
455+
in: query
456+
name: json_serialized_object_array_ref_string_query
457+
required: false
458+
responses:
459+
"200":
460+
content:
461+
text/plain:
462+
schema:
463+
type: string
464+
description: Successful operation
465+
summary: Test query parameter(s)
466+
tags:
467+
- query
437468
/body/application/octetstream/binary:
438469
post:
439470
description: Test body parameter(s)

samples/client/echo_api/csharp/restsharp/net8/EchoApi/docs/QueryApi.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All URIs are relative to *http://localhost:3000*
1414
| [**TestQueryStyleFormExplodeTrueArrayString**](QueryApi.md#testquerystyleformexplodetruearraystring) | **GET** /query/style_form/explode_true/array_string | Test query parameter(s) |
1515
| [**TestQueryStyleFormExplodeTrueObject**](QueryApi.md#testquerystyleformexplodetrueobject) | **GET** /query/style_form/explode_true/object | Test query parameter(s) |
1616
| [**TestQueryStyleFormExplodeTrueObjectAllOf**](QueryApi.md#testquerystyleformexplodetrueobjectallof) | **GET** /query/style_form/explode_true/object/allOf | Test query parameter(s) |
17+
| [**TestQueryStyleJsonSerializationObject**](QueryApi.md#testquerystylejsonserializationobject) | **GET** /query/style_jsonSerialization/object | Test query parameter(s) |
1718

1819
<a id="testenumrefstring"></a>
1920
# **TestEnumRefString**
@@ -935,3 +936,96 @@ No authorization required
935936

936937
[[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)
937938

939+
<a id="testquerystylejsonserializationobject"></a>
940+
# **TestQueryStyleJsonSerializationObject**
941+
> string TestQueryStyleJsonSerializationObject (Pet? jsonSerializedObjectRefStringQuery = null, List<Pet>? jsonSerializedObjectArrayRefStringQuery = null)
942+
943+
Test query parameter(s)
944+
945+
Test query parameter(s)
946+
947+
### Example
948+
```csharp
949+
using System.Collections.Generic;
950+
using System.Diagnostics;
951+
using Org.OpenAPITools.Api;
952+
using Org.OpenAPITools.Client;
953+
using Org.OpenAPITools.Model;
954+
955+
namespace Example
956+
{
957+
public class TestQueryStyleJsonSerializationObjectExample
958+
{
959+
public static void Main()
960+
{
961+
Configuration config = new Configuration();
962+
config.BasePath = "http://localhost:3000";
963+
var apiInstance = new QueryApi(config);
964+
var jsonSerializedObjectRefStringQuery = new Pet?(); // Pet? | (optional)
965+
var jsonSerializedObjectArrayRefStringQuery = new List<Pet>?(); // List<Pet>? | (optional)
966+
967+
try
968+
{
969+
// Test query parameter(s)
970+
string result = apiInstance.TestQueryStyleJsonSerializationObject(jsonSerializedObjectRefStringQuery, jsonSerializedObjectArrayRefStringQuery);
971+
Debug.WriteLine(result);
972+
}
973+
catch (ApiException e)
974+
{
975+
Debug.Print("Exception when calling QueryApi.TestQueryStyleJsonSerializationObject: " + e.Message);
976+
Debug.Print("Status Code: " + e.ErrorCode);
977+
Debug.Print(e.StackTrace);
978+
}
979+
}
980+
}
981+
}
982+
```
983+
984+
#### Using the TestQueryStyleJsonSerializationObjectWithHttpInfo variant
985+
This returns an ApiResponse object which contains the response data, status code and headers.
986+
987+
```csharp
988+
try
989+
{
990+
// Test query parameter(s)
991+
ApiResponse<string> response = apiInstance.TestQueryStyleJsonSerializationObjectWithHttpInfo(jsonSerializedObjectRefStringQuery, jsonSerializedObjectArrayRefStringQuery);
992+
Debug.Write("Status Code: " + response.StatusCode);
993+
Debug.Write("Response Headers: " + response.Headers);
994+
Debug.Write("Response Body: " + response.Data);
995+
}
996+
catch (ApiException e)
997+
{
998+
Debug.Print("Exception when calling QueryApi.TestQueryStyleJsonSerializationObjectWithHttpInfo: " + e.Message);
999+
Debug.Print("Status Code: " + e.ErrorCode);
1000+
Debug.Print(e.StackTrace);
1001+
}
1002+
```
1003+
1004+
### Parameters
1005+
1006+
| Name | Type | Description | Notes |
1007+
|------|------|-------------|-------|
1008+
| **jsonSerializedObjectRefStringQuery** | [**Pet?**](Pet?.md) | | [optional] |
1009+
| **jsonSerializedObjectArrayRefStringQuery** | [**List&lt;Pet&gt;?**](Pet.md) | | [optional] |
1010+
1011+
### Return type
1012+
1013+
**string**
1014+
1015+
### Authorization
1016+
1017+
No authorization required
1018+
1019+
### HTTP request headers
1020+
1021+
- **Content-Type**: Not defined
1022+
- **Accept**: text/plain
1023+
1024+
1025+
### HTTP response details
1026+
| Status code | Description | Response headers |
1027+
|-------------|-------------|------------------|
1028+
| **200** | Successful operation | - |
1029+
1030+
[[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)
1031+

0 commit comments

Comments
 (0)