Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ namespace {{packageName}}.Client

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -231,10 +230,7 @@ namespace {{packageName}}.Client
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,30 @@ public void testUserAgentIsNotUrlEncoded() throws IOException {
}
}

@Test
public void testSelectHeaderAcceptKeepsAllTypesJsonFirst() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
final DefaultGenerator defaultGenerator = new DefaultGenerator();
final ClientOptInput clientOptInput = new ClientOptInput();
clientOptInput.openAPI(openAPI);
CSharpClientCodegen cSharpClientCodegen = new CSharpClientCodegen();
cSharpClientCodegen.setLibrary("restsharp");
cSharpClientCodegen.setOutputDir(output.getAbsolutePath());
clientOptInput.config(cSharpClientCodegen);
defaultGenerator.opts(clientOptInput);

Map<String, File> files = defaultGenerator.generate().stream()
.collect(Collectors.toMap(File::getPath, Function.identity()));

File clientUtils = files
.get(Paths.get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Client", "ClientUtils.cs").toString());
assertNotNull(clientUtils);
assertFileContains(clientUtils.toPath(), "accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1)");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: These assertions verify sorting but not that every accepted type is returned; SelectHeaderAccept could still discard non-JSON types and pass. Assert the full string.Join expression.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/test/java/org/openapitools/codegen/csharpnetcore/CSharpClientCodegenTest.java, line 432:

<comment>These assertions verify sorting but not that every accepted type is returned; `SelectHeaderAccept` could still discard non-JSON types and pass. Assert the full `string.Join` expression.</comment>

<file context>
@@ -409,6 +409,30 @@ public void testUserAgentIsNotUrlEncoded() throws IOException {
+        File clientUtils = files
+                .get(Paths.get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Client", "ClientUtils.cs").toString());
+        assertNotNull(clientUtils);
+        assertFileContains(clientUtils.toPath(), "accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1)");
+        assertFileNotContains(clientUtils.toPath(), "return \"application/json\";");
+    }
</file context>

assertFileNotContains(clientUtils.toPath(), "return \"application/json\";");
}

@Test
public void test31specAdditionalPropertiesOfOneOf() throws IOException {
// for https://github.com/OpenAPITools/openapi-generator/pull/18772
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -216,10 +215,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -225,10 +224,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -231,10 +230,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -231,10 +230,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -231,10 +230,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -231,10 +230,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -225,10 +224,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -231,10 +230,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -231,10 +230,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Org.OpenAPITools.Api;
using Xunit;

namespace Org.OpenAPITools.Test.Client
{
public sealed class AcceptHeaderTests
{
[Fact]
public void MixedResponseTypes_SendsAllAcceptTypesJsonFirst()
{
var port = FreePort();
using (var server = new HttpListener())
{
server.Prefixes.Add("http://127.0.0.1:" + port + "/");
server.Start();
var accept = Task.Run(() =>
{
var ctx = server.GetContext();
var sent = ctx.Request.Headers["Accept"];
var body = Encoding.UTF8.GetBytes("{\"name\":\"doggie\",\"photoUrls\":[]}");
ctx.Response.ContentType = "application/json";
ctx.Response.OutputStream.Write(body, 0, body.Length);
ctx.Response.Close();
return sent;
});

new PetApi("http://127.0.0.1:" + port).GetPetById(1);

Assert.Equal("application/json,application/xml", accept.Result);
}
}

private static int FreePort()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -225,10 +224,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -225,10 +224,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -225,10 +224,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public static string SelectHeaderContentType(string[] contentTypes)

/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
/// all of them, JSON types first (joining into a string)
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
Expand All @@ -231,10 +230,7 @@ public static string SelectHeaderAccept(string[] accepts)
if (accepts.Length == 0)
return null;

if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";

return string.Join(",", accepts);
return string.Join(",", accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1));
}

/// <summary>
Expand Down
Loading
Loading