Skip to content

Commit

Permalink
Clean up redirect response JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Nov 2, 2024
1 parent 592e1dc commit 8ceb912
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
18 changes: 18 additions & 0 deletions src/Framework/Framework/CallerArgumentExpressionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.CompilerServices
{
#if !NET5_0_OR_GREATER
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
internal sealed class CallerArgumentExpressionAttribute : Attribute
{
public CallerArgumentExpressionAttribute(string parameterName)
{
ParameterName = parameterName;
}

public string ParameterName { get; }
}
#endif
}
2 changes: 1 addition & 1 deletion src/Framework/Framework/Controls/KnockoutHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ private static JsExpression TransformOptionValueToExpression(DotvvmBindableObjec
}
else
{
return $"[{handlerNameJson},{{q:{MakeStringLiteral(queueName)}}}]";
return $"[{handlerNameJson},{{q:{MakeStringLiteral(queueName!)}}}]";
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/Framework/Framework/Utils/ThrowHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace DotVVM.Framework.Utils
{
internal static class ThrowHelpers
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArgumentNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET6_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (argument is null)
{
ThrowArgumentNullException(paramName);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
[DoesNotReturn]
private static void ThrowArgumentNullException(string? paramName)
{
throw new ArgumentNullException(paramName);
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.Extensions.Logging;
using DotVVM.Framework.Runtime.Tracing;
using Microsoft.Extensions.DependencyInjection;
using System.Text.Encodings.Web;

namespace DotVVM.Framework.ViewModel.Serialization
{
Expand Down Expand Up @@ -338,25 +339,34 @@ private List<NamedResource> SerializeResources(IDotvvmRequestContext context, Fu
/// <summary>
/// Serializes the redirect action.
/// </summary>
public static string GenerateRedirectActionResponse(string url, bool replace, bool allowSpa, string? downloadName)
/// <return>UTF-8 encoded JSON response</return>
public static byte[] GenerateRedirectActionResponse(string url, bool replace, bool allowSpa, string? downloadName)
{
ThrowHelpers.ArgumentNull(url);
// create result object
return JsonSerializer.Serialize(new {
url,
action = "redirect",
replace,
allowSpa,
download = downloadName
}, DefaultSerializerSettingsProvider.Instance.SettingsHtmlUnsafe);
var result = new MemoryStream();
using (var w = new Utf8JsonWriter(result, new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }))
{
w.WriteStartObject();
w.WriteString("url"u8, url);
w.WriteString("action"u8, "redirect"u8);
if (replace)
w.WriteBoolean("replace"u8, true);
if (allowSpa)
w.WriteBoolean("allowSpa"u8, true);
if (downloadName is not null)
w.WriteString("download"u8, downloadName);
w.WriteEndObject();
}
return result.ToArray();
}

/// <summary>
/// Serializes the missing cached viewmodel action.
/// </summary>
internal static string GenerateMissingCachedViewModelResponse()
{
// create result object
return JsonSerializer.Serialize(new { action = "viewModelNotCached" }, DefaultSerializerSettingsProvider.Instance.SettingsHtmlUnsafe);
return """{"action":"viewModelNotCached"}""";
}

/// <summary>
Expand Down

0 comments on commit 8ceb912

Please sign in to comment.