Skip to content

Commit

Permalink
feat(app): Change FCE MediaTypes (#1729)
Browse files Browse the repository at this point in the history
<!--- Provide a general summary of your changes in the Title above -->

## Description

Changing FCE media types (breaks the API)

~This PR is step 1 of 2(?), the app needs to allow for the new media
types before we yank out the old ones in the DB lookup type.
Tests have been modified to [Theory] and check both types, and checks
for conversion from old to new when storing in DB.~

## Related Issue(s)

- #1728 

## Verification

- [ ] **Your** code builds clean without any errors or warnings
- [ ] Manual testing done (required)
- [ ] Relevant automated test added (if you find this hard, leave it and
we'll help out)

## Documentation

- [ ] Documentation is updated (either in `docs`-directory, Altinnpedia
or a separate linked PR in
[altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if
applicable)
  • Loading branch information
oskogstad authored Feb 3, 2025
1 parent d784890 commit ef4e0a4
Show file tree
Hide file tree
Showing 7 changed files with 2,328 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ public ContentValueDtoValidator(DialogTransmissionContentType contentType)
{
RuleFor(x => x.MediaType)
.NotEmpty()
.Must(value => value is not null && contentType.AllowedMediaTypes.Contains(value))
.Must(value => value is not null && contentType.AllowedMediaTypes
// Manually adding this for backwards compatibility
// until correspondence is updated and deployed
// TODO: https://github.com/Altinn/dialogporten/issues/1782
.Append(MediaTypes.EmbeddableMarkdownDeprecated).Contains(value))
.WithMessage($"{{PropertyName}} '{{PropertyValue}}' is not allowed for content type {contentType.Name}. " +
$"Allowed media types are {string.Join(", ", contentType.AllowedMediaTypes.Select(x => $"'{x}'"))}");
$"Allowed media types are {string.Join(", ", contentType.AllowedMediaTypes
// Removing the deprecated values from the list of allowed media types in the error message
.Where(x => !x.Equals(MediaTypes.EmbeddableMarkdownDeprecated, StringComparison.Ordinal))
.Select(x => $"'{x}'"))}");

When(x =>
x.MediaType is not null
Expand All @@ -43,12 +50,17 @@ x.MediaType is not null

public ContentValueDtoValidator(DialogContentType contentType, IUser? user = null)
{

var allowedMediaTypes = GetAllowedMediaTypes(contentType, user);
RuleFor(x => x.MediaType)
.NotEmpty()
.Must(value => value is not null && allowedMediaTypes.Contains(value))
.WithMessage($"{{PropertyName}} '{{PropertyValue}}' is not allowed for content type {contentType.Name}. " +
$"Allowed media types are {string.Join(", ", allowedMediaTypes.Select(x => $"'{x}'"))}");
$"Allowed media types are {string.Join(", ", allowedMediaTypes
// Removing the deprecated values from the list of allowed media types in the error message
.Where(x => !x.Equals(MediaTypes.EmbeddableMarkdownDeprecated, StringComparison.Ordinal) &&
!x.Equals(MediaTypes.LegacyEmbeddableHtmlDeprecated, StringComparison.Ordinal))
.Select(x => $"'{x}'"))}");

When(x =>
x.MediaType is not null
Expand All @@ -73,7 +85,14 @@ private static string[] GetAllowedMediaTypes(DialogContentType contentType, IUse
DialogContentType.Values.AdditionalInfo when UserHasLegacyHtmlScope(user)
=> contentType.AllowedMediaTypes.Append(MediaTypes.LegacyHtml).ToArray(),
DialogContentType.Values.MainContentReference when UserHasLegacyHtmlScope(user)
=> contentType.AllowedMediaTypes.Append(MediaTypes.LegacyEmbeddableHtml).ToArray(),
=> contentType.AllowedMediaTypes.Append(MediaTypes.LegacyEmbeddableHtml)
// Manually adding this for backwards compatibility
// until correspondence is updated and deployed
// TODO: https://github.com/Altinn/dialogporten/issues/1782
.Append(MediaTypes.EmbeddableMarkdownDeprecated)
.Append(MediaTypes.LegacyEmbeddableHtmlDeprecated).ToArray(),
DialogContentType.Values.MainContentReference
=> contentType.AllowedMediaTypes.Append(MediaTypes.EmbeddableMarkdownDeprecated).ToArray(),
_ => contentType.AllowedMediaTypes
};
private static bool UserHasLegacyHtmlScope(IUser? user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ internal sealed class DialogContentInputConverter<TDialogContent> :
{
TypeId = dialogContentType.Id,
Value = sourceValue.Value,
MediaType = sourceValue.MediaType
// Temporary converting of deprecated media types
// TODO: https://github.com/Altinn/dialogporten/issues/1782
MediaType = sourceValue.MediaType.MapDeprecatedMediaType()
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ internal sealed class TransmissionContentInputConverter<TTransmissionContent> :
{
TypeId = transmissionContentType.Id,
Value = sourceValue.Value,
MediaType = sourceValue.MediaType
// Temporary converting of deprecated media types
// TODO: https://github.com/Altinn/dialogporten/issues/1782
MediaType = sourceValue.MediaType.MapDeprecatedMediaType()
});
}

Expand Down
23 changes: 21 additions & 2 deletions src/Digdir.Domain.Dialogporten.Domain/MediaTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@ namespace Digdir.Domain.Dialogporten.Domain;
public static class MediaTypes
{
public const string EmbeddablePrefix = "application/vnd.dialogporten.frontchannelembed";
public const string EmbeddableMarkdown = $"{EmbeddablePrefix}+json;type=markdown";
public const string LegacyEmbeddableHtml = $"{EmbeddablePrefix}+json;type=html";

public const string EmbeddableMarkdown = $"{EmbeddablePrefix}-url;type=text/markdown";
public const string EmbeddableMarkdownDeprecated = $"{EmbeddablePrefix}+json;type=markdown";

public const string LegacyEmbeddableHtml = $"{EmbeddablePrefix}-url;type=text/html";
public const string LegacyEmbeddableHtmlDeprecated = $"{EmbeddablePrefix}+json;type=html";

public const string LegacyHtml = "text/html";
public const string Markdown = "text/markdown";
public const string PlainText = "text/plain";
}

// Temporary mapping for deprecated media types,
// we will support both old and new media types
// until correspondence is updated and deployed
// TODO: https://github.com/Altinn/dialogporten/issues/1782
public static class MediaTypeExtensions
{
public static string MapDeprecatedMediaType(this string mediaType)
=> mediaType switch
{
MediaTypes.EmbeddableMarkdownDeprecated => MediaTypes.EmbeddableMarkdown,
MediaTypes.LegacyEmbeddableHtmlDeprecated => MediaTypes.LegacyEmbeddableHtml,
_ => mediaType
};
}
Loading

0 comments on commit ef4e0a4

Please sign in to comment.