Skip to content
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

fix: don't raise InconsistentPropertyNamesException if annotation of ExtensionData differs #4

Merged
merged 1 commit into from
Sep 9, 2024
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
Expand Up @@ -30,7 +30,7 @@ internal InconsistentPropertyNamesException(
string? systemTextPropertyName = null
)
: base(
$"The system.text.property name {systemTextPropertyName ?? "(unset)"} and the newtonsoft json property{newtonsoftJsonPropertyName ?? "(unset)"} name don't match for property {propertyName}. Because the logic of this package relies on System.Text.Json but ASP.NET Core relies on Newtonsoft internally, both have to match"
$"The system.text.property name {systemTextPropertyName ?? "(unset)"} and the newtonsoft json property {newtonsoftJsonPropertyName ?? "(unset)"} name don't match for property {propertyName}. Because the logic of this package relies on System.Text.Json but ASP.NET Core relies on Newtonsoft internally, both have to match"
)
{
PropertyName = propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,45 +77,55 @@ private string GetJsonPathOfAnnotatedProperty(
propertyInfo.GetCustomAttribute<JsonPropertyNameAttribute>();
var newtonsoftJsonPropertyAttribute =
propertyInfo.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>();
if (
newtonsoftJsonPropertyAttribute is not null
&& systemTextJsonPropertyNameAttribute is null
)
{
throw new InconsistentPropertyNamesException(
propertyInfo.Name,
newtonsoftJsonPropertyAttribute.PropertyName,
null
);
}
if (
systemTextJsonPropertyNameAttribute is not null
&& newtonsoftJsonPropertyAttribute is null
)
{
throw new InconsistentPropertyNamesException(
propertyInfo.Name,
null,
systemTextJsonPropertyNameAttribute.Name
);
}
if (
newtonsoftJsonPropertyAttribute is not null
&& systemTextJsonPropertyNameAttribute is not null
)

var extensionDataAttribute =
propertyInfo.GetCustomAttribute<JsonExtensionDataAttribute>();
bool propertyJsonNameIsRelevant = extensionDataAttribute is null;
if (propertyJsonNameIsRelevant)
{
if (
newtonsoftJsonPropertyAttribute.PropertyName
!= systemTextJsonPropertyNameAttribute.Name
newtonsoftJsonPropertyAttribute is not null
&& systemTextJsonPropertyNameAttribute is null
)
{
throw new InconsistentPropertyNamesException(
propertyInfo.Name,
newtonsoftJsonPropertyAttribute.PropertyName,
null
);
}

if (
systemTextJsonPropertyNameAttribute is not null
&& newtonsoftJsonPropertyAttribute is null
)
{
throw new InconsistentPropertyNamesException(
propertyInfo.Name,
null,
systemTextJsonPropertyNameAttribute.Name
);
}

if (
newtonsoftJsonPropertyAttribute is not null
&& systemTextJsonPropertyNameAttribute is not null
)
{
if (
newtonsoftJsonPropertyAttribute.PropertyName
!= systemTextJsonPropertyNameAttribute.Name
)
{
throw new InconsistentPropertyNamesException(
propertyInfo.Name,
newtonsoftJsonPropertyAttribute.PropertyName,
systemTextJsonPropertyNameAttribute.Name
);
}
}
}

var jsonPropertyName =
newtonsoftJsonPropertyAttribute?.PropertyName
?? systemTextJsonPropertyNameAttribute?.Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ public void Inconsistent_Serialization_Settings_Raise_Error()
.Should()
.ThrowExactly<InconsistentPropertyNamesException>();
}

[TestMethod]
public void Inconsistent_Serialization_Settings_Raise_NoError_On_ExtensionData_Name()
{
var instantiatingWithInvalidAttributes = () =>
new JsonPatchDocumentExtensionDataAdapter<MyClassWithInconsistentJsonNameAttributes>(
x => x.MyExtensionData
);
instantiatingWithInvalidAttributes.Should().NotThrow<InconsistentPropertyNamesException>();
}
}
2 changes: 1 addition & 1 deletion JsonPatchDocumentExtensionDataAdapter/UnitTest/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class MyClassWithInconsistentJsonNameAttributes
public int Foo { get; set; }
public string? Bar { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("thePropertyWithTheExtensionData")]
[System.Text.Json.Serialization.JsonPropertyName("thePropertyWithTheExtensionData")] // this name is inconsistent but not relevant as it's the extension data themselves
// no newtonsoft attribute here
[System.Text.Json.Serialization.JsonExtensionData]
public IDictionary<string, object>? MyExtensionData { get; set; }
Expand Down
Loading