Skip to content

Commit

Permalink
Eliminate duplicated request bodies for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
millicentachieng committed Aug 24, 2022
1 parent 7439ab5 commit 90b6a94
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public static bool IsOperationOverload(this IEdmModel model, IEdmOperation opera
}

/// <summary>
/// Check whether the operaiton import is overload in the model.
/// Check whether the operation import is overload in the model.
/// </summary>
/// <param name="model">The Edm model.</param>
/// <param name="operationImport">The test operations.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,22 @@ public static IDictionary<string, OpenApiRequestBody> CreateRequestBodies(this O
{
Utils.CheckArgumentNull(context, nameof(context));

return new Dictionary<string, OpenApiRequestBody>
Dictionary<string, OpenApiRequestBody> requestBodies = new()
{
{
Constants.ReferencePostRequestBodyName,
CreateRefPostRequestBody()
CreateRefPostRequestBody()
},
{
Constants.ReferencePutRequestBodyName,
CreateRefPutRequestBody()
}
};

// get request bodies for overloaded actions
Dictionary<string, OpenApiRequestBody> requestBodiesForOverloadedActions = CreateRequestBodiesForOverloadedActions(context);

return requestBodies.Union(requestBodiesForOverloadedActions).ToDictionary(k => k.Key, v => v.Value);
}

/// <summary>
Expand Down Expand Up @@ -170,5 +175,23 @@ private static OpenApiRequestBody CreateRefPutRequestBody()
}
};
}

private static Dictionary<string, OpenApiRequestBody> CreateRequestBodiesForOverloadedActions(ODataContext context)
{
Dictionary<string, OpenApiRequestBody> requestBodies = new();

foreach (IEdmAction action in context.Model.SchemaElements.Where(x => x.SchemaElementKind == EdmSchemaElementKind.Action).ToList())
{
if (context.Model.IsOperationOverload(action))
{
requestBodies.Add(
$"{action.Name}RequestBody",
new OpenApiRequestBody()
);
}
}

return requestBodies;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,24 @@ protected override void SetBasicInfo(OpenApiOperation operation)
/// <inheritdoc/>
protected override void SetRequestBody(OpenApiOperation operation)
{
IEdmAction action = EdmOperation as IEdmAction;
if (action != null)
if (EdmOperation is IEdmAction action)
{
operation.RequestBody = Context.CreateRequestBody(action);
if (Context.Model.IsOperationOverload(action))
{
operation.RequestBody = new OpenApiRequestBody
{
UnresolvedReference = true,
Reference = new OpenApiReference
{
Type = ReferenceType.RequestBody,
Id = $"{action.Name}RequestBody"
}
};
}
else
{
operation.RequestBody = Context.CreateRequestBody(action);
}
}

base.SetRequestBody(operation);
Expand Down

0 comments on commit 90b6a94

Please sign in to comment.