Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#322 Deserializing applicationmetadata keeps unmapped properties #323

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ApplicationMetadataController(IAppMetadata appMetadata, ILogger<Applicati
}

/// <summary>
/// Get the application metadata
/// Get the application metadata https://altinncdn.no/schemas/json/application/application-metadata.schema.v1.json
///
/// If org and app does not match, this returns a 409 Conflict response
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion src/Altinn.App.Core/Models/ApplicationMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text.Json.Serialization;
using Altinn.Platform.Storage.Interface.Models;
using Newtonsoft.Json;

Expand Down Expand Up @@ -58,5 +57,11 @@ public ApplicationMetadata(string id)
/// </summary>
[JsonProperty(PropertyName = "logo")]
public Logo? Logo { get; set; }

/// <summary>
/// Holds properties that are not mapped to other properties
/// </summary>
[System.Text.Json.Serialization.JsonExtensionData]
public Dictionary<string, object>? UnmappedProperties { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Text.Json;
using Altinn.App.Core.Internal.App;
using Altinn.App.Core.Models;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

namespace Altinn.App.Api.Tests.Constants;

public class ApplicationMetadataControllerTests
{
private readonly WebApplicationFactory<Program> _factory = new();
private readonly Mock<IAppMetadata> _appMetadataMock = new();

[Fact]
public async Task VeryfyExtraFieldsInApplicationMetadataIsPreserved()
{
var org = "tdd";
var appId = "test-app";
var appMetadataSample = $"{{\"id\":\"{org}/{appId}\",\"org\":\"{org}\",\"title\":{{\"nb\":\"Bestillingseksempelapp\"}},\"dataTypes\":[],\"partyTypesAllowed\":{{}},\"extra_Unknown_list\":[3,\"tre\",{{\"verdi\":3}}]}}";
var application = JsonSerializer.Deserialize<ApplicationMetadata>(appMetadataSample, new JsonSerializerOptions(JsonSerializerDefaults.Web))!;
_appMetadataMock.Setup(m => m.GetApplicationMetadata()).ReturnsAsync(application);
using var client = _factory.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
services.AddTransient<IAppMetadata>(sp => _appMetadataMock.Object);
});
}).CreateClient();

var response = await client.GetStringAsync($"/{org}/{appId}/api/v1/applicationmetadata");

// Assert that unknonwn parts of json is preserved
response.Should().ContainAll("extra_Unknown_list", "verdi\":3");
}
}
26 changes: 26 additions & 0 deletions test/Altinn.App.Core.Tests/Internal/App/AppMedataTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using Altinn.App.Core.Configuration;
using Altinn.App.Core.Internal.App;
using Altinn.App.Core.Models;
Expand All @@ -6,7 +7,9 @@
using Microsoft.Extensions.Options;
using Microsoft.FeatureManagement;
using Moq;
using Newtonsoft.Json;
using Xunit;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Altinn.App.Core.Tests.Internal.App
{
Expand Down Expand Up @@ -461,6 +464,29 @@ public async Task GetApplicationMetadata_logo_can_intstantiate_with_source_and_D
actual.Should().BeEquivalentTo(expected);
}

[Fact]
public async Task GetApplicationMetadata_deserializes_unmapped_properties()
{
AppSettings appSettings = GetAppSettings("AppMetadata", "unmapped-properties.applicationmetadata.json");
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var actual = await appMetadata.GetApplicationMetadata();
actual.Should().NotBeNull();
actual.UnmappedProperties.Should().NotBeNull();
actual.UnmappedProperties!["foo"].Should().BeOfType<JsonElement>();
((JsonElement)actual.UnmappedProperties["foo"]).GetProperty("bar").GetString().Should().Be("baz");
}

[Fact]
public async Task GetApplicationMetadata_deserialize_serialize_unmapped_properties()
{
AppSettings appSettings = GetAppSettings("AppMetadata", "unmapped-properties.applicationmetadata.json");
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var appMetadataObj = await appMetadata.GetApplicationMetadata();
string serialized = JsonSerializer.Serialize(appMetadataObj, new JsonSerializerOptions { WriteIndented = true });
string expected = File.ReadAllText(Path.Join(appBasePath, "AppMetadata", "unmapped-properties.applicationmetadata.expected.json"));
serialized.Should().Be(expected);
}

[Fact]
public async void GetApplicationMetadata_throws_ApplicationConfigException_if_file_not_found()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"Id": "tdd/bestilling",
"Features": {
"footer": true,
"processActions": true,
"jsonObjectInDataResponse": false
},
"OnEntry": {
"InstanceSelection": null,
"Show": "select-instance"
},
"Logo": null,
"VersionId": null,
"Org": "tdd",
"Title": {
"nb": "Bestillingseksempelapp"
},
"ValidFrom": null,
"ValidTo": null,
"ProcessId": null,
"DataTypes": [
{
"Id": "vedlegg",
"Description": null,
"AllowedContentTypes": [
"application/pdf",
"image/png",
"image/jpeg"
],
"AllowedContributers": null,
"AppLogic": null,
"TaskId": "Task_1",
"MaxSize": null,
"MaxCount": 0,
"MinCount": 0,
"Grouping": null,
"EnablePdfCreation": true,
"EnableFileScan": false,
"ValidationErrorOnPendingFileScan": false,
"EnabledFileAnalysers": [],
"EnabledFileValidators": []
},
{
"Id": "ref-data-as-pdf",
"Description": null,
"AllowedContentTypes": [
"application/pdf"
],
"AllowedContributers": null,
"AppLogic": null,
"TaskId": "Task_1",
"MaxSize": null,
"MaxCount": 0,
"MinCount": 1,
"Grouping": null,
"EnablePdfCreation": true,
"EnableFileScan": false,
"ValidationErrorOnPendingFileScan": false,
"EnabledFileAnalysers": [],
"EnabledFileValidators": []
}
],
"PartyTypesAllowed": {
"BankruptcyEstate": true,
"Organisation": true,
"Person": true,
"SubUnit": true
},
"AutoDeleteOnProcessEnd": false,
"PresentationFields": null,
"DataFields": null,
"EFormidling": null,
"MessageBoxConfig": null,
"CopyInstanceSettings": null,
"Created": "2019-09-16T22:22:22",
"CreatedBy": "username",
"LastChanged": null,
"LastChangedBy": null,
"foo": {
"bar": "baz"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"id": "tdd/bestilling",
"org": "tdd",
"created": "2019-09-16T22:22:22",
"createdBy": "username",
"title": { "nb": "Bestillingseksempelapp" },
"dataTypes": [
{
"id": "vedlegg",
"allowedContentTypes": [ "application/pdf", "image/png", "image/jpeg" ],
"minCount": 0,
"taskId": "Task_1"
},
{
"id": "ref-data-as-pdf",
"allowedContentTypes": [ "application/pdf" ],
"minCount": 1,
"taskId": "Task_1"
}
],
"partyTypesAllowed": {
"bankruptcyEstate": true,
"organisation": true,
"person": true,
"subUnit": true
},
"onEntry": {
"show": "select-instance"
},
"foo": {
"bar": "baz"
}
}
Loading