Skip to content

Commit f2da65f

Browse files
Add requestbody references as parameter references in v2 serialization
1 parent 6e55511 commit f2da65f

File tree

4 files changed

+51
-46
lines changed

4 files changed

+51
-46
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
160160
// paths
161161
writer.WriteRequiredObject(OpenApiConstants.Paths, Paths, (w, p) => p.SerializeAsV2(w));
162162

163-
// If references have been inlined we don't need the to render the components section
163+
// If references have been inlined we don't need to render the components section
164164
// however if they have cycles, then we will need a component rendered
165165
if (writer.GetSettings().InlineLocalReferences)
166166
{
@@ -208,9 +208,17 @@ public void SerializeAsV2(IOpenApiWriter writer)
208208
});
209209
}
210210
// parameters
211+
IDictionary<string, OpenApiParameter> parameters = Components?.Parameters;
212+
if (Components?.RequestBodies != null)
213+
{
214+
foreach (var requestBody in Components.RequestBodies)
215+
{
216+
parameters.Add(requestBody.Key, requestBody.Value.ConvertToBodyParameter());
217+
}
218+
}
211219
writer.WriteOptionalMap(
212220
OpenApiConstants.Parameters,
213-
Components?.Parameters,
221+
parameters,
214222
(w, key, component) =>
215223
{
216224
if (component.Reference != null &&

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -275,28 +275,18 @@ public void SerializeAsV2(IOpenApiWriter writer)
275275
}
276276
else
277277
{
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);
278+
parameters.Add(RequestBody.ConvertToBodyParameter());
298279
}
299280
}
281+
else if (RequestBody.Reference != null)
282+
{
283+
parameters.Add(
284+
new OpenApiParameter
285+
{
286+
UnresolvedReference = true,
287+
Reference = RequestBody.Reference
288+
});
289+
}
300290
}
301291

302292
if (Responses != null)

src/Microsoft.OpenApi/Models/OpenApiReference.cs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -214,31 +214,17 @@ private string GetExternalReferenceV2()
214214

215215
private string GetReferenceTypeNameAsV2(ReferenceType type)
216216
{
217-
switch (type)
217+
return type switch
218218
{
219-
case ReferenceType.Schema:
220-
return OpenApiConstants.Definitions;
221-
222-
case ReferenceType.Parameter:
223-
return OpenApiConstants.Parameters;
224-
225-
case ReferenceType.Response:
226-
return OpenApiConstants.Responses;
227-
228-
case ReferenceType.Header:
229-
return OpenApiConstants.Headers;
230-
231-
case ReferenceType.Tag:
232-
return OpenApiConstants.Tags;
233-
234-
case ReferenceType.SecurityScheme:
235-
return OpenApiConstants.SecurityDefinitions;
236-
237-
default:
238-
// If the reference type is not supported in V2, simply return null
239-
// to indicate that the reference is not pointing to any object.
240-
return null;
241-
}
219+
ReferenceType.Schema => OpenApiConstants.Definitions,
220+
ReferenceType.Parameter or ReferenceType.RequestBody => OpenApiConstants.Parameters,
221+
ReferenceType.Response => OpenApiConstants.Responses,
222+
ReferenceType.Header => OpenApiConstants.Headers,
223+
ReferenceType.Tag => OpenApiConstants.Tags,
224+
ReferenceType.SecurityScheme => OpenApiConstants.SecurityDefinitions,
225+
_ => null,// If the reference type is not supported in V2, simply return null
226+
// to indicate that the reference is not pointing to any object.
227+
};
242228
}
243229
}
244230
}

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Linq;
56
using Microsoft.OpenApi.Any;
67
using Microsoft.OpenApi.Interfaces;
78
using Microsoft.OpenApi.Writers;
@@ -144,5 +145,25 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
144145
{
145146
// RequestBody object does not exist in V2.
146147
}
148+
149+
internal OpenApiBodyParameter ConvertToBodyParameter()
150+
{
151+
var bodyParameter = new OpenApiBodyParameter
152+
{
153+
Description = Description,
154+
// V2 spec actually allows the body to have custom name.
155+
// To allow round-tripping we use an extension to hold the name
156+
Name = "body",
157+
Schema = Content.Values.FirstOrDefault()?.Schema ?? new OpenApiSchema(),
158+
Required = Required,
159+
Extensions = Extensions.ToDictionary(k => k.Key, v => v.Value) // Clone extensions so we can remove the x-bodyName extensions from the output V2 model.
160+
};
161+
if (bodyParameter.Extensions.ContainsKey(OpenApiConstants.BodyName))
162+
{
163+
bodyParameter.Name = (Extensions[OpenApiConstants.BodyName] as OpenApiString)?.Value ?? "body";
164+
bodyParameter.Extensions.Remove(OpenApiConstants.BodyName);
165+
}
166+
return bodyParameter;
167+
}
147168
}
148169
}

0 commit comments

Comments
 (0)