Skip to content

Commit 62e4999

Browse files
Add consumes and produces properties when we have reference request bodies and responses respectively during v2 serialization
1 parent f2da65f commit 62e4999

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
224224
// operationId
225225
writer.WriteProperty(OpenApiConstants.OperationId, OperationId);
226226

227-
IList<OpenApiParameter> parameters;
227+
List<OpenApiParameter> parameters;
228228
if (Parameters == null)
229229
{
230230
parameters = new List<OpenApiParameter>();
@@ -240,38 +240,11 @@ public void SerializeAsV2(IOpenApiWriter writer)
240240
var consumes = RequestBody.Content.Keys.Distinct().ToList();
241241
if (consumes.Any())
242242
{
243-
writer.WritePropertyName(OpenApiConstants.Consumes);
244-
writer.WriteStartArray();
245-
foreach (var mediaType in consumes)
246-
{
247-
writer.WriteValue(mediaType);
248-
}
249-
250-
writer.WriteEndArray();
251-
252243
// This is form data. We need to split the request body into multiple parameters.
253244
if (consumes.Contains("application/x-www-form-urlencoded") ||
254245
consumes.Contains("multipart/form-data"))
255246
{
256-
foreach (var property in RequestBody.Content.First().Value.Schema.Properties)
257-
{
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)))
262-
{
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-
}
247+
parameters.AddRange(RequestBody.ConvertToFormDataParameters());
275248
}
276249
else
277250
{
@@ -281,18 +254,37 @@ public void SerializeAsV2(IOpenApiWriter writer)
281254
else if (RequestBody.Reference != null)
282255
{
283256
parameters.Add(
284-
new OpenApiParameter
285-
{
257+
new OpenApiParameter
258+
{
286259
UnresolvedReference = true,
287260
Reference = RequestBody.Reference
288261
});
262+
263+
if (RequestBody.Reference.HostDocument != null)
264+
consumes = RequestBody.GetEffective(RequestBody.Reference.HostDocument)?.Content.Keys.Distinct().ToList();
265+
}
266+
267+
if (consumes.Any())
268+
{
269+
writer.WritePropertyName(OpenApiConstants.Consumes);
270+
writer.WriteStartArray();
271+
foreach (var mediaType in consumes)
272+
{
273+
writer.WriteValue(mediaType);
274+
}
275+
writer.WriteEndArray();
289276
}
290277
}
291278

292279
if (Responses != null)
293280
{
294-
var produces = Responses.Where(r => r.Value.Content != null)
281+
var produces = Responses
282+
.Where(r => r.Value.Content != null)
295283
.SelectMany(r => r.Value.Content?.Keys)
284+
.Concat(
285+
Responses
286+
.Where(r => r.Value.Reference != null && r.Value.Reference.HostDocument != null)
287+
.SelectMany(r => r.Value.GetEffective(r.Value.Reference.HostDocument)?.Content?.Keys))
296288
.Distinct()
297289
.ToList();
298290

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using Microsoft.OpenApi.Any;
@@ -165,5 +166,27 @@ internal OpenApiBodyParameter ConvertToBodyParameter()
165166
}
166167
return bodyParameter;
167168
}
169+
170+
internal IEnumerable<OpenApiFormDataParameter> ConvertToFormDataParameters()
171+
{
172+
foreach (var property in Content.First().Value.Schema.Properties)
173+
{
174+
var paramSchema = property.Value;
175+
if ("string".Equals(paramSchema.Type, StringComparison.OrdinalIgnoreCase)
176+
&& ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)
177+
|| "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)))
178+
{
179+
paramSchema.Type = "file";
180+
paramSchema.Format = null;
181+
}
182+
yield return new OpenApiFormDataParameter
183+
{
184+
Description = property.Value.Description,
185+
Name = property.Key,
186+
Schema = property.Value,
187+
Required = Content.First().Value.Schema.Required.Contains(property.Key)
188+
};
189+
}
190+
}
168191
}
169192
}

0 commit comments

Comments
 (0)