Skip to content

Commit

Permalink
Fix nullability warnings in MulipartRequestReader
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarne committed Oct 18, 2023
1 parent de0ef73 commit 1c73f06
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/Altinn.App.Api/Controllers/InstancesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public async Task<ActionResult<Instance>> Post(
ApplicationMetadata application = await _appMetadata.GetApplicationMetadata();

RequestPartValidator requestValidator = new RequestPartValidator(application);
string multipartError = requestValidator.ValidateParts(parsedRequest.Parts);
string? multipartError = requestValidator.ValidateParts(parsedRequest.Parts);

if (!string.IsNullOrEmpty(multipartError))
{
Expand Down Expand Up @@ -958,7 +958,7 @@ private async Task StorePrefillParts(Instance instance, ApplicationMetadata appI
DataElement dataElement;
if (dataType?.AppLogic?.ClassRef != null)
{
_logger.LogInformation($"Storing part {part.Name}");
_logger.LogInformation("Storing part {partName}", part.Name);

Type type;
try
Expand All @@ -978,7 +978,7 @@ private async Task StorePrefillParts(Instance instance, ApplicationMetadata appI
throw new InvalidOperationException(deserializer.Error);
}

await _prefillService.PrefillDataModel(instance.InstanceOwner.PartyId, part.Name, data);
await _prefillService.PrefillDataModel(instance.InstanceOwner.PartyId, part.Name!, data);

await _instantiationProcessor.DataCreation(instance, data, null);

Expand All @@ -989,11 +989,11 @@ private async Task StorePrefillParts(Instance instance, ApplicationMetadata appI
org,
app,
instanceOwnerIdAsInt,
part.Name);
part.Name!);
}
else
{
dataElement = await _dataClient.InsertBinaryData(instance.Id, part.Name, part.ContentType, part.FileName, part.Stream);
dataElement = await _dataClient.InsertBinaryData(instance.Id, part.Name!, part.ContentType, part.FileName, part.Stream);
}

if (dataElement == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
#nullable enable

using Altinn.App.Core.Helpers.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Net.Http.Headers;

Expand Down Expand Up @@ -62,15 +59,13 @@ public async Task Read()
{
MultipartReader reader = new MultipartReader(GetBoundary(), request.Body);

MultipartSection section;
MultipartSection? section;
while ((section = await reader.ReadNextSectionAsync()) != null)
{
partCounter++;

bool hasContentDispositionHeader = ContentDispositionHeaderValue
.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition);

if (!hasContentDispositionHeader)
if (!ContentDispositionHeaderValue
.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue? contentDisposition))
{
Errors.Add(string.Format("Part number {0} doesn't have a content disposition", partCounter));
continue;
Expand All @@ -82,8 +77,8 @@ public async Task Read()
continue;
}

string sectionName = contentDisposition.Name.HasValue ? contentDisposition.Name.Value : null;
string contentFileName = null;
string? sectionName = contentDisposition.Name.Value;
string? contentFileName = null;
if (contentDisposition.FileNameStar.HasValue)
{
contentFileName = contentDisposition.FileNameStar.Value;
Expand Down Expand Up @@ -135,7 +130,7 @@ public async Task Read()
private string GetBoundary()
{
MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse(request.ContentType);
return mediaType.Boundary.Value.Trim('"');
return mediaType.Boundary.Value!.Trim('"');
}
}
}
12 changes: 6 additions & 6 deletions src/Altinn.App.Api/Helpers/RequestHandling/RequestPart.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.IO;
#nullable enable

namespace Altinn.App.Api.Helpers.RequestHandling
{
Expand All @@ -10,25 +10,25 @@ public class RequestPart
/// <summary>
/// The stream to access this part.
/// </summary>
public Stream Stream { get; set; }
public Stream Stream { get; set; } = default!;

/// <summary>
/// The file name as given in content description.
/// </summary>
public string FileName { get; set; }
public string? FileName { get; set; }

/// <summary>
/// The parts name.
/// </summary>
public string Name { get; set; }
public string? Name { get; set; }

/// <summary>
/// The content type of the part.
/// </summary>
public string ContentType { get; set; }
public string ContentType { get; set; } = default!;

/// <summary>
/// The file size of the part, if given.
/// The file size of the part, 0 if not given.
/// </summary>
public long FileSize { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using Altinn.Platform.Storage.Interface.Models;
Expand Down Expand Up @@ -26,7 +28,7 @@ public RequestPartValidator(Application appInfo)
/// </summary>
/// <param name="part">The request part to be validated.</param>
/// <returns>null if no errors where found. Otherwise an error message.</returns>
public string ValidatePart(RequestPart part)
public string? ValidatePart(RequestPart part)
{
if (part.Name == "instance")
{
Expand All @@ -44,7 +46,7 @@ public string ValidatePart(RequestPart part)
Console.WriteLine($"// {DateTime.Now} // Debug // appinfo : {appInfo}");
Console.WriteLine($"// {DateTime.Now} // Debug // appinfo.Id : {appInfo.Id}");

DataType dataType = appInfo.DataTypes.Find(e => e.Id == part.Name);
DataType? dataType = appInfo.DataTypes.Find(e => e.Id == part.Name);

Console.WriteLine($"// {DateTime.Now} // Debug // elementType : {dataType}");

Expand Down Expand Up @@ -91,11 +93,11 @@ public string ValidatePart(RequestPart part)
/// </summary>
/// <param name="parts">The list of request parts to be validated.</param>
/// <returns>null if no errors where found. Otherwise an error message.</returns>
public string ValidateParts(List<RequestPart> parts)
public string? ValidateParts(List<RequestPart> parts)
{
foreach (RequestPart part in parts)
{
string partError = ValidatePart(part);
string? partError = ValidatePart(part);
if (partError != null)
{
return partError;
Expand Down

0 comments on commit 1c73f06

Please sign in to comment.