Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5902707

Browse files
Merge pull request #1687 from json-api-dotnet/refactor-extensions
Refactor internal-only JSON:API extensions support
2 parents 169ffee + c777ba1 commit 5902707

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

‎src/JsonApiDotNetCore/Serialization/JsonConverters/ResourceObjectConverter.cs‎

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
4444

4545
var resourceObject = new ResourceObject
4646
{
47-
// The 'attributes' element may occur before 'type', but we need to know the resource type before we can deserialize attributes
48-
// into their corresponding CLR types.
49-
Type = PeekType(refreader)
47+
// The 'attributes' or 'relationships' element may occur before 'type', but we need to know the resource type
48+
// before we can deserialize attributes/relationships into their corresponding CLR types.
49+
Type = PeekType(reader)
5050
};
5151

5252
ResourceType? resourceType = resourceObject.Type != null ? _resourceGraph.FindResourceType(resourceObject.Type) : null;
@@ -99,7 +99,15 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
9999
}
100100
case "relationships":
101101
{
102-
resourceObject.Relationships = ReadRelationships(ref reader, options);
102+
if (resourceType != null)
103+
{
104+
resourceObject.Relationships = ReadRelationships(ref reader, options, resourceType);
105+
}
106+
else
107+
{
108+
reader.Skip();
109+
}
110+
103111
break;
104112
}
105113
case "links":
@@ -127,27 +135,27 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
127135
throw GetEndOfStreamError();
128136
}
129137

130-
private static string? PeekType(refUtf8JsonReader reader)
138+
private static string? PeekType(Utf8JsonReader reader)
131139
{
132-
// https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to?pivots=dotnet-5-0#an-alternative-way-to-do-polymorphic-deserialization
133-
Utf8JsonReaderreaderClone=reader;
140+
// This method receives a clone of the reader (which is a struct, and there's no ref modifier on the parameter),
141+
// so advancing here doesn't affect the reader position of the caller.
134142

135-
while (readerClone.Read())
143+
while (reader.Read())
136144
{
137-
if (readerClone.TokenType == JsonTokenType.PropertyName)
145+
if (reader.TokenType == JsonTokenType.PropertyName)
138146
{
139-
string? propertyName = readerClone.GetString();
140-
readerClone.Read();
147+
string? propertyName = reader.GetString();
148+
reader.Read();
141149

142150
switch (propertyName)
143151
{
144152
case "type":
145153
{
146-
return readerClone.GetString();
154+
return reader.GetString();
147155
}
148156
default:
149157
{
150-
readerClone.Skip();
158+
reader.Skip();
151159
break;
152160
}
153161
}
@@ -181,7 +189,7 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
181189
string extensionNamespace = attributeName[..extensionSeparatorIndex];
182190
string extensionName = attributeName[(extensionSeparatorIndex + 1)..];
183191

184-
ValidateExtensionInAttributes(extensionNamespace, extensionName, reader);
192+
ValidateExtensionInAttributes(extensionNamespace, extensionName, resourceType,reader);
185193
reader.Skip();
186194
continue;
187195
}
@@ -232,12 +240,14 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
232240
}
233241

234242
// Currently exposed for internal use only, so we don't need a breaking change when adding support for multiple extensions.
235-
private protected virtual void ValidateExtensionInAttributes(string extensionNamespace, string extensionName, Utf8JsonReader reader)
243+
// ReSharper disable once UnusedParameter.Global
244+
private protected virtual void ValidateExtensionInAttributes(string extensionNamespace, string extensionName, ResourceType resourceType,
245+
Utf8JsonReader reader)
236246
{
237247
throw new JsonException($"Unsupported usage of JSON:API extension '{extensionNamespace}' in attributes.");
238248
}
239249

240-
private Dictionary<string, RelationshipObject?> ReadRelationships(ref Utf8JsonReader reader, JsonSerializerOptions options)
250+
private Dictionary<string, RelationshipObject?> ReadRelationships(ref Utf8JsonReader reader, JsonSerializerOptions options,ResourceTyperesourceType)
241251
{
242252
var relationships = new Dictionary<string, RelationshipObject?>();
243253

@@ -261,7 +271,7 @@ private protected virtual void ValidateExtensionInAttributes(string extensionNam
261271
string extensionNamespace = relationshipName[..extensionSeparatorIndex];
262272
string extensionName = relationshipName[(extensionSeparatorIndex + 1)..];
263273

264-
ValidateExtensionInRelationships(extensionNamespace, extensionName, reader);
274+
ValidateExtensionInRelationships(extensionNamespace, extensionName, resourceType,reader);
265275
reader.Skip();
266276
continue;
267277
}
@@ -277,7 +287,9 @@ private protected virtual void ValidateExtensionInAttributes(string extensionNam
277287
}
278288

279289
// Currently exposed for internal use only, so we don't need a breaking change when adding support for multiple extensions.
280-
private protected virtual void ValidateExtensionInRelationships(string extensionNamespace, string extensionName, Utf8JsonReader reader)
290+
// ReSharper disable once UnusedParameter.Global
291+
private protected virtual void ValidateExtensionInRelationships(string extensionNamespace, string extensionName, ResourceType resourceType,
292+
Utf8JsonReader reader)
281293
{
282294
throw new JsonException($"Unsupported usage of JSON:API extension '{extensionNamespace}' in relationships.");
283295
}

‎test/JsonApiDotNetCoreTests/UnitTests/Serialization/Extensions/ResourceObjectConverterTests.cs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ public ExtensionAwareResourceObjectConverter(IResourceGraph resourceGraph, JsonA
417417
_requestAccessor = requestAccessor;
418418
}
419419

420-
private protected override void ValidateExtensionInAttributes(string extensionNamespace, string extensionName, Utf8JsonReader reader)
420+
private protected override void ValidateExtensionInAttributes(string extensionNamespace, string extensionName, ResourceType resourceType,
421+
Utf8JsonReader reader)
421422
{
422423
if (extensionNamespace == ExtensionNamespace && IsTypeInfoExtensionEnabled && extensionName == "fail")
423424
{
@@ -429,10 +430,11 @@ private protected override void ValidateExtensionInAttributes(string extensionNa
429430
return;
430431
}
431432

432-
base.ValidateExtensionInAttributes(extensionNamespace, extensionName, reader);
433+
base.ValidateExtensionInAttributes(extensionNamespace, extensionName, resourceType,reader);
433434
}
434435

435-
private protected override void ValidateExtensionInRelationships(string extensionNamespace, string extensionName, Utf8JsonReader reader)
436+
private protected override void ValidateExtensionInRelationships(string extensionNamespace, string extensionName, ResourceType resourceType,
437+
Utf8JsonReader reader)
436438
{
437439
if (extensionNamespace == ExtensionNamespace && IsTypeInfoExtensionEnabled && extensionName == "fail")
438440
{
@@ -444,7 +446,7 @@ private protected override void ValidateExtensionInRelationships(string extensio
444446
return;
445447
}
446448

447-
base.ValidateExtensionInRelationships(extensionNamespace, extensionName, reader);
449+
base.ValidateExtensionInRelationships(extensionNamespace, extensionName, resourceType,reader);
448450
}
449451

450452
private protected override void WriteExtensionInAttributes(Utf8JsonWriter writer, ResourceObject value)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /