Altinn / altinn-studio

Next generation open source Altinn platform and applications.
https://docs.altinn.studio
BSD 3-Clause "New" or "Revised" License
116 stars 70 forks source link

Deserialize JSON files to strongly-typed C# classes #14073

Open mlqn opened 1 week ago

mlqn commented 1 week ago

Description

JSON files should be deserialized into strongly-typed C# classes. This would prevent error like #14004.

Current situation:

https://github.com/Altinn/altinn-studio/blob/0aa6a457fd7dcc608a2fd5463ed6bd769d782894/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs#L385-L391

https://github.com/Altinn/altinn-studio/blob/fc5f892a26ca5bf49189d327abcfbd7210671844/backend/src/Designer/Services/Implementation/TextsService.cs#L398-L407

Suggested solution:

public async Task<Layout> GetLayout(string layoutSetName, string layoutName, CancellationToken cancellationToken = default)
{
    ...
    return JsonSerializer.Deserialize<Layout>(fileContent);
}
private static bool UpdateSourceKeys(Source source, TextIdMutation mutation)
{
    if (source.Label == mutation.OldId && mutation.NewId.HasValue)
    {
        source.Label = mutation.NewId.Value;
        return true;
    }
    return false;
}