Skip to content

Commit 6e55511

Browse files
Do not add empty consumes array if RequestBody content contains no elements
1 parent b1fca56 commit 6e55511

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -237,63 +237,65 @@ public void SerializeAsV2(IOpenApiWriter writer)
237237
if (RequestBody != null)
238238
{
239239
// consumes
240-
writer.WritePropertyName(OpenApiConstants.Consumes);
241-
writer.WriteStartArray();
242240
var consumes = RequestBody.Content.Keys.Distinct().ToList();
243-
foreach (var mediaType in consumes)
241+
if (consumes.Any())
244242
{
245-
writer.WriteValue(mediaType);
246-
}
243+
writer.WritePropertyName(OpenApiConstants.Consumes);
244+
writer.WriteStartArray();
245+
foreach (var mediaType in consumes)
246+
{
247+
writer.WriteValue(mediaType);
248+
}
247249

248-
writer.WriteEndArray();
250+
writer.WriteEndArray();
249251

250-
// This is form data. We need to split the request body into multiple parameters.
251-
if (consumes.Contains("application/x-www-form-urlencoded") ||
252-
consumes.Contains("multipart/form-data"))
253-
{
254-
foreach (var property in RequestBody.Content.First().Value.Schema.Properties)
252+
// This is form data. We need to split the request body into multiple parameters.
253+
if (consumes.Contains("application/x-www-form-urlencoded") ||
254+
consumes.Contains("multipart/form-data"))
255255
{
256-
var paramSchema = property.Value;
257-
if ("string".Equals(paramSchema.Type, StringComparison.OrdinalIgnoreCase)
258-
&& ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)
259-
|| "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)))
256+
foreach (var property in RequestBody.Content.First().Value.Schema.Properties)
260257
{
261-
paramSchema.Type = "file";
262-
paramSchema.Format = null;
263-
}
264-
parameters.Add(
265-
new OpenApiFormDataParameter
258+
var paramSchema = property.Value;
259+
if ("string".Equals(paramSchema.Type, StringComparison.OrdinalIgnoreCase)
260+
&& ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)
261+
|| "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)))
266262
{
267-
Description = property.Value.Description,
268-
Name = property.Key,
269-
Schema = property.Value,
270-
Required = RequestBody.Content.First().Value.Schema.Required.Contains(property.Key)
271-
272-
});
263+
paramSchema.Type = "file";
264+
paramSchema.Format = null;
265+
}
266+
parameters.Add(
267+
new OpenApiFormDataParameter
268+
{
269+
Description = property.Value.Description,
270+
Name = property.Key,
271+
Schema = property.Value,
272+
Required = RequestBody.Content.First().Value.Schema.Required.Contains(property.Key)
273+
});
274+
}
273275
}
274-
}
275-
else
276-
{
277-
var content = RequestBody.Content.Values.FirstOrDefault();
278-
279-
var bodyParameter = new OpenApiBodyParameter
276+
else
280277
{
281-
Description = RequestBody.Description,
282-
// V2 spec actually allows the body to have custom name.
283-
// To allow round-tripping we use an extension to hold the name
284-
Name = "body",
285-
Schema = content?.Schema ?? new OpenApiSchema(),
286-
Required = RequestBody.Required,
287-
Extensions = RequestBody.Extensions.ToDictionary(k => k.Key, v => v.Value) // Clone extensions so we can remove the x-bodyName extensions from the output V2 model.
288-
};
289-
290-
if (bodyParameter.Extensions.ContainsKey(OpenApiConstants.BodyName))
291-
{
292-
bodyParameter.Name = (RequestBody.Extensions[OpenApiConstants.BodyName] as OpenApiString)?.Value ?? "body";
293-
bodyParameter.Extensions.Remove(OpenApiConstants.BodyName);
278+
var content = RequestBody.Content.Values.FirstOrDefault();
279+
280+
var bodyParameter = new OpenApiBodyParameter
281+
{
282+
Description = RequestBody.Description,
283+
// V2 spec actually allows the body to have custom name.
284+
// To allow round-tripping we use an extension to hold the name
285+
Name = "body",
286+
Schema = content?.Schema ?? new OpenApiSchema(),
287+
Required = RequestBody.Required,
288+
Extensions = RequestBody.Extensions.ToDictionary(k => k.Key, v => v.Value) // Clone extensions so we can remove the x-bodyName extensions from the output V2 model.
289+
};
290+
291+
if (bodyParameter.Extensions.ContainsKey(OpenApiConstants.BodyName))
292+
{
293+
bodyParameter.Name = (RequestBody.Extensions[OpenApiConstants.BodyName] as OpenApiString)?.Value ?? "body";
294+
bodyParameter.Extensions.Remove(OpenApiConstants.BodyName);
295+
}
296+
297+
parameters.Add(bodyParameter);
294298
}
295-
296-
parameters.Add(bodyParameter);
297299
}
298300
}
299301

0 commit comments

Comments
 (0)