Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added support retaining body parameter name
  • Loading branch information
darrelmiller committed Aug 16, 2020
commit e5babbd20b13b9e8da77337e380a92df8c44689c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
Expand Down Expand Up @@ -164,6 +165,7 @@ private static OpenApiRequestBody CreateFormBody(ParsingContext context, List<Op
{
var schema = v.Schema;
schema.Description = v.Description;
schema.Extensions = v.Extensions;
return schema;
}),
Required = new HashSet<string>(formParameters.Where(p => p.Required).Select(p => p.Name))
Expand Down Expand Up @@ -201,9 +203,11 @@ internal static OpenApiRequestBody CreateRequestBody(
v => new OpenApiMediaType
{
Schema = bodyParameter.Schema
})
}),
Extensions = bodyParameter.Extensions
};

requestBody.Extensions[OpenApiConstants.BodyName] = new OpenApiString(bodyParameter.Name);
return requestBody;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,11 @@ public static class OpenApiConstants
/// </summary>
public const string DefaultDescription = "Default Description";

/// <summary>
/// Field: BodyName extensions
/// </summary>
public const string BodyName = "x-bodyName";

/// <summary>
/// Field: version3_0_0
/// </summary>
Expand Down
13 changes: 11 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,22 +241,31 @@ public void SerializeAsV2(IOpenApiWriter writer)
Name = property.Key,
Schema = property.Value,
Required = RequestBody.Content.First().Value.Schema.Required.Contains(property.Key)

});
}
}
else
{
var content = RequestBody.Content.Values.FirstOrDefault();

var bodyParameter = new OpenApiBodyParameter
{
Description = RequestBody.Description,
// V2 spec actually allows the body to have custom name.
// Our library does not support this at the moment.
// To allow round-tripping we use an extension to hold the name
Name = "body",
Schema = content?.Schema ?? new OpenApiSchema(),
Required = RequestBody.Required
Required = RequestBody.Required,
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.
};

if (bodyParameter.Extensions.ContainsKey(OpenApiConstants.BodyName))
{
bodyParameter.Name = (RequestBody.Extensions[OpenApiConstants.BodyName] as OpenApiString)?.Value ?? "body";
bodyParameter.Extensions.Remove(OpenApiConstants.BodyName);
}

parameters.Add(bodyParameter);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V2;
Expand Down Expand Up @@ -180,6 +181,9 @@ public class OpenApiOperationTests
Type = "object"
}
}
},
Extensions = {
[OpenApiConstants.BodyName] = new OpenApiString("petObject")
}
},
Responses = new OpenApiResponses
Expand Down