Skip to content

[Blazor] Workaround linker regression #45028

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

Merged
merged 1 commit into from
Nov 14, 2022
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
@@ -0,0 +1,5 @@
<linker>
<assembly fullname="Microsoft.AspNetCore.Components.WebAssembly.Authentication">
<type fullname="Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationContext`1" preserve="all" />
</assembly>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@

<Content Remove="$(YarnWorkingDir)**" />
<None Include="$(YarnWorkingDir)*" Exclude="$(YarnWorkingDir)node_modules\**" />

<EmbeddedResource Include="ILLink.Descriptors.xml">
<LogicalName>ILLink.Descriptors.xml</LogicalName>
</EmbeddedResource>

<UpToDateCheckInput Include="@(YarnInputs)" Set="StaticWebassets" />
<UpToDateCheckOutput Include="@(YarnOutputs)" Set="StaticWebassets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication;
[JsonConverter(typeof(Converter))]
public sealed class InteractiveRequestOptions
{
private static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
{
MaxDepth = 32,
PropertyNameCaseInsensitive = true,
};

/// <summary>
/// Gets the request type.
/// </summary>
Expand Down Expand Up @@ -86,17 +92,29 @@ public bool TryRemoveAdditionalParameter(string name)
static TValue Deserialize(JsonElement element) => element.Deserialize<TValue>();
}

internal string ToState() => JsonSerializer.Serialize(this, InteractiveRequestOptionsSerializerContext.Default.InteractiveRequestOptions);

internal static InteractiveRequestOptions FromState(string state) => JsonSerializer.Deserialize(
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "This method serializes InteractiveRequestOptions which has an 'AdditionalRequestParameters' that might contain user defined types but that have already been annotated by 'TryAddAdditionalParameter'.")]
internal string ToState() => JsonSerializer.Serialize(this, SerializerOptions);

[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "This method deserializes InteractiveRequestOptions which has an 'AdditionalRequestParameters' that might contain user defined types but that have already been annotated by 'TryAddAdditionalParameter'.")]
internal static InteractiveRequestOptions FromState(string state) => JsonSerializer.Deserialize<InteractiveRequestOptions>(
state,
InteractiveRequestOptionsSerializerContext.Default.InteractiveRequestOptions);
SerializerOptions);

internal class Converter : JsonConverter<InteractiveRequestOptions>
{
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "This converter reads 'AdditionalRequestParameters' that might contain user defined types but that have already been annotated by 'TryAddAdditionalParameter'.")]
public override InteractiveRequestOptions Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var requestOptions = JsonSerializer.Deserialize(ref reader, InteractiveRequestOptionsSerializerContext.Default.OptionsRecord);
var requestOptions = JsonSerializer.Deserialize<InteractiveOptions>(ref reader, options);

return new InteractiveRequestOptions
{
Expand All @@ -107,26 +125,27 @@ public override InteractiveRequestOptions Read(ref Utf8JsonReader reader, Type t
};
}

[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "This converter writes 'AdditionalRequestParameters' that might contain user defined types but that have already been annotated by 'TryAddAdditionalParameter'.")]
public override void Write(Utf8JsonWriter writer, InteractiveRequestOptions value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(
writer,
new OptionsRecord(value.ReturnUrl, value.Scopes, value.Interaction, value.AdditionalRequestParameters),
InteractiveRequestOptionsSerializerContext.Default.OptionsRecord);
new InteractiveOptions { ReturnUrl = value.ReturnUrl, Scopes = value.Scopes, Interaction = value.Interaction, AdditionalRequestParameters = value.AdditionalRequestParameters },
options);
}

internal record struct OptionsRecord(
[property: JsonInclude] string ReturnUrl,
[property: JsonInclude] IEnumerable<string> Scopes,
[property: JsonInclude] InteractionType Interaction,
[property: JsonInclude] Dictionary<string, object> AdditionalRequestParameters);
}
}
public struct InteractiveOptions
{
public string ReturnUrl { get; set; }

[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, WriteIndented = false)]
[JsonSerializable(typeof(InteractiveRequestOptions))]
[JsonSerializable(typeof(InteractiveRequestOptions.Converter.OptionsRecord))]
[JsonSerializable(typeof(JsonElement))]
internal partial class InteractiveRequestOptionsSerializerContext : JsonSerializerContext
{
public IEnumerable<string> Scopes { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Microsoft framework guidelines say you should use ICollection instead of IEnumerable.


public InteractionType Interaction { get; set; }

public Dictionary<string, object> AdditionalRequestParameters { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use IDictionary instead of Dictionary.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@ private static partial class Log

[LoggerMessage(15, LogLevel.Debug, "Logout redirect completed successfully.", EventName = nameof(LogoutRedirectCompletedSuccessfully))]
public static partial void LogoutRedirectCompletedSuccessfully(ILogger logger);

[LoggerMessage(16, LogLevel.Debug, "Login request '{Request}'.", EventName = nameof(LoginRequest))]
public static partial void LoginRequest(ILogger logger, string request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ protected override async Task OnParametersSetAsync()

private async Task ProcessLogIn(string returnUrl)
{
Log.LoginRequest(Logger, Navigation.HistoryEntryState);
AuthenticationState.ReturnUrl = returnUrl;
var interactiveRequest = GetCachedNavigationState();
var result = await AuthenticationService.SignInAsync(new RemoteAuthenticationContext<TAuthenticationState>
Expand Down