Skip to content

Commit 3e720de

Browse files
authored
Make it possible to use Json as storage format (#1526)
* Update IDataClient and ModelSerializationService to support json in storage When creating a new data element 1. Check appmetadata.DataTypes[type].AllowedContentTypes and see if `application/xml` or `application/json` appears first 2. Use fallback `application/xml` When getting or updating a data element 1. Look at dataElement.ContentType and use xml serialization unless it says `application/json` * [Obsolete] methods that does not work when storage format is dependent on DataElement.ContentType * Obsolete methods only work if DataType.AllowedContentTypes does not contain "application/json" * [Obsolete] methods in IDataClient that accepts org and app arguments and replace with similar methods without, as those are not required and is a hassle to provide. * Add more tests * Obsolete org/app parameters to GetBinaryDataList aswell * CodeQL fixes * Rename dataTypeString to dataTypeId for new method keep it for the old signatures in case someone had `dataTypeString: "default"` in their app Activate the warning about not matching interface parameter names and fix the warning I got.
1 parent e89a20f commit 3e720de

File tree

46 files changed

+2145
-697
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2145
-697
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ dotnet_diagnostic.CA2254.severity = none
135135
# CA1822: Mark members as static
136136
dotnet_diagnostic.CA1822.severity = warning
137137

138+
# CA1725: Parameter names should match base declaration
139+
dotnet_diagnostic.CA1725.severity = warning
140+
138141
# IDE0080: Remove unnecessary suppression operator
139142
dotnet_diagnostic.IDE0080.severity = error
140143

src/Altinn.App.Analyzers/FormDataWrapper/FormDataWrapperAnalyzer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ public class FormDataWrapperAnalyzer : DiagnosticAnalyzer
66
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
77
[Diagnostics.FormDataWrapperGenerator.AppMetadataError];
88

9-
public override void Initialize(AnalysisContext analysisContext)
9+
public override void Initialize(AnalysisContext context)
1010
{
11-
analysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
12-
analysisContext.EnableConcurrentExecution();
11+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
12+
context.EnableConcurrentExecution();
1313

14-
analysisContext.RegisterCompilationAction(CompilationAnalysisAction);
14+
context.RegisterCompilationAction(CompilationAnalysisAction);
1515
}
1616

1717
private void CompilationAnalysisAction(CompilationAnalysisContext compilationContext)

src/Altinn.App.Analyzers/Utils/EquatableArray.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ public EquatableArray(T[]? array)
3030
}
3131

3232
/// <inheritdoc/>
33-
public bool Equals(EquatableArray<T> array)
33+
public bool Equals(EquatableArray<T> other)
3434
{
35-
return AsSpan().SequenceEqual(array.AsSpan());
35+
return AsSpan().SequenceEqual(other.AsSpan());
3636
}
3737

38-
/// <sinheritdoc/>
38+
/// <inheritdoc/>
3939
public override bool Equals(object? obj)
4040
{
4141
return obj is EquatableArray<T> array && this.Equals(array);
4242
}
4343

44-
/// <sinheritdoc/>
44+
/// <inheritdoc/>
4545
public override int GetHashCode()
4646
{
4747
if (_array is null)

src/Altinn.App.Api/Controllers/DataController.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -535,19 +535,17 @@ public async Task<ActionResult> Get(
535535
{
536536
return await GetFormData(
537537
org,
538-
app,
539538
instanceOwnerPartyId,
540539
instanceGuid,
541540
instance,
542541
dataGuid,
543542
dataElement,
544-
dataTypeObject,
545543
includeRowId,
546544
language
547545
);
548546
}
549547

550-
return await GetBinaryData(org, app, instanceOwnerPartyId, instanceGuid, dataGuid, dataElement);
548+
return await GetBinaryData(org, instanceOwnerPartyId, instanceGuid, dataGuid, dataElement);
551549
}
552550
catch (PlatformHttpException e)
553551
{
@@ -898,14 +896,13 @@ private ObjectResult ExceptionResponse(Exception exception, string message)
898896
/// <returns>The data element is returned in the body of the response</returns>
899897
private async Task<ActionResult> GetBinaryData(
900898
string org,
901-
string app,
902899
int instanceOwnerPartyId,
903900
Guid instanceGuid,
904901
Guid dataGuid,
905902
DataElement dataElement
906903
)
907904
{
908-
Stream dataStream = await _dataClient.GetBinaryData(org, app, instanceOwnerPartyId, instanceGuid, dataGuid);
905+
Stream dataStream = await _dataClient.GetBinaryData(instanceOwnerPartyId, instanceGuid, dataGuid);
909906

910907
if (dataStream is not null)
911908
{
@@ -935,26 +932,17 @@ DataElement dataElement
935932
/// <returns>data element is returned in response body</returns>
936933
private async Task<ActionResult> GetFormData(
937934
string org,
938-
string app,
939935
int instanceOwnerId,
940936
Guid instanceGuid,
941937
Instance instance,
942938
Guid dataGuid,
943939
DataElement dataElement,
944-
DataType dataType,
945940
bool includeRowId,
946941
string? language
947942
)
948943
{
949944
// Get Form Data from data service. Assumes that the data element is form data.
950-
object appModel = await _dataClient.GetFormData(
951-
instanceGuid,
952-
_appModel.GetModelType(dataType.AppLogic.ClassRef),
953-
org,
954-
app,
955-
instanceOwnerId,
956-
dataGuid
957-
);
945+
object appModel = await _dataClient.GetFormData(instance, dataElement);
958946

959947
if (appModel is null)
960948
{
@@ -985,15 +973,7 @@ private async Task<ActionResult> GetFormData(
985973
{
986974
try
987975
{
988-
await _dataClient.UpdateData(
989-
appModel,
990-
instanceGuid,
991-
appModel.GetType(),
992-
org,
993-
app,
994-
instanceOwnerId,
995-
dataGuid
996-
);
976+
await _dataClient.UpdateFormData(instance, appModel, dataElement);
997977
}
998978
catch (PlatformHttpException e) when (e.Response.StatusCode is HttpStatusCode.Forbidden)
999979
{

src/Altinn.App.Api/Controllers/InstancesController.cs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using Altinn.App.Core.Helpers;
1616
using Altinn.App.Core.Helpers.Serialization;
1717
using Altinn.App.Core.Internal.App;
18-
using Altinn.App.Core.Internal.AppModel;
1918
using Altinn.App.Core.Internal.Data;
2019
using Altinn.App.Core.Internal.Events;
2120
using Altinn.App.Core.Internal.Instances;
@@ -62,7 +61,6 @@ public class InstancesController : ControllerBase
6261
private readonly IProfileClient _profileClient;
6362

6463
private readonly IAppMetadata _appMetadata;
65-
private readonly IAppModel _appModel;
6664
private readonly AppImplementationFactory _appImplementationFactory;
6765
private readonly IPDP _pdp;
6866
private readonly IPrefill _prefillService;
@@ -87,7 +85,6 @@ public InstancesController(
8785
IInstanceClient instanceClient,
8886
IDataClient dataClient,
8987
IAppMetadata appMetadata,
90-
IAppModel appModel,
9188
IAuthenticationContext authenticationContext,
9289
IPDP pdp,
9390
IEventsClient eventsClient,
@@ -109,7 +106,6 @@ IServiceProvider serviceProvider
109106
_appMetadata = appMetadata;
110107
_altinnPartyClient = altinnPartyClient;
111108
_registerClient = serviceProvider.GetRequiredService<IRegisterClient>();
112-
_appModel = appModel;
113109
_appImplementationFactory = serviceProvider.GetRequiredService<AppImplementationFactory>();
114110
_pdp = pdp;
115111
_eventsClient = eventsClient;
@@ -960,8 +956,6 @@ private async Task CopyDataFromSourceInstance(
960956
Instance sourceInstance
961957
)
962958
{
963-
string org = application.Org;
964-
string app = application.AppIdentifier.App;
965959
int instanceOwnerPartyId = int.Parse(targetInstance.InstanceOwner.PartyId, CultureInfo.InvariantCulture);
966960

967961
string[] sourceSplit = sourceInstance.Id.Split("/");
@@ -987,28 +981,7 @@ Instance sourceInstance
987981
{
988982
DataType dt = dts.First(dt => dt.Id.Equals(de.DataType, StringComparison.Ordinal));
989983

990-
Type type;
991-
try
992-
{
993-
type = _appModel.GetModelType(dt.AppLogic.ClassRef);
994-
}
995-
catch (Exception altinnAppException)
996-
{
997-
throw new ServiceException(
998-
HttpStatusCode.InternalServerError,
999-
$"App.GetAppModelType failed: {altinnAppException.Message}",
1000-
altinnAppException
1001-
);
1002-
}
1003-
1004-
object data = await _dataClient.GetFormData(
1005-
sourceInstanceGuid,
1006-
type,
1007-
org,
1008-
app,
1009-
instanceOwnerPartyId,
1010-
Guid.Parse(de.Id)
1011-
);
984+
object data = await _dataClient.GetFormData(sourceInstance, de);
1012985

1013986
if (application.CopyInstanceSettings.ExcludedDataFields != null)
1014987
{
@@ -1026,15 +999,7 @@ await _prefillService.PrefillDataModel(
1026999

10271000
ObjectUtils.InitializeAltinnRowId(data);
10281001

1029-
await _dataClient.InsertFormData(
1030-
data,
1031-
Guid.Parse(targetInstance.Id.Split("/")[1]),
1032-
type,
1033-
org,
1034-
app,
1035-
instanceOwnerPartyId,
1036-
dt.Id
1037-
);
1002+
await _dataClient.InsertFormData(targetInstance, dt.Id, data);
10381003

10391004
await UpdatePresentationTextsOnInstance(application.PresentationFields, targetInstance, dt.Id, data);
10401005
await UpdateDataValuesOnInstance(application.DataFields, targetInstance, dt.Id, data);
@@ -1067,8 +1032,6 @@ await _dataClient.InsertFormData(
10671032
if (binaryDataTypes.Any(dt => dt.Id.Equals(de.DataType, StringComparison.Ordinal)))
10681033
{
10691034
using var binaryDataStream = await _dataClient.GetBinaryData(
1070-
org,
1071-
app,
10721035
instanceOwnerPartyId,
10731036
sourceInstanceGuid,
10741037
Guid.Parse(de.Id)

src/Altinn.App.Core/EFormidling/Implementation/DefaultEFormidlingService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,6 @@ ValidAltinnEFormidlingConfiguration config
250250
usedFileNames.Add(uniqueFileName);
251251

252252
await using Stream stream = await _dataClient.GetBinaryData(
253-
applicationMetadata.Org,
254-
applicationMetadata.AppIdentifier.App,
255253
instanceOwnerPartyId,
256254
instanceGuid,
257255
new Guid(dataElement.Id)

src/Altinn.App.Core/Features/Action/UniqueSignatureAuthorizer.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ public async Task<bool> AuthorizeAction(UserActionAuthorizerContext context)
7575
var signatureDataElements = instance.Data.Where(d => dataTypes.Contains(d.DataType)).ToList();
7676
foreach (var signatureDataElement in signatureDataElements)
7777
{
78-
var signee = await GetSigneeFromSignDocument(
79-
appMetadata.AppIdentifier,
80-
context.InstanceIdentifier,
81-
signatureDataElement
82-
);
78+
var signee = await GetSigneeFromSignDocument(context.InstanceIdentifier, signatureDataElement);
8379
bool unauthorized = context.Authentication switch
8480
{
8581
Authenticated.User a => a.UserId.ToString(CultureInfo.InvariantCulture) == signee?.UserId,
@@ -97,14 +93,11 @@ public async Task<bool> AuthorizeAction(UserActionAuthorizerContext context)
9793
}
9894

9995
private async Task<Signee?> GetSigneeFromSignDocument(
100-
AppIdentifier appIdentifier,
10196
InstanceIdentifier instanceIdentifier,
10297
DataElement dataElement
10398
)
10499
{
105100
await using var data = await _dataClient.GetBinaryData(
106-
appIdentifier.Org,
107-
appIdentifier.App,
108101
instanceIdentifier.InstanceOwnerPartyId,
109102
instanceIdentifier.InstanceGuid,
110103
Guid.Parse(dataElement.Id)

src/Altinn.App.Core/Features/Signing/Services/SigningReceiptService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ IDataClient dataClient
206206
.WithSendersReference(element.Id)
207207
.WithData(
208208
await dataClient.GetDataBytes(
209-
appMetadata.AppIdentifier.Org,
210-
appMetadata.AppIdentifier.App,
211209
instanceIdentifier.InstanceOwnerPartyId,
212210
instanceIdentifier.InstanceGuid,
213211
Guid.Parse(element.Id)

src/Altinn.App.Core/Features/Telemetry/Telemetry.DataClient.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ partial class Telemetry
1313
return activity;
1414
}
1515

16-
internal Activity? StartInsertFormDataActivity(Guid? instanceId, int? partyId)
16+
internal Activity? StartUpdateDataActivity(Guid instanceId, Guid dataElementId)
1717
{
18-
var activity = ActivitySource.StartActivity($"{Prefix}.InsertFormData");
18+
var activity = ActivitySource.StartActivity($"{Prefix}.UpdateData");
1919
activity?.SetInstanceId(instanceId);
20-
activity?.SetInstanceOwnerPartyId(partyId);
20+
activity?.SetDataElementId(dataElementId);
2121
return activity;
2222
}
2323

24-
internal Activity? StartUpdateDataActivity(Guid instanceId, Guid dataElementId)
24+
internal Activity? StartUpdateDataActivity(Instance instance, DataElement dataElement)
2525
{
2626
var activity = ActivitySource.StartActivity($"{Prefix}.UpdateData");
27-
activity?.SetInstanceId(instanceId);
28-
activity?.SetDataElementId(dataElementId);
27+
activity?.SetInstanceId(instance);
28+
activity?.SetDataElementId(dataElement);
2929
return activity;
3030
}
3131

@@ -45,14 +45,6 @@ partial class Telemetry
4545
return activity;
4646
}
4747

48-
internal Activity? StartDeleteBinaryDataActivity(Guid? instanceId, int? partyId)
49-
{
50-
var activity = ActivitySource.StartActivity($"{Prefix}.DeleteBinaryData");
51-
activity?.SetInstanceId(instanceId);
52-
activity?.SetInstanceOwnerPartyId(partyId);
53-
return activity;
54-
}
55-
5648
internal Activity? StartInsertBinaryDataActivity(Guid? instanceId, int? partyId)
5749
{
5850
var activity = ActivitySource.StartActivity($"{Prefix}.InsertBinaryData");
@@ -106,6 +98,13 @@ partial class Telemetry
10698
return activity;
10799
}
108100

101+
internal Activity? StartGetFormDataActivity(Instance? instance)
102+
{
103+
var activity = ActivitySource.StartActivity($"{Prefix}.GetFormData");
104+
activity?.SetInstanceId(instance);
105+
return activity;
106+
}
107+
109108
internal Activity? StartLockDataElementActivity(string? instanceId, Guid? dataGuid)
110109
{
111110
var activity = ActivitySource.StartActivity($"{Prefix}.LockDataElement");

0 commit comments

Comments
 (0)