Skip to content

Commit

Permalink
Code updates for walk-thru comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
biaol-odata committed Mar 5, 2018
1 parent 76a775c commit ba5d506
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,6 @@ internal void WriteTopLevelProperty(ODataProperty property)
});
}

/// <summary>
/// This is the backward-compatible method to write property names and value pairs
/// without omitting null property values.
/// </summary>
/// <param name="owningType">The <see cref="IEdmStructuredType"/> of the resource (or null if not metadata is available).</param>
/// <param name="properties">The enumeration of properties to write out.</param>
/// <param name="isComplexValue">
/// Whether the properties are being written for complex value. Also used for detecting whether stream properties
/// are allowed as named stream properties should only be defined on ODataResource instances
/// </param>
/// <param name="duplicatePropertyNameChecker">The DuplicatePropertyNameChecker to use.</param>
internal void WriteProperties(
IEdmStructuredType owningType,
IEnumerable<ODataProperty> properties,
bool isComplexValue,
IDuplicatePropertyNameChecker duplicatePropertyNameChecker)
{
this.WriteProperties(owningType, properties, isComplexValue, /*omitNullValues*/ false, duplicatePropertyNameChecker);
}

/// <summary>
/// Writes property names and value pairs.
/// </summary>
Expand Down Expand Up @@ -171,7 +151,7 @@ private bool IsOpenProperty(ODataProperty property)
else
{
// TODO: (issue #888) this logic results in type annotations not being written for dynamic properties on types that are not
// marked as open. Type annotations should always be written for dynamic properties whose type cannot be hueristically
// marked as open. Type annotations should always be written for dynamic properties whose type cannot be heuristically
// determined. Need to change this.currentPropertyInfo.MetadataType.IsOpenProperty to this.currentPropertyInfo.MetadataType.IsDynamic,
// and fix related tests and other logic (this change alone results in writing type even if it's already implied by context).
isOpenProperty = (!this.WritingResponse && this.currentPropertyInfo.MetadataType.OwningType == null) // Treat property as dynamic property when writing request and owning type is null
Expand All @@ -194,6 +174,7 @@ private bool IsOpenProperty(ODataProperty property)
/// <param name="isTopLevel">true when writing a top-level property; false for nested properties.</param>
/// <param name="allowStreamProperty">Should pass in true if we are writing a property of an ODataResource instance, false otherwise.
/// Named stream properties should only be defined on ODataResource instances.</param>
/// <param name="omitNullValues">whether to omit null property values for writing.</param>
/// <param name="duplicatePropertyNameChecker">The DuplicatePropertyNameChecker to use.</param>
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Splitting the code would make the logic harder to understand; class coupling is only slightly above threshold.")]
private void WriteProperty(
Expand Down Expand Up @@ -387,6 +368,7 @@ private void WriteStreamReferenceProperty(string propertyName, ODataStreamRefere
/// Writes a Null property.
/// </summary>
/// <param name="property">The property to write out.</param>
/// <param name="omitNullValues">Whether to omit the writing of this null property value.</param>
private void WriteNullProperty(
ODataProperty property,
bool omitNullValues)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Core/JsonLight/ODataJsonLightReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2335,7 +2335,7 @@ private void RestoreOmittedNullValues()
{
// For entity resource, concatenate the property to original enumerable.
resource.Properties =
resource.Properties.Concat(new List<ODataProperty>() {property});
resource.Properties.Concat(new List<ODataProperty>() { property });
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/Microsoft.OData.Core/ODataConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ internal static class ODataInternalConstants
/// </summary>
public const string ContentIdHeader = "Content-ID";

#region Preference Headers
/// <summary>
/// Valid value for <code>OmitValuesPreferenceToken</code> indicating nulls are omitted.
/// </summary>
public const string OmitValuesNulls = "nulls";

/// <summary>
/// Valid value for <code>OmitValuesPreferenceToken</code> indicating defaults are omitted.
/// Internal access only since "defaults" is not supported yet.
/// </summary>
internal const string OmitValuesDefaults = "defaults";
#endregion Preference Headers

/// <summary>
/// Name of the Content-Length HTTP header.
/// </summary>
Expand Down Expand Up @@ -201,17 +214,5 @@ internal static class ODataInternalConstants
internal const string ContextUriDeletedLink = UriSegmentSeparator + DeletedLink;

#endregion Context URL

#region Preference Headers
/// <summary>
/// Valid value for <code>OmitValuesPreferenceToken</code> indicating nulls are omitted.
/// </summary>
internal const string OmitValuesNulls = "nulls";

/// <summary>
/// Valid value for <code>OmitValuesPreferenceToken</code> indicating defaults are omitted.
/// </summary>
internal const string OmitValuesDefaults = "defaults";
#endregion Preference Headers
}
}
10 changes: 5 additions & 5 deletions src/Microsoft.OData.Core/ODataMessageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ public ODataMessageWriter(IODataResponseMessage responseMessage, ODataMessageWri
WriterValidationUtils.ValidateMessageWriterSettings(this.settings, this.writingResponse);
this.message = new ODataResponseMessage(responseMessage, /*writing*/ true, this.settings.EnableMessageStreamDisposal, /*maxMessageSize*/ -1);

// Set the Preference-Applied header's parameter omit-values=nulls per writer settings.
// For response writing, source of specifying omit-null-values preference is the settings object
// from caller(the OData service).
if (this.settings.OmitNullValues)
// Set the writer settings per Preference-Applied header's parameter omit-values.
// For response writing, source of specifying omit-null-values preference is the Preference-Applied header.
if (string.Equals(responseMessage.PreferenceAppliedHeader().OmitValues, ODataConstants.OmitValuesNulls,
StringComparison.OrdinalIgnoreCase))
{
responseMessage.PreferenceAppliedHeader().OmitValues = ODataConstants.OmitValuesNulls;
this.settings.OmitNullValues = true;
}

// If the Preference-Applied header on the response message contains an annotation filter, we set the filter
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Core/ODataMessageWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public ODataUri ODataUri
/// <remarks>
/// Default value is false, that means serialize null values.
/// </remarks>
public bool OmitNullValues { get; set; }
internal bool OmitNullValues { get; set; }

/// <summary>
/// Returns whether ThrowOnUndeclaredPropertyForNonOpenType validation setting is enabled.
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.OData.Core/ODataOutputContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ public virtual Task WriteErrorAsync(ODataError odataError, bool includeDebugInfo
}
#endif

/// <summary>
/// Returns whether properties of null values should be omitted when serializing the payload.
/// </summary>
/// <returns>Return true to omit null-value properties; false otherwise.</returns>
internal bool ShouldOmitNullValues()
{
return this.WritingResponse && this.MessageWriterSettings.OmitNullValues;
}

/// <summary>
/// Writes an <see cref="ODataError"/> into the message payload.
/// </summary>
Expand Down
12 changes: 3 additions & 9 deletions src/Microsoft.OData.Core/ODataPreferenceHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public string OmitValues
get
{
HttpHeaderValueElement omitValues = this.Get(OmitValuesPreferenceToken);
return omitValues?.Value;
return omitValues != null ? omitValues.Value : null;
}

[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Need lower case string here.")]
Expand All @@ -273,14 +273,8 @@ public string OmitValues
{
this.Set(new HttpHeaderValueElement(OmitValuesPreferenceToken, ODataConstants.OmitValuesNulls, EmptyParameters));
}
else if (string.Equals(normalizedValue, ODataConstants.OmitValuesDefaults, StringComparison.Ordinal))
{
throw new NotSupportedException("omit-values=defaults is not supported yet.");
}
else
{
throw new ODataException(Strings.HttpPreferenceHeader_InvalidValueForToken(value, OmitValuesPreferenceToken));
}

// No-ops for other preference values, including "defaults" which is in the OData protocol but not implemented yet.
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/Microsoft.OData.Core/ODataWriterCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,11 +1150,8 @@ protected void ValidateNoDeltaLinkForExpandedResourceSet(ODataResourceSet resour
/// <returns>Return true to omit null-value properties; false otherwise.</returns>
protected bool ShouldOmitNullValues()
{
// Omit null values preference should only affect response payload, and should not
// be applied to writing delta payload.
return this.outputContext.WritingResponse
&& !this.writingDelta
&& this.outputContext.MessageWriterSettings.OmitNullValues;
// Per protocol, omitNullValues doesn't affect delta payload.
return !this.writingDelta && this.outputContext.ShouldOmitNullValues();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ private string SerializeProperty(IEdmStructuredType owningType, ODataProperty od
owningType,
new[] { odataProperty },
/*isComplexValue*/ false,
/*omitNullValues*/ false,
new NullDuplicatePropertyNameChecker());
jsonLightOutputContext.JsonWriter.EndObjectScope();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ private string SerializeProperty(ODataProperty odataProperty, ODataVersion versi
this.entityType,
new[] { odataProperty },
/*isComplexValue*/ false,
/*omitNullValues*/ false,
new NullDuplicatePropertyNameChecker());
jsonLightOutputContext.JsonWriter.EndObjectScope();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4503,6 +4503,7 @@ public sealed class Microsoft.OData.ODataConstants {
public static string MethodPost = "POST"
public static string MethodPut = "PUT"
public static string ODataVersionHeader = "OData-Version"
public static string OmitValuesNulls = "nulls"
}

[
Expand Down Expand Up @@ -5143,7 +5144,6 @@ public sealed class Microsoft.OData.ODataMessageWriterSettings {
string JsonPCallback { public get; public set; }
Microsoft.OData.ODataMessageQuotas MessageQuotas { public get; public set; }
Microsoft.OData.ODataUri ODataUri { public get; public set; }
bool OmitNullValues { public get; public set; }
Microsoft.OData.ValidationKinds Validations { public get; public set; }
System.Nullable`1[[Microsoft.OData.ODataVersion]] Version { public get; public set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
<Content Include="JsonLight\JsonLightEntryWriterTests.OpenPropertiesInEntryTest.approved.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="JsonLight\JsonLightEntryWriterTests.IgnoreNullPropertiesInEntryTest.approved.txt">
<Content Include="JsonLight\JsonLightEntryWriterTests.OmitNullPropertiesInEntryTest.approved.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="JsonLight\JsonLightPropertyWriterTests.WriteFloatingPointValue.approved.txt">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,12 @@ public static TestMessage CreateOutputMessageFromStream(
}

responseMessage.StatusCode = 200;

if (testConfiguration.MessageWriterSettings.OmitNullValues)
{
responseMessage.PreferenceAppliedHeader().OmitValues = Microsoft.OData.ODataConstants.OmitValuesNulls;
}

message = responseMessage;
}

Expand Down

0 comments on commit ba5d506

Please sign in to comment.