Skip to content

Commit e5babbd

Browse files
committed
Added support retaining body parameter name
1 parent 544d0ba commit e5babbd

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/Microsoft.OpenApi.Readers/V2/OpenApiOperationDeserializer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Linq;
6+
using Microsoft.OpenApi.Any;
67
using Microsoft.OpenApi.Extensions;
78
using Microsoft.OpenApi.Models;
89
using Microsoft.OpenApi.Readers.ParseNodes;
@@ -164,6 +165,7 @@ private static OpenApiRequestBody CreateFormBody(ParsingContext context, List<Op
164165
{
165166
var schema = v.Schema;
166167
schema.Description = v.Description;
168+
schema.Extensions = v.Extensions;
167169
return schema;
168170
}),
169171
Required = new HashSet<string>(formParameters.Where(p => p.Required).Select(p => p.Name))
@@ -201,9 +203,11 @@ internal static OpenApiRequestBody CreateRequestBody(
201203
v => new OpenApiMediaType
202204
{
203205
Schema = bodyParameter.Schema
204-
})
206+
}),
207+
Extensions = bodyParameter.Extensions
205208
};
206209

210+
requestBody.Extensions[OpenApiConstants.BodyName] = new OpenApiString(bodyParameter.Name);
207211
return requestBody;
208212
}
209213

src/Microsoft.OpenApi/Models/OpenApiConstants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@ public static class OpenApiConstants
560560
/// </summary>
561561
public const string DefaultDescription = "Default Description";
562562

563+
/// <summary>
564+
/// Field: BodyName extensions
565+
/// </summary>
566+
public const string BodyName = "x-bodyName";
567+
563568
/// <summary>
564569
/// Field: version3_0_0
565570
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,31 @@ public void SerializeAsV2(IOpenApiWriter writer)
241241
Name = property.Key,
242242
Schema = property.Value,
243243
Required = RequestBody.Content.First().Value.Schema.Required.Contains(property.Key)
244+
244245
});
245246
}
246247
}
247248
else
248249
{
249250
var content = RequestBody.Content.Values.FirstOrDefault();
251+
250252
var bodyParameter = new OpenApiBodyParameter
251253
{
252254
Description = RequestBody.Description,
253255
// V2 spec actually allows the body to have custom name.
254-
// Our library does not support this at the moment.
256+
// To allow round-tripping we use an extension to hold the name
255257
Name = "body",
256258
Schema = content?.Schema ?? new OpenApiSchema(),
257-
Required = RequestBody.Required
259+
Required = RequestBody.Required,
260+
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.
258261
};
259262

263+
if (bodyParameter.Extensions.ContainsKey(OpenApiConstants.BodyName))
264+
{
265+
bodyParameter.Name = (RequestBody.Extensions[OpenApiConstants.BodyName] as OpenApiString)?.Value ?? "body";
266+
bodyParameter.Extensions.Remove(OpenApiConstants.BodyName);
267+
}
268+
260269
parameters.Add(bodyParameter);
261270
}
262271
}

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using FluentAssertions;
88
using Microsoft.OpenApi.Any;
99
using Microsoft.OpenApi.Extensions;
10+
using Microsoft.OpenApi.Interfaces;
1011
using Microsoft.OpenApi.Models;
1112
using Microsoft.OpenApi.Readers.ParseNodes;
1213
using Microsoft.OpenApi.Readers.V2;
@@ -180,6 +181,9 @@ public class OpenApiOperationTests
180181
Type = "object"
181182
}
182183
}
184+
},
185+
Extensions = {
186+
[OpenApiConstants.BodyName] = new OpenApiString("petObject")
183187
}
184188
},
185189
Responses = new OpenApiResponses

0 commit comments

Comments
 (0)