Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 11 commits into from
Sep 20, 2023
23 changes: 23 additions & 0 deletions modules/openapi-generator/src/main/resources/csharp/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -27,9 +28,11 @@

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 {
Expand Down Expand Up @@ -62,5 +65,8 @@ public void deepObject() throws IOException {
"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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using Org.OpenAPITools.Client;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Model;
// uncomment below to import models
//using Org.OpenAPITools.Model;

Expand Down Expand Up @@ -96,10 +97,9 @@ public void TestEchoBodyFreeFormObjectResponseStringTest()
[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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@

if (queryObject != null)
{
if (queryObject.Id != null)

Check warning on line 1067 in samples/client/echo_api/csharp-restsharp/src/Org.OpenAPITools/Api/QueryApi.cs

View workflow job for this annotation

GitHub Actions / Build .Net clients (samples/client/echo_api/csharp-restsharp/)

The result of the expression is always 'true' since a value of type 'long' is never equal to 'null' of type 'long?'

Check warning on line 1067 in samples/client/echo_api/csharp-restsharp/src/Org.OpenAPITools/Api/QueryApi.cs

View workflow job for this annotation

GitHub Actions / Build .Net clients (samples/client/echo_api/csharp-restsharp/)

The result of the expression is always 'true' since a value of type 'long' is never equal to 'null' of type 'long?'
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "queryObject[id]", queryObject.Id));
}
Expand Down Expand Up @@ -1157,7 +1157,30 @@

if (queryObject != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query_object", queryObject));
if (queryObject.Id != null)

Check warning on line 1160 in samples/client/echo_api/csharp-restsharp/src/Org.OpenAPITools/Api/QueryApi.cs

View workflow job for this annotation

GitHub Actions / Build .Net clients (samples/client/echo_api/csharp-restsharp/)

The result of the expression is always 'true' since a value of type 'long' is never equal to 'null' of type 'long?'

Check warning on line 1160 in samples/client/echo_api/csharp-restsharp/src/Org.OpenAPITools/Api/QueryApi.cs

View workflow job for this annotation

GitHub Actions / Build .Net clients (samples/client/echo_api/csharp-restsharp/)

The result of the expression is always 'true' since a value of type 'long' is never equal to 'null' of type 'long?'
{
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";
Expand Down Expand Up @@ -1294,7 +1317,6 @@

if (queryObject != null)
{
localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query_object", queryObject));
}

localVarRequestOptions.Operation = "QueryApi.TestQueryStyleDeepObjectExplodeTrueObjectAllOf";
Expand Down
Loading