Skip to content

Commit

Permalink
Remove the Toplevel related codes for writing resource value
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Dec 18, 2018
1 parent 40617cd commit eb0fc9a
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ internal void WriteInstanceAnnotation(ODataInstanceAnnotation instanceAnnotation
this.WriteInstanceAnnotationName(propertyName, name);
this.valueSerializer.WriteResourceValue(resourceValue,
expectedType,
false, /*isTopLevel*/
treatLikeOpenProperty,
this.valueSerializer.CreateDuplicatePropertyNameChecker());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ protected override void WriteCollectionItem(object item, IEdmTypeReference expec
this.jsonLightCollectionSerializer.WriteResourceValue(
resourceValue,
expectedItemType,
false /*isTopLevel*/,
false /*isOpenPropertyType*/,
this.DuplicatePropertyNameChecker);
this.jsonLightCollectionSerializer.AssertRecursionDepthIsZero();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,12 @@ private void WriteResourceProperty(
ODataResourceValue resourceValue,
bool isOpenPropertyType)
{
if (!this.currentPropertyInfo.IsTopLevel)
{
this.JsonWriter.WriteName(property.Name);
}
Debug.Assert(!this.currentPropertyInfo.IsTopLevel, "Resource property should not be top level");
this.JsonWriter.WriteName(property.Name);

this.JsonLightValueSerializer.WriteResourceValue(
resourceValue,
this.currentPropertyInfo.MetadataType.TypeReference,
this.currentPropertyInfo.IsTopLevel,
isOpenPropertyType,
this.CreateDuplicatePropertyNameChecker());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public virtual void WriteEnumValue(
/// </summary>
/// <param name="resourceValue">The resource (complex or entity) value to write.</param>
/// <param name="metadataTypeReference">The metadata type for the resource value.</param>
/// <param name="isTopLevel">true when writing a top-level property; false for nested properties.</param>
/// <param name="isOpenPropertyType">true if the type name belongs to an open (dynamic) property.</param>
/// <param name="duplicatePropertyNamesChecker">The checker instance for duplicate property names.</param>
/// <remarks>The current recursion depth should be a value, measured by the number of resource and collection values between
Expand All @@ -111,7 +110,6 @@ public virtual void WriteEnumValue(
public virtual void WriteResourceValue(
ODataResourceValue resourceValue,
IEdmTypeReference metadataTypeReference,
bool isTopLevel,
bool isOpenPropertyType,
IDuplicatePropertyNameChecker duplicatePropertyNamesChecker)
{
Expand All @@ -120,29 +118,14 @@ public virtual void WriteResourceValue(
this.IncreaseRecursionDepth();

// Start the object scope which will represent the entire resource instance;
// for top-level resource properties we already wrote the object scope (and the context URI when needed).
if (!isTopLevel)
{
this.JsonWriter.StartObjectScope();
}
this.JsonWriter.StartObjectScope();

string typeName = resourceValue.TypeName;

if (isTopLevel)
// In requests, we allow the property type reference to be null if the type name is specified in the OM
if (metadataTypeReference == null && !this.WritingResponse && typeName == null && this.Model.IsUserModel())
{
Debug.Assert(metadataTypeReference == null, "Never expect a metadata type for top-level properties.");
if (typeName == null)
{
throw new ODataException(ODataErrorStrings.ODataJsonLightValueSerializer_MissingTypeNameOnResource);
}
}
else
{
// In requests, we allow the property type reference to be null if the type name is specified in the OM
if (metadataTypeReference == null && !this.WritingResponse && typeName == null && this.Model.IsUserModel())
{
throw new ODataException(ODataErrorStrings.ODataJsonLightPropertyAndValueSerializer_NoExpectedTypeOrTypeNameSpecifiedForResourceValueRequest);
}
throw new ODataException(ODataErrorStrings.ODataJsonLightPropertyAndValueSerializer_NoExpectedTypeOrTypeNameSpecifiedForResourceValueRequest);
}

// Resolve the type name to the type; if no type name is specified we will use the type inferred from metadata.
Expand All @@ -169,11 +152,7 @@ public virtual void WriteResourceValue(
duplicatePropertyNamesChecker);

// End the object scope which represents the resource instance;
// for top-level resource properties we already wrote the end object scope.
if (!isTopLevel)
{
this.JsonWriter.EndObjectScope();
}
this.JsonWriter.EndObjectScope();

this.DecreaseRecursionDepth();
}
Expand Down Expand Up @@ -265,7 +244,6 @@ public virtual void WriteCollectionValue(ODataCollectionValue collectionValue, I
this.WriteResourceValue(
itemAsResourceValue,
expectedItemTypeReference,
false /*isTopLevel*/,
false /*isOpenPropertyType*/,
duplicatePropertyNamesChecker);

Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.OData.Core/Microsoft.OData.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,6 @@ internal sealed class TextRes {
internal const string ODataInstanceAnnotation_BadTermName = "ODataInstanceAnnotation_BadTermName";
internal const string ODataInstanceAnnotation_ValueCannotBeODataStreamReferenceValue = "ODataInstanceAnnotation_ValueCannotBeODataStreamReferenceValue";
internal const string ODataJsonLightValueSerializer_MissingTypeNameOnCollection = "ODataJsonLightValueSerializer_MissingTypeNameOnCollection";
internal const string ODataJsonLightValueSerializer_MissingTypeNameOnResource = "ODataJsonLightValueSerializer_MissingTypeNameOnResource";
internal const string ODataJsonLightValueSerializer_MissingRawValueOnUntyped = "ODataJsonLightValueSerializer_MissingRawValueOnUntyped";
internal const string AtomInstanceAnnotation_MissingTermAttributeOnAnnotationElement = "AtomInstanceAnnotation_MissingTermAttributeOnAnnotationElement";
internal const string AtomInstanceAnnotation_AttributeValueNotationUsedWithIncompatibleType = "AtomInstanceAnnotation_AttributeValueNotationUsedWithIncompatibleType";
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.OData.Core/Microsoft.OData.Core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,6 @@ ODataInstanceAnnotation_BadTermName='{0}' is an invalid instance annotation name
ODataInstanceAnnotation_ValueCannotBeODataStreamReferenceValue=The value of an instance annotation cannot be of type ODataStreamReferenceValue.

ODataJsonLightValueSerializer_MissingTypeNameOnCollection=A type name was not provided for an instance of ODataCollectionValue.
ODataJsonLightValueSerializer_MissingTypeNameOnResource=A type name was not provided for an instance of ODataResourceValue.
ODataJsonLightValueSerializer_MissingRawValueOnUntyped=A raw value was not provided for an instance of ODataUntypedValue.

AtomInstanceAnnotation_MissingTermAttributeOnAnnotationElement=Encountered an 'annotation' element without a 'term' attribute. All 'annotation' elements must have a 'term' attribute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4150,15 +4150,6 @@ internal static string ODataJsonLightValueSerializer_MissingTypeNameOnCollection
}
}

/// <summary>
/// A string like "A type name was not provided for an instance of ODataResourceValue."
/// </summary>
internal static string ODataJsonLightValueSerializer_MissingTypeNameOnResource {
get {
return Microsoft.OData.TextRes.GetString(Microsoft.OData.TextRes.ODataJsonLightValueSerializer_MissingTypeNameOnResource);
}
}

/// <summary>
/// A string like "A raw value was not provided for an instance of ODataUntypedValue."
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.OData.Core/Uri/ODataUriConversionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ internal static string ConvertToResourceLiteral(ODataResourceValue resourceValue
(serializer) => serializer.WriteResourceValue(
resourceValue, /* resourceValue */
null, /* metadataTypeReference */
false, /* isTopLevel */
true, /* isOpenPropertyType */
serializer.CreateDuplicatePropertyNameChecker()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,10 @@ public void WriteInstanceAnnotation_ForResourceShouldUseResourceCodePath()
name.Should().Be("@" + term);
verifierCalls++;
};
this.valueWriter.WriteResourceValueVerifier = (value, reference, isTopLevel, isOpenProperty, dupChecker) =>
this.valueWriter.WriteResourceValueVerifier = (value, reference, isOpenProperty, dupChecker) =>
{
value.Should().Be(resourceValue);
reference.Should().BeNull();
isTopLevel.Should().BeFalse();
isOpenProperty.Should().BeTrue();
verifierCalls.Should().Be(1);
verifierCalls++;
Expand Down Expand Up @@ -574,7 +573,7 @@ public void WriteInstanceAnnotationShouldPassResourceTypeFromModelToUnderlyingWr

bool calledWriteResource = false;

this.valueWriter.WriteResourceValueVerifier = (resourceValue, typeReference, isTopLevel, isOpenProperty, dupChecker) =>
this.valueWriter.WriteResourceValueVerifier = (resourceValue, typeReference, isOpenProperty, dupChecker) =>
{
typeReference.Should().NotBeNull();
typeReference.IsComplex().Should().BeTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class MockJsonLightValueSerializer : ODataJsonLightValueSerializer
public const string ComplexOutput = "FAKE_COMPLEX_VALUE";
public const string CollectionOutput = "FAKE_COLLECTION_VALUE_";

public Action<ODataResourceValue, IEdmTypeReference, bool, bool, IDuplicatePropertyNameChecker> WriteResourceValueVerifier { get; set; }
public Action<ODataResourceValue, IEdmTypeReference, bool, IDuplicatePropertyNameChecker> WriteResourceValueVerifier { get; set; }
public Action<ODataCollectionValue, IEdmTypeReference, IEdmTypeReference, bool, bool, bool> WriteCollectionVerifier { get; set; }
public Action<object, IEdmTypeReference> WritePrimitiveVerifier { get; set; }
public Action<object, IEdmTypeReference> WriteEnumVerifier { get; set; }
Expand All @@ -36,10 +36,10 @@ public override void WriteNullValue()
this.WriteNullVerifier();
}

public override void WriteResourceValue(ODataResourceValue resourceValue, IEdmTypeReference metadataTypeReference, bool isTopLevel, bool isOpenPropertyType, IDuplicatePropertyNameChecker duplicatePropertyNamesChecker)
public override void WriteResourceValue(ODataResourceValue resourceValue, IEdmTypeReference metadataTypeReference, bool isOpenPropertyType, IDuplicatePropertyNameChecker duplicatePropertyNamesChecker)
{
this.WriteResourceValueVerifier.Should().NotBeNull("WriteResourceValue was called.");
this.WriteResourceValueVerifier(resourceValue, metadataTypeReference, isTopLevel, isOpenPropertyType, duplicatePropertyNamesChecker);
this.WriteResourceValueVerifier(resourceValue, metadataTypeReference, isOpenPropertyType, duplicatePropertyNamesChecker);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,14 @@ public void WritingDateTimeOffsetWithCustomFormat()
result.Should().Be("\"Thu, 12 Apr 2012 18:43:10 GMT\"");
}

[Fact]
public void WritingResourceValueWithoutTypeNameInTopLevelShouldFail()
{
var serializer = CreateODataJsonLightValueSerializer(true);

var resourceValue = new ODataResourceValue();

Action test = () => serializer.WriteResourceValue(resourceValue, null, true /*isTopLevel*/, false, null);

test.ShouldThrow<ODataException>().WithMessage(Strings.ODataJsonLightValueSerializer_MissingTypeNameOnResource);
}

[Fact]
public void WritingResourceValueWithoutMetadataTypeAndWithoutTypeNameInRequestShouldFail()
{
var serializer = CreateODataJsonLightValueSerializer(false);

var resourceValue = new ODataResourceValue();

Action test = () => serializer.WriteResourceValue(resourceValue, null, false /*isTopLevel*/, false, null);
Action test = () => serializer.WriteResourceValue(resourceValue, null, false, null);

test.ShouldThrow<ODataException>().WithMessage(Strings.ODataJsonLightPropertyAndValueSerializer_NoExpectedTypeOrTypeNameSpecifiedForResourceValueRequest);
}
Expand Down Expand Up @@ -188,7 +176,7 @@ public void WritingResourceValueWithPropertiesShouldWrite()
};

var entityTypeRef = new EdmEntityTypeReference(entityType, false);
serializer.WriteResourceValue(resourceValue, entityTypeRef, false, false, serializer.CreateDuplicatePropertyNameChecker());
serializer.WriteResourceValue(resourceValue, entityTypeRef, false, serializer.CreateDuplicatePropertyNameChecker());
});

Assert.Equal(@"{""Name"":""MyName"",""Location"":{""City"":""MyCity""}}", result);
Expand Down Expand Up @@ -247,7 +235,7 @@ public void WritingInstanceAnnotationInResourceValueShouldWrite()
};

var complexTypeRef = new EdmComplexTypeReference(complexType, false);
serializer.WriteResourceValue(resourceValue, complexTypeRef, false, false, serializer.CreateDuplicatePropertyNameChecker());
serializer.WriteResourceValue(resourceValue, complexTypeRef, false, serializer.CreateDuplicatePropertyNameChecker());
});

Assert.Equal(@"{""@Is.ReadOnly"":true}", result);
Expand Down Expand Up @@ -279,7 +267,7 @@ public void WritingMultipleInstanceAnnotationInResourceValueShouldWrite(string f
};

var complexTypeRef = new EdmComplexTypeReference(complexType, false);
serializer.WriteResourceValue(resourceValue, complexTypeRef, false, false, serializer.CreateDuplicatePropertyNameChecker());
serializer.WriteResourceValue(resourceValue, complexTypeRef, false, serializer.CreateDuplicatePropertyNameChecker());
});

Assert.Equal(expect, result);
Expand Down

0 comments on commit eb0fc9a

Please sign in to comment.