-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
137 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/IntelliTect.Coalesce/Api/Controllers/CoalesceJsonReferenceHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using IntelliTect.Coalesce.Models; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace IntelliTect.Coalesce.Api.Controllers; | ||
|
||
|
||
internal sealed class CoalesceJsonReferenceHandler : ReferenceHandler | ||
{ | ||
public override ReferenceResolver CreateResolver() => new PreserveReferenceResolver(); | ||
|
||
/// <summary> | ||
/// From https://source.dot.net/#System.Text.Json/System/Text/Json/Serialization/PreserveReferenceResolver.cs | ||
/// Same as the normal reference handler, but doesn't make refs for the root <see cref="ApiResult"/>, | ||
/// nor for any <see cref="ICollection"/>s because collections will never be duplicated in Coalesce responses. | ||
/// </summary> | ||
internal sealed class PreserveReferenceResolver : ReferenceResolver | ||
{ | ||
private uint _referenceCount; | ||
private readonly Dictionary<object, string>? _objectToReferenceIdMap; | ||
|
||
public PreserveReferenceResolver() | ||
{ | ||
_objectToReferenceIdMap = new Dictionary<object, string>(ReferenceEqualityComparer.Instance); | ||
} | ||
|
||
public override void AddReference(string referenceId, object value) | ||
=> throw new NotSupportedException(); | ||
|
||
public override string GetReference(object value, out bool alreadyExists) | ||
{ | ||
Debug.Assert(_objectToReferenceIdMap != null); | ||
if (value is ApiResult || value is ICollection) | ||
{ | ||
// Don't produce refs for the root response object, | ||
// nor for collections (collections will never be duplicated) | ||
alreadyExists = false; | ||
return null!; | ||
} | ||
|
||
if (_objectToReferenceIdMap.TryGetValue(value, out string? referenceId)) | ||
{ | ||
alreadyExists = true; | ||
} | ||
else | ||
{ | ||
_referenceCount++; | ||
referenceId = _referenceCount.ToString(); | ||
_objectToReferenceIdMap.Add(value, referenceId); | ||
alreadyExists = false; | ||
} | ||
|
||
return referenceId; | ||
} | ||
|
||
public override object ResolveReference(string referenceId) | ||
=> throw new NotSupportedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters