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
63 changes: 63 additions & 0 deletions src/NSwag.CodeGeneration.Tests/CodeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,69 @@ public void When_using_MultipleClientsFromOperationId_then_ensure_that_underscor
Assert.Equal(expectedClientName, clientName);
}

[Fact]
public void When_Success_Response_contains_multiple_content_types_prioritizes_wildcard()
{
// Arrange
var document = CreateDocument();
var operation = document.Paths["/Person"][OpenApiOperationMethod.Get];

operation.Responses["200"].Content.Clear();

operation.Responses["200"].Content.Add("application/xml", new OpenApiMediaType
{
Schema = new JsonSchema { Type = JsonObjectType.Object }
});

operation.Responses["200"].Content.Add("application/json", new OpenApiMediaType
{
Schema = new JsonSchema { Type = JsonObjectType.Object }
});

operation.Responses["200"].Content.Add("*/*", new OpenApiMediaType
{
Schema = new JsonSchema { Type = JsonObjectType.Object }
});

// Act
var settings = new CSharpClientGeneratorSettings();
settings.CSharpGeneratorSettings.JsonLibrary = NJsonSchema.CodeGeneration.CSharp.CSharpJsonLibrary.SystemTextJson;
var generator = new CSharpClientGenerator(document, settings);
var code = generator.GenerateFile();

// Assert
Assert.Contains("Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse(\"*/*\"));", code);
}

[Fact]
public void When_Success_Response_contains_multiple_content_types_prioritizes_json()
{
// Arrange
var document = CreateDocument();
var operation = document.Paths["/Person"][OpenApiOperationMethod.Get];

operation.Responses["200"].Content.Clear();

operation.Responses["200"].Content.Add("application/xml", new OpenApiMediaType
{
Schema = new JsonSchema { Type = JsonObjectType.Object }
});

operation.Responses["200"].Content.Add("application/json", new OpenApiMediaType
{
Schema = new JsonSchema { Type = JsonObjectType.Object }
});

// Act
var settings = new CSharpClientGeneratorSettings();
settings.CSharpGeneratorSettings.JsonLibrary = NJsonSchema.CodeGeneration.CSharp.CSharpJsonLibrary.SystemTextJson;
var generator = new CSharpClientGenerator(document, settings);
var code = generator.GenerateFile();

// Assert
Assert.Contains("Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse(\"application/json\"));", code);
}

private static OpenApiDocument CreateDocument()
{
var document = new OpenApiDocument();
Expand Down
5 changes: 4 additions & 1 deletion src/NSwag.CodeGeneration/Models/ResponseModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public bool IsSuccess
public IDictionary<string, object> ExtensionData => _response.ExtensionData;

/// <summary>Gets the produced mime type of this response if available.</summary>
public string Produces => _response.Content.Keys.FirstOrDefault();
public string Produces =>
_response.Content.Keys.FirstOrDefault(k => k == "*/*") ??
_response.Content.Keys.FirstOrDefault(k => k == "application/json") ??
_response.Content.Keys.FirstOrDefault();
}
}
Loading