[dotnetcore] Implement QueryParameter deepObject style is missing for… (#15945)

* [dotnetcore] Implement QueryParameter deepObject style is missing for async function

* Add some csharp-restsharp tests

* add and fix tests

* restore missing files

* remove test with datetime problem

---------

Co-authored-by: Oliver Krug <Oliver.Krug@ptvgroup.com>
This commit is contained in:
MachineUserPTV
2023-09-20 08:57:33 +02:00
committed by GitHub
parent a2f6b8eae5
commit f530496f21
6 changed files with 207 additions and 6 deletions

View File

@@ -586,12 +586,35 @@ namespace {{packageName}}.{{apiPackage}}
{{/pathParams}}
{{#queryParams}}
{{#required}}
{{#isDeepObject}}
{{#items.vars}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{collectionFormat}}", "{{baseName}}", {{paramName}}.{{name}}));
{{/items.vars}}
{{^items}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("deepObject", "{{baseName}}", {{paramName}}));
{{/items}}
{{/isDeepObject}}
{{^isDeepObject}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{collectionFormat}}", "{{baseName}}", {{paramName}}));
{{/isDeepObject}}
{{/required}}
{{^required}}
if ({{paramName}} != null)
{
{{#isDeepObject}}
{{#items.vars}}
if ({{paramName}}.{{name}} != null)
{
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{collectionFormat}}", "{{paramName}}[{{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}]", {{paramName}}.{{name}}));
}
{{/items.vars}}
{{^items}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("deepObject", "{{baseName}}", {{paramName}}));
{{/items}}
{{/isDeepObject}}
{{^isDeepObject}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{collectionFormat}}", "{{baseName}}", {{paramName}}));
{{/isDeepObject}}
}
{{/required}}
{{/queryParams}}

View File

@@ -19,6 +19,7 @@ package org.openapitools.codegen.csharpnetcore;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.DefaultGenerator;
@@ -27,9 +28,11 @@ import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.junit.Assert.assertEquals;
import static org.openapitools.codegen.TestUtils.assertFileContains;
public class CSharpClientDeepObjectTest {
@@ -62,5 +65,8 @@ public class CSharpClientDeepObjectTest {
"options[id]", "options[name]", "options[category]", "options[tags]",
"options[status]", "options[photoUrls]",
"inputOptions[a]", "inputOptions[b]", "inputOptions[c]");
String content = new String(Files.readAllBytes(Paths.get(outputPath + "/src/Org.OpenAPITools/Api/DefaultApi.cs")), StandardCharsets.UTF_8);
int counter = StringUtils.countMatches(content,"inputOptions[a]");
assertEquals(2, counter);
}
}

View File

@@ -19,6 +19,7 @@ using Xunit;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Model;
// uncomment below to import models
//using Org.OpenAPITools.Model;
@@ -96,10 +97,9 @@ namespace Org.OpenAPITools.Test.Api
[Fact]
public void TestEchoBodyPetTest()
{
// TODO uncomment below to test the method and replace null with proper value
//Pet? pet = null;
//var response = instance.TestEchoBodyPet(pet);
//Assert.IsType<Pet>(response);
Pet? pet = new Pet(123, "cat", new Category() { Id = 12, Name = "Test" }, new List<string>(){"http://google.com"},null, null);
var response = instance.TestEchoBodyPet(pet);
Assert.IsType<Pet>(response);
}
/// <summary>

View File

@@ -0,0 +1,68 @@
using Newtonsoft.Json.Linq;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Xunit;
namespace Org.OpenAPITools.Test
{
public class CustomTest
{
private QueryApi api = new QueryApi();
private BodyApi bodyApi = new BodyApi();
[Fact]
public void TestEchoBodyPet()
{
Pet queryObject = new Pet(12345L, "Hello World", new Category(987L, "new category"), new List<string> { "http://a.com", "http://b.com" });
Pet p = bodyApi.TestEchoBodyPet(queryObject);
Assert.NotNull(p);
Assert.Equal("Hello World", p.Name);
Assert.Equal(12345L, p.Id);
// response is empty body
Pet p2 = bodyApi.TestEchoBodyPet(null);
Assert.Null(p2);
}
/**
* Test query parameter(s)
* <p>
* Test query parameter(s)
*
* @throws ApiException if the Api call fails
*/
[Fact]
public void TestQueryStyleFormExplodeTrueObjectTest()
{
Pet queryObject = new Pet(12345L, "Hello World", new Category(987L, "new category"), new List<string> { "http://a.com", "http://b.com" });
String response = api.TestQueryStyleFormExplodeTrueObject(queryObject);
EchoServerResponseParser p = new EchoServerResponseParser(response);
Assert.Equal("/query/style_form/explode_true/object?query_object=class%20Pet%20%7b%0a%20%20Id%3a%2012345%0a%20%20Name%3a%20Hello%20World%0a%20%20Category%3a%20class%20Category%20%7b%0a%20%20Id%3a%20987%0a%20%20Name%3a%20new%20category%0a%7d%0a%0a%20%20PhotoUrls%3a%20System.Collections.Generic.List%601%5bSystem.String%5d%0a%20%20Tags%3a%20%0a%20%20Status%3a%20%0a%7d%0a", p.path);
}
[Fact]
public void testQueryStyleDeepObjectExplodeTrueObjectTest()
{
Pet queryObject = new Pet(12345L, "Hello World", new Category(987L, "new category"), new List<string> { "http://a.com", "http://b.com" });
String response = api.TestQueryStyleDeepObjectExplodeTrueObject(queryObject);
EchoServerResponseParser p = new EchoServerResponseParser(response);
Assert.Equal("/query/style_deepObject/explode_true/object?queryObject%5bid%5d=12345&queryObject%5bname%5d=Hello%20World&queryObject%5bcategory%5d=class%20Category%20%7b%0a%20%20Id%3a%20987%0a%20%20Name%3a%20new%20category%0a%7d%0a&queryObject%5bphotoUrls%5d=http%3a%2f%2fa.com%2chttp%3a%2f%2fb.com", p.path);
}
[Fact]
public void testQueryStyleDeepObjectExplodeTrueObjectAsyncTest()
{
Pet queryObject = new Pet(12345L, "Hello World", new Category(987L, "new category"), new List<string> { "http://a.com", "http://b.com" });
Task<String> responseTask = api.TestQueryStyleDeepObjectExplodeTrueObjectAsync(queryObject);
EchoServerResponseParser p = new EchoServerResponseParser(responseTask.Result);
Assert.Equal("/query/style_deepObject/explode_true/object?queryObject%5bid%5d=12345&queryObject%5bname%5d=Hello%20World&queryObject%5bcategory%5d=class%20Category%20%7b%0a%20%20Id%3a%20987%0a%20%20Name%3a%20new%20category%0a%7d%0a&queryObject%5bphotoUrls%5d=http%3a%2f%2fa.com%2chttp%3a%2f%2fb.com", p.path);
}
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace Org.OpenAPITools.Test
{
public class EchoServerResponseParser
{
public String method; // e.g. GET
public String path; // e.g. /query/style_form/explode_true/object?id=12345
public String protocol; // e.g. HTTP/1.1
public Dictionary<String, String> headers = new Dictionary<String, String>();
public String body; // e.g. <html><head></head><body>Hello World!</body></html>
public EchoServerResponseParser(String response)
{
if (response == null)
{
throw new SystemException("Echo server response cannot be null");
}
String[] lines = Regex.Split(response, "\r\n|\r|\n");
bool firstLine = true;
bool bodyStart = false;
StringBuilder bodyBuilder = new StringBuilder();
foreach (String line in lines)
{
if (firstLine)
{
String[] items = line.Split(" ");
this.method = items[0];
this.path = items[1];
this.protocol = items[2];
firstLine = false;
continue;
}
if (bodyStart)
{
bodyBuilder.Append(line);
bodyBuilder.Append("\n");
}
if (String.IsNullOrEmpty(line))
{
bodyStart = true;
continue;
}
// store the header key-value pair in headers
String[] keyValue = line.Split(": ");
if (keyValue.Length == 2)
{ // skip blank line, non key-value pair
this.headers.Add(keyValue[0], keyValue[1]);
}
}
body = bodyBuilder.ToString();
}
}
}

View File

@@ -1157,7 +1157,30 @@ namespace Org.OpenAPITools.Api
if (queryObject != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query_object", queryObject));
if (queryObject.Id != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "queryObject[id]", queryObject.Id));
}
if (queryObject.Name != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "queryObject[name]", queryObject.Name));
}
if (queryObject.Category != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "queryObject[category]", queryObject.Category));
}
if (queryObject.PhotoUrls != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "queryObject[photoUrls]", queryObject.PhotoUrls));
}
if (queryObject.Tags != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "queryObject[tags]", queryObject.Tags));
}
if (queryObject.Status != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "queryObject[status]", queryObject.Status));
}
}
localVarRequestOptions.Operation = "QueryApi.TestQueryStyleDeepObjectExplodeTrueObject";
@@ -1294,7 +1317,6 @@ namespace Org.OpenAPITools.Api
if (queryObject != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query_object", queryObject));
}
localVarRequestOptions.Operation = "QueryApi.TestQueryStyleDeepObjectExplodeTrueObjectAllOf";