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

fix: v2 request body content null propagation #2042

Merged
merged 2 commits into from
Jan 6, 2025
Merged
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
24 changes: 9 additions & 15 deletions src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,18 @@ public void SerializeAsV2(IOpenApiWriter writer)
List<OpenApiParameter> parameters;
if (Parameters == null)
{
parameters = new();
parameters = [];
}
else
{
parameters = new(Parameters);
parameters = [.. Parameters];
}

if (RequestBody != null)
{
// consumes
var consumes = RequestBody.Content.Keys.Distinct().ToList();
if (consumes.Any())
var consumes = new HashSet<string>(RequestBody.Content?.Keys.Distinct(StringComparer.OrdinalIgnoreCase) ?? [], StringComparer.OrdinalIgnoreCase);
Copy link
Member

Choose a reason for hiding this comment

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

👍🏼

if (consumes.Count > 0)
{
// This is form data. We need to split the request body into multiple parameters.
if (consumes.Contains("application/x-www-form-urlencoded") ||
Expand All @@ -261,19 +261,13 @@ public void SerializeAsV2(IOpenApiWriter writer)
parameters.Add(RequestBody.ConvertToBodyParameter());
}
}
else if (RequestBody.Reference != null)
else if (RequestBody.Reference != null && RequestBody.Reference.HostDocument is {} hostDocument)
{
var hostDocument = RequestBody.Reference.HostDocument;
parameters.Add(
new OpenApiParameterReference(RequestBody.Reference.Id, hostDocument));

if (hostDocument != null)
{
consumes = RequestBody.Content.Keys.Distinct().ToList();
}
}

if (consumes.Any())
if (consumes.Count > 0)
{
writer.WritePropertyName(OpenApiConstants.Consumes);
writer.WriteStartArray();
Expand All @@ -294,10 +288,10 @@ public void SerializeAsV2(IOpenApiWriter writer)
Responses
.Where(static r => r.Value.Reference is {HostDocument: not null})
.SelectMany(static r => r.Value.Content?.Keys))
.Distinct()
.ToList();
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();

if (produces.Any())
if (produces.Length > 0)
{
// produces
writer.WritePropertyName(OpenApiConstants.Produces);
Expand Down
Loading