Skip to content

Commit 2400abb

Browse files
authored
Prioritize wildcard over json over first entry for accept content-type (#5198)
1 parent 4b4a35d commit 2400abb

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/NSwag.CodeGeneration.Tests/CodeGenerationTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,69 @@ public void When_using_MultipleClientsFromOperationId_then_ensure_that_underscor
306306
Assert.Equal(expectedClientName, clientName);
307307
}
308308

309+
[Fact]
310+
public void When_Success_Response_contains_multiple_content_types_prioritizes_wildcard()
311+
{
312+
// Arrange
313+
var document = CreateDocument();
314+
var operation = document.Paths["/Person"][OpenApiOperationMethod.Get];
315+
316+
operation.Responses["200"].Content.Clear();
317+
318+
operation.Responses["200"].Content.Add("application/xml", new OpenApiMediaType
319+
{
320+
Schema = new JsonSchema { Type = JsonObjectType.Object }
321+
});
322+
323+
operation.Responses["200"].Content.Add("application/json", new OpenApiMediaType
324+
{
325+
Schema = new JsonSchema { Type = JsonObjectType.Object }
326+
});
327+
328+
operation.Responses["200"].Content.Add("*/*", new OpenApiMediaType
329+
{
330+
Schema = new JsonSchema { Type = JsonObjectType.Object }
331+
});
332+
333+
// Act
334+
var settings = new CSharpClientGeneratorSettings();
335+
settings.CSharpGeneratorSettings.JsonLibrary = NJsonSchema.CodeGeneration.CSharp.CSharpJsonLibrary.SystemTextJson;
336+
var generator = new CSharpClientGenerator(document, settings);
337+
var code = generator.GenerateFile();
338+
339+
// Assert
340+
Assert.Contains("Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse(\"*/*\"));", code);
341+
}
342+
343+
[Fact]
344+
public void When_Success_Response_contains_multiple_content_types_prioritizes_json()
345+
{
346+
// Arrange
347+
var document = CreateDocument();
348+
var operation = document.Paths["/Person"][OpenApiOperationMethod.Get];
349+
350+
operation.Responses["200"].Content.Clear();
351+
352+
operation.Responses["200"].Content.Add("application/xml", new OpenApiMediaType
353+
{
354+
Schema = new JsonSchema { Type = JsonObjectType.Object }
355+
});
356+
357+
operation.Responses["200"].Content.Add("application/json", new OpenApiMediaType
358+
{
359+
Schema = new JsonSchema { Type = JsonObjectType.Object }
360+
});
361+
362+
// Act
363+
var settings = new CSharpClientGeneratorSettings();
364+
settings.CSharpGeneratorSettings.JsonLibrary = NJsonSchema.CodeGeneration.CSharp.CSharpJsonLibrary.SystemTextJson;
365+
var generator = new CSharpClientGenerator(document, settings);
366+
var code = generator.GenerateFile();
367+
368+
// Assert
369+
Assert.Contains("Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse(\"application/json\"));", code);
370+
}
371+
309372
private static OpenApiDocument CreateDocument()
310373
{
311374
var document = new OpenApiDocument();

src/NSwag.CodeGeneration/Models/ResponseModelBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public bool IsSuccess
124124
public IDictionary<string, object> ExtensionData => _response.ExtensionData;
125125

126126
/// <summary>Gets the produced mime type of this response if available.</summary>
127-
public string Produces => _response.Content.Keys.FirstOrDefault();
127+
public string Produces =>
128+
_response.Content.Keys.FirstOrDefault(k => k == "*/*") ??
129+
_response.Content.Keys.FirstOrDefault(k => k == "application/json") ??
130+
_response.Content.Keys.FirstOrDefault();
128131
}
129132
}

0 commit comments

Comments
 (0)