-
-
Notifications
You must be signed in to change notification settings - Fork 382
fix(Table): support QueryPageOptions serialization #7310
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
Changes from all commits
1679565
d2db59e
0bbbb21
1fb327a
ebb0ac1
439ea73
162b699
59e52e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone | ||
|
|
@@ -104,80 +104,14 @@ public class JsonQueryPageOptionsConverter : JsonConverter<QueryPageOptions> | |
| reader.Read(); | ||
| ret.IsVirtualScroll = reader.GetBoolean(); | ||
| } | ||
| else if (propertyName == "searches") | ||
|
|
||
| else if (propertyName == "filterKeyValueAction") | ||
| { | ||
| reader.Read(); | ||
| if (reader.TokenType == JsonTokenType.StartArray) | ||
| { | ||
| while (reader.Read()) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.EndArray) | ||
| { | ||
| break; | ||
| } | ||
| var val = JsonSerializer.Deserialize<SearchFilterAction>(ref reader, options); | ||
| if (val != null) | ||
| { | ||
| ret.Searches.Add(val); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else if (propertyName == "customerSearches") | ||
| { | ||
| reader.Read(); | ||
| if (reader.TokenType == JsonTokenType.StartArray) | ||
| { | ||
| while (reader.Read()) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.EndArray) | ||
| { | ||
| break; | ||
| } | ||
| var val = JsonSerializer.Deserialize<SearchFilterAction>(ref reader, options); | ||
| if (val != null) | ||
| { | ||
| ret.CustomerSearches.Add(val); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else if (propertyName == "advanceSearches") | ||
| { | ||
| reader.Read(); | ||
| if (reader.TokenType == JsonTokenType.StartArray) | ||
| { | ||
| while (reader.Read()) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.EndArray) | ||
| { | ||
| break; | ||
| } | ||
| var val = JsonSerializer.Deserialize<SearchFilterAction>(ref reader, options); | ||
| if (val != null) | ||
| { | ||
| ret.AdvanceSearches.Add(val); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else if (propertyName == "filters") | ||
| { | ||
| reader.Read(); | ||
| if (reader.TokenType == JsonTokenType.StartArray) | ||
| var val = JsonSerializer.Deserialize<FilterKeyValueAction>(ref reader, options); | ||
| if (val != null) | ||
| { | ||
| while (reader.Read()) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.EndArray) | ||
| { | ||
| break; | ||
| } | ||
| var val = JsonSerializer.Deserialize<SearchFilterAction>(ref reader, options); | ||
| if (val != null) | ||
| { | ||
| ret.Filters.Add(val); | ||
| } | ||
| } | ||
| ret.FilterKeyValueAction = val; | ||
| } | ||
| } | ||
| else if (propertyName == "isFirstQuery") | ||
|
|
@@ -252,41 +186,12 @@ public override void Write(Utf8JsonWriter writer, QueryPageOptions value, JsonSe | |
| { | ||
| writer.WriteBoolean("isVirtualScroll", value.IsVirtualScroll); | ||
| } | ||
| if (value.Searches.Count != 0) | ||
| { | ||
| writer.WriteStartArray("searches"); | ||
| foreach (var filter in value.Searches) | ||
| { | ||
| writer.WriteRawValue(JsonSerializer.Serialize(filter, options)); | ||
| } | ||
| writer.WriteEndArray(); | ||
| } | ||
| if (value.CustomerSearches.Count != 0) | ||
| { | ||
| writer.WriteStartArray("customerSearches"); | ||
| foreach (var filter in value.CustomerSearches) | ||
| { | ||
| writer.WriteRawValue(JsonSerializer.Serialize(filter, options)); | ||
| } | ||
| writer.WriteEndArray(); | ||
| } | ||
| if (value.AdvanceSearches.Count != 0) | ||
| { | ||
| writer.WriteStartArray("advanceSearches"); | ||
| foreach (var filter in value.AdvanceSearches) | ||
| { | ||
| writer.WriteRawValue(JsonSerializer.Serialize(filter, options)); | ||
| } | ||
| writer.WriteEndArray(); | ||
| } | ||
| if (value.Filters.Count != 0) | ||
|
|
||
| if (value.Searches.Count != 0||value.CustomerSearches.Count != 0||value.AdvanceSearches.Count != 0|| value.Filters.Count != 0) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Guard against the case where Because deserialization sets |
||
| { | ||
| writer.WriteStartArray("filters"); | ||
| foreach (var filter in value.Filters) | ||
| { | ||
| writer.WriteRawValue(JsonSerializer.Serialize(filter, options)); | ||
| } | ||
| writer.WriteEndArray(); | ||
| writer.WritePropertyName("filterKeyValueAction"); | ||
| var filterKeyValueAction = value.ToFilter(); | ||
| writer.WriteRawValue(JsonSerializer.Serialize(filterKeyValueAction, options)); | ||
| } | ||
| if (value.IsFirstQuery) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone | ||
|
|
||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| internal class ObjectWithTypeConverter : JsonConverter<object> | ||
| { | ||
| public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| using var doc = JsonDocument.ParseValue(ref reader); | ||
|
|
||
| if (!doc.RootElement.TryGetProperty("$type", out var typeProp)) | ||
| return doc.RootElement.Clone(); // 无类型信息 | ||
|
|
||
| var type = Type.GetType(typeProp.GetString()!)!; | ||
|
|
||
| var valueElement = doc.RootElement.GetProperty("value"); | ||
| return JsonSerializer.Deserialize(valueElement.GetRawText(), type, options); | ||
|
Comment on lines
+20
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: Handle cases where the specified type cannot be resolved or the
|
||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) | ||
| { | ||
| writer.WriteStartObject(); | ||
| writer.WriteString("$type", value.GetType().AssemblyQualifiedName); | ||
| writer.WritePropertyName("value"); | ||
| JsonSerializer.Serialize(writer, value, value.GetType(), options); | ||
| writer.WriteEndObject(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚨 issue (security): Using type information from JSON for polymorphic deserialization can be a security risk.
Annotating
FieldValuewithObjectWithTypeConverterallows clients to control the$typeused during deserialization. BecauseType.GetTypewith assembly-qualified names can resolve arbitrary types, this is unsafe for untrusted input. Use a restricted type resolution strategy (e.g., a whitelist or custom resolver) instead of callingType.GetTypedirectly on user data.