-
|
I have an endpoint that requires some properties, one of which is IFormFile. i.e Without the IFormFile, Kiota generates a request model with properties and their respective type. (I believe asp.net/swaggergen defaults to However, once we change to Is there a way to expose the request model instead of MultiPartBody (with IFormFile support), via config, correct swaggergen attributes, etc? I have looked at this -> The biggest issue we have at the moment is the complexity of handling our |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
Hi @AndrewL97 |
Beta Was this translation helpful? Give feedback.
-
|
Just some details on how I solved it for my projects: APIA .NET 9 API Controller expects a [HttpPost("Decode")]
[Consumes(MediaTypeNames.Multipart.FormData)]
[RequestSizeLimit(ValidationRanges.ReceiptFileMaximumSizeInBytes)]
[ProducesResponseType<IReadOnlyList<NewExpensesClaimLineData>>(StatusCodes.Status200OK)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status400BadRequest)]
public async Task<IResult> Decode([FromForm] IFormFileCollection files, [FromQuery] bool includeContentInResponse = true)
{
}ClientThe generated client code had this method signature: public async Task<List<NewExpensesClaimLineData>?> PostAsync(
DecodePostRequestBody body,
Action<RequestConfiguration<DecodeRequestBuilder.DecodeRequestBuilderPostQueryParameters>>? requestConfiguration = default,
CancellationToken cancellationToken = default)I could not use this PostAsync method for Extra methodpublic async Task<List<NewExpensesClaimLineData>?> PostAsync(
MultipartFormDataContent multipartContent,
Action<RequestConfiguration<DecodeRequestBuilderPostQueryParameters>>? requestConfiguration = default,
CancellationToken cancellationToken = default)
{
// ✨ Just add some extra validation
ArgumentNullException.ThrowIfNull(multipartContent);
ArgumentNullException.ThrowIfNull(multipartContent.Headers.ContentType);
// ✨ Add the same errorMapping as defined in the original generated method
var errorMapping = new Dictionary<string, ParsableFactory<IParsable>>
{
{ "400", ProblemDetails.CreateFromDiscriminatorValue },
{ "401", ProblemDetails.CreateFromDiscriminatorValue }
};
// ✨Create a requestInfo object and make sure to configure it correctly.
var requestInfo = new RequestInformation(Method.POST, UrlTemplate, PathParameters);
requestInfo.Configure(requestConfiguration);
// ✨Get the Stream + ContentType from the multipartContent and set it to the requestInfo
requestInfo.SetStreamContent(await multipartContent.ReadAsStreamAsync(cancellationToken), multipartContent.Headers.ContentType.ToString());
// ✨Now just call the same code as was generated
var collectionResult = await RequestAdapter.SendCollectionAsync(requestInfo, NewExpensesClaimLineData.CreateFromDiscriminatorValue, errorMapping, cancellationToken);
return collectionResult?.AsList();
} |
Beta Was this translation helpful? Give feedback.
Thanks for sharing this. I'm not sure why the FromForm attribute results in the description splitting the schema here. IMHO it should be identical to the description of WithFile endpoint. Maybe worth having @captainsafia take a look at that.
As for your initial question, using the multipart body object, you should be able to add parsable body parts with the add or replace part method. (i.e. just add your model value with application/json as the media type and the property name (casing needs to match) as the part name)