Skip to content

Commit

Permalink
Cache DuplicateNameChecker in the OutputContext
Browse files Browse the repository at this point in the history
  • Loading branch information
KenitoInc committed Nov 22, 2021
1 parent 18ca274 commit afda687
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ internal void WriteInstanceAnnotation(
this.valueSerializer.WriteResourceValue(resourceValue,
expectedType,
treatLikeOpenProperty,
this.valueSerializer.CreateDuplicatePropertyNameChecker());
this.valueSerializer.JsonLightOutputContext.DuplicatePropertyNameChecker);
return;
}

Expand Down Expand Up @@ -394,7 +394,7 @@ await this.WriteInstanceAnnotationNameAsync(propertyName, annotationName)
await this.valueSerializer.WriteResourceValueAsync(resourceValue,
expectedType,
treatLikeOpenProperty,
this.valueSerializer.CreateDuplicatePropertyNameChecker()).ConfigureAwait(false);
this.valueSerializer.JsonLightOutputContext.DuplicatePropertyNameChecker).ConfigureAwait(false);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal void WriteTopLevelProperty(ODataProperty property)
property,
null /*owningType*/,
true /* isTopLevel */,
this.CreateDuplicatePropertyNameChecker(),
this.JsonLightOutputContext.DuplicatePropertyNameChecker,
null /* metadataBuilder */);
this.JsonLightValueSerializer.AssertRecursionDepthIsZero();

Expand Down Expand Up @@ -280,7 +280,7 @@ await this.WritePropertyAsync(
property,
null /*owningType*/,
true /* isTopLevel */,
this.CreateDuplicatePropertyNameChecker(),
this.JsonLightOutputContext.DuplicatePropertyNameChecker,
null /* metadataBuilder */).ConfigureAwait(false);
this.JsonLightValueSerializer.AssertRecursionDepthIsZero();

Expand Down Expand Up @@ -649,7 +649,7 @@ private void WriteResourceProperty(
resourceValue,
this.currentPropertyInfo.MetadataType.TypeReference,
isOpenPropertyType,
this.CreateDuplicatePropertyNameChecker());
this.JsonLightOutputContext.DuplicatePropertyNameChecker);
}

/// <summary>
Expand Down Expand Up @@ -1074,7 +1074,7 @@ await this.JsonLightValueSerializer.WriteResourceValueAsync(
resourceValue,
this.currentPropertyInfo.MetadataType.TypeReference,
isOpenPropertyType,
this.CreateDuplicatePropertyNameChecker()).ConfigureAwait(false);
this.JsonLightOutputContext.DuplicatePropertyNameChecker).ConfigureAwait(false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public virtual void WriteCollectionValue(
{
if (duplicatePropertyNamesChecker == null)
{
duplicatePropertyNamesChecker = this.CreateDuplicatePropertyNameChecker();
duplicatePropertyNamesChecker = this.JsonLightOutputContext.DuplicatePropertyNameChecker;
}

this.WriteResourceValue(
Expand Down Expand Up @@ -567,7 +567,7 @@ await this.AsynchronousODataAnnotationWriter.WriteODataTypeInstanceAnnotationAsy
{
if (duplicatePropertyNamesChecker == null)
{
duplicatePropertyNamesChecker = this.CreateDuplicatePropertyNameChecker();
duplicatePropertyNamesChecker = this.JsonLightOutputContext.DuplicatePropertyNameChecker;
}

await this.WriteResourceValueAsync(
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Core/ODataCollectionWriterCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected IDuplicatePropertyNameChecker DuplicatePropertyNameChecker
{
return duplicatePropertyNameChecker
?? (duplicatePropertyNameChecker
= outputContext.MessageWriterSettings.Validator.CreateDuplicatePropertyNameChecker());
= outputContext.DuplicatePropertyNameChecker);
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/Microsoft.OData.Core/ODataOutputContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public abstract class ODataOutputContext : IDisposable
/// </summary>
private readonly ODataSimplifiedOptions odataSimplifiedOptions;

/// <summary>
/// Checker to detect duplicate property names.
/// </summary>
private readonly IDuplicatePropertyNameChecker duplicatePropertyNameChecker;

/// <summary>
/// Constructor.
/// </summary>
Expand All @@ -81,6 +86,7 @@ protected ODataOutputContext(
this.payloadValueConverter = ODataPayloadValueConverter.GetPayloadValueConverter(this.container);
this.writerValidator = messageWriterSettings.Validator;
this.odataSimplifiedOptions = ODataSimplifiedOptions.GetODataSimplifiedOptions(this.container, messageWriterSettings.Version);
this.duplicatePropertyNameChecker = MessageWriterSettings.Validator.CreateDuplicatePropertyNameChecker();
}

/// <summary>
Expand Down Expand Up @@ -139,6 +145,17 @@ public IODataPayloadUriConverter PayloadUriConverter
}
}

/// <summary>
/// Checker to detect duplicate property names.
/// </summary>
internal IDuplicatePropertyNameChecker DuplicatePropertyNameChecker
{
get
{
return this.duplicatePropertyNameChecker;
}
}

/// <summary>
/// The optional dependency injection container to get related services for message writing.
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions src/Microsoft.OData.Core/ODataParameterWriterCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ protected IDuplicatePropertyNameChecker DuplicatePropertyNameChecker
{
return this.duplicatePropertyNameChecker ??
(this.duplicatePropertyNameChecker =
outputContext.MessageWriterSettings.Validator
.CreateDuplicatePropertyNameChecker());
outputContext.DuplicatePropertyNameChecker);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Core/Uri/ODataUriConversionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ internal static string ConvertToResourceLiteral(ODataResourceValue resourceValue
resourceValue, /* resourceValue */
null, /* metadataTypeReference */
true, /* isOpenPropertyType */
serializer.CreateDuplicatePropertyNameChecker()));
serializer.JsonLightOutputContext.DuplicatePropertyNameChecker));
}

return builder.ToString();
Expand Down

0 comments on commit afda687

Please sign in to comment.