Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix S.T.J tests
  • Loading branch information
layomia committed Jul 12, 2021
commit 653bfd3b88d2d8b0db04fde2574d160cfa245dbb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract partial class PropertyVisibilityTests
public virtual async Task InitOnlyProperties(Type type)
{
// Init-only property included by default.
object obj = JsonSerializerWrapperForString.DeserializeWrapper(@"{""MyInt"":1}", type);
object obj = await JsonSerializerWrapperForString.DeserializeWrapper(@"{""MyInt"":1}", type);
Assert.Equal(1, (int)type.GetProperty("MyInt").GetValue(obj));

// Init-only properties can be serialized.
Expand Down Expand Up @@ -42,7 +42,7 @@ public async Task NonPublicInitOnlySetter_Without_JsonInclude_Fails(Type type)
public virtual async Task NonPublicInitOnlySetter_With_JsonInclude(Type type)
{
// Non-public init-only property setter included with [JsonInclude].
object obj = JsonSerializerWrapperForString.DeserializeWrapper(@"{""MyInt"":1}", type);
object obj = await JsonSerializerWrapperForString.DeserializeWrapper(@"{""MyInt"":1}", type);
Assert.Equal(1, (int)type.GetProperty("MyInt").GetValue(obj));

// Init-only properties can be serialized.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Xunit;

Expand Down Expand Up @@ -395,8 +396,10 @@ private void CheckCompilationDiagnosticsErrors(ImmutableArray<Diagnostic> diagno

private void CheckFieldsPropertiesMethods(Type type, string[] expectedFields, string[] expectedProperties, string[] expectedMethods)
{
string[] receivedFields = type.GetFields().Select(field => field.Name).OrderBy(s => s).ToArray();
string[] receivedProperties = type.GetProperties().Select(property => property.Name).OrderBy(s => s).ToArray();
BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

string[] receivedFields = type.GetFields(bindingFlags).Select(field => field.Name).OrderBy(s => s).ToArray();
string[] receivedProperties = type.GetProperties(bindingFlags).Select(property => property.Name).OrderBy(s => s).ToArray();
string[] receivedMethods = type.GetMethods().Select(method => method.Name).OrderBy(s => s).ToArray();

Assert.Equal(expectedFields, receivedFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ public void MySecondMethod() { }
receivedMethodsWithAttributeNames
);

BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

// Check for FieldInfoWrapper attribute usage.
(string, string[])[] receivedFieldsWithAttributeNames = foundType.GetFields().Select(field => (field.Name, field.GetCustomAttributesData().Cast<CustomAttributeData>().Select(attributeData => attributeData.AttributeType.Name).ToArray())).Where(x => x.Item2.Any()).ToArray();
(string, string[])[] receivedFieldsWithAttributeNames = foundType.GetFields(bindingFlags).Select(field => (field.Name, field.GetCustomAttributesData().Cast<CustomAttributeData>().Select(attributeData => attributeData.AttributeType.Name).ToArray())).Where(x => x.Item2.Any()).ToArray();
Assert.Equal(
new (string, string[])[] {
("PublicDouble", new string[] { "JsonIncludeAttribute" }),
Expand All @@ -169,7 +171,7 @@ public void MySecondMethod() { }
);

// Check for PropertyInfoWrapper attribute usage.
(string, string[])[] receivedPropertyWithAttributeNames = foundType.GetProperties().Select(property => (property.Name, property.GetCustomAttributesData().Cast<CustomAttributeData>().Select(attributeData => attributeData.AttributeType.Name).ToArray())).Where(x => x.Item2.Any()).ToArray();
(string, string[])[] receivedPropertyWithAttributeNames = foundType.GetProperties(bindingFlags).Select(property => (property.Name, property.GetCustomAttributesData().Cast<CustomAttributeData>().Select(attributeData => attributeData.AttributeType.Name).ToArray())).Where(x => x.Item2.Any()).ToArray();
Assert.Equal(
new (string, string[])[] {
("PublicPropertyInt", new string[] { "JsonPropertyNameAttribute" }),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Serialization.Metadata;
using System.Threading.Tasks;

namespace System.Text.Json.Serialization.Tests
{
internal sealed class JsonSerializerWrapperForString_Dynamic
: JsonSerializerWrapperForString
{
protected internal override Task<T> DeserializeWrapper<T>(string json, JsonSerializerOptions options = null)
=> Task.FromResult(JsonSerializer.Deserialize<T>(json, options));

protected internal override Task<object> DeserializeWrapper(string json, Type type, JsonSerializerOptions options = null)
=> Task.FromResult(JsonSerializer.Deserialize(json, type, options));

protected internal override Task<T> DeserializeWrapper<T>(string json, JsonTypeInfo<T> jsonTypeInfo) => throw new NotImplementedException();

protected internal override Task<object> DeserializeWrapper(string json, Type type, JsonSerializerContext context) => throw new NotImplementedException();

protected internal override Task<string> SerializeWrapper(object value, Type inputType, JsonSerializerOptions options = null)
=> Task.FromResult(JsonSerializer.Serialize(value, inputType, options));

protected internal override Task<string> SerializeWrapper<T>(T value, JsonSerializerOptions options = null)
=> Task.FromResult(JsonSerializer.Serialize<T>(value, options));

protected internal override Task<string> SerializeWrapper(object value, Type inputType, JsonSerializerContext context) => throw new NotImplementedException();

protected internal override Task<string> SerializeWrapper<T>(T value, JsonTypeInfo<T> jsonTypeInfo) => throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static JsonPropertyInfo[] HighLowTempsPropInitFunc(JsonSerializerContext
properties[0] = JsonMetadataServices.CreatePropertyInfo<int>(
options,
isProperty: true,
isPublic: false,
isPublic: true,
isVirtual: false,
declaringType: typeof(HighLowTemps),
propertyTypeInfo: jsonContext.Int32,
Expand All @@ -63,7 +63,7 @@ private static JsonPropertyInfo[] HighLowTempsPropInitFunc(JsonSerializerContext
properties[1] = JsonMetadataServices.CreatePropertyInfo<int>(
options,
isProperty: true,
isPublic: false,
isPublic: true,
isVirtual: false,
declaringType: typeof(HighLowTemps),
propertyTypeInfo: jsonContext.Int32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static JsonPropertyInfo[] WeatherForecastWithPOCOsPropInitFunc(JsonSeria
properties[0] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: false,
isPublic: true,
isVirtual: false,
declaringType: typeof(WeatherForecastWithPOCOs),
propertyTypeInfo: jsonContext.DateTimeOffset,
Expand All @@ -63,7 +63,7 @@ private static JsonPropertyInfo[] WeatherForecastWithPOCOsPropInitFunc(JsonSeria
properties[1] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: false,
isPublic: true,
isVirtual: false,
declaringType: typeof(WeatherForecastWithPOCOs),
propertyTypeInfo: jsonContext.Int32,
Expand All @@ -79,7 +79,7 @@ private static JsonPropertyInfo[] WeatherForecastWithPOCOsPropInitFunc(JsonSeria
properties[2] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: false,
isPublic: true,
isVirtual: false,
declaringType: typeof(WeatherForecastWithPOCOs),
propertyTypeInfo: jsonContext.String,
Expand All @@ -95,7 +95,7 @@ private static JsonPropertyInfo[] WeatherForecastWithPOCOsPropInitFunc(JsonSeria
properties[3] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: false,
isPublic: true,
isVirtual: false,
declaringType: typeof(WeatherForecastWithPOCOs),
propertyTypeInfo: jsonContext.ListSystemDateTimeOffset,
Expand All @@ -111,7 +111,7 @@ private static JsonPropertyInfo[] WeatherForecastWithPOCOsPropInitFunc(JsonSeria
properties[4] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: false,
isPublic: true,
isVirtual: false,
declaringType: typeof(WeatherForecastWithPOCOs),
propertyTypeInfo: jsonContext.Dictionary,
Expand All @@ -127,7 +127,7 @@ private static JsonPropertyInfo[] WeatherForecastWithPOCOsPropInitFunc(JsonSeria
properties[5] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: false,
isPublic: true,
isVirtual: false,
declaringType: typeof(WeatherForecastWithPOCOs),
propertyTypeInfo: jsonContext.StringArray,
Expand All @@ -143,7 +143,7 @@ private static JsonPropertyInfo[] WeatherForecastWithPOCOsPropInitFunc(JsonSeria
properties[6] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: false,
isPublic: false,
isPublic: true,
isVirtual: false,
declaringType: typeof(WeatherForecastWithPOCOs),
propertyTypeInfo: jsonContext.String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace System.Text.Json.Serialization.Tests
{
public sealed partial class PropertyVisibilityTestsDynamic : PropertyVisibilityTests
{
public PropertyVisibilityTestsDynamic() : base(new JsonSerializerDynamic()) { }
public PropertyVisibilityTestsDynamic() : base(new JsonSerializerWrapperForString_Dynamic()) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);net461</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -139,7 +139,7 @@
<Compile Include="Serialization\InvalidTypeTests.cs" />
<Compile Include="Serialization\JsonDocumentTests.cs" />
<Compile Include="Serialization\JsonElementTests.cs" />
<Compile Include="Serialization\JsonSerializerDynamic.cs" />
<Compile Include="Serialization\JsonSerializerWrapperForString_Dynamic.cs" />
<Compile Include="Serialization\JsonSerializerWrapperForString.cs" />
<Compile Include="Serialization\JsonSerializationWrapperForStream.cs" />
<Compile Include="Serialization\MetadataTests\JsonContext\DateTimeOffset.cs" />
Expand Down