Skip to content

Commit

Permalink
Fix error for relative context Uri resolution with additional request…
Browse files Browse the repository at this point in the history
… uri.
  • Loading branch information
biaol-odata committed Jul 14, 2018
1 parent ed68ebd commit c92c1b8
Show file tree
Hide file tree
Showing 11 changed files with 608 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,35 @@ private ODataJsonLightContextUriParser(IEdmModel model, Uri contextUriFromPayloa
/// <param name="model">The model to use when resolving the target of the URI.</param>
/// <param name="contextUriFromPayload">The string value of the odata.metadata annotation read from the payload.</param>
/// <param name="payloadKind">The payload kind we expect the context URI to conform to.</param>
/// <param name="clientCustomTypeResolver">The function of client cuetom type resolver.</param>
/// <param name="clientCustomTypeResolver">The function of client custom type resolver.</param>
/// <param name="needParseFragment">Whether the fragment after $metadata should be parsed, if set to false, only MetadataDocumentUri is parsed.</param>
/// <param name="throwIfMetadataConflict">Whether to throw if a type specified in the ContextUri is not found in metadata.</param>
/// <param name="baseUri">Optional value (with default value of null) of base Uri used for resolving the context url.
/// It should be a non-null value when resolving a top-level relative context Uri.
/// </param>
/// <returns>The result from parsing the context URI.</returns>
internal static ODataJsonLightContextUriParseResult Parse(
IEdmModel model,
string contextUriFromPayload,
ODataPayloadKind payloadKind,
Func<IEdmType, string, IEdmType> clientCustomTypeResolver,
bool needParseFragment,
bool throwIfMetadataConflict = true)
bool throwIfMetadataConflict = true,
Uri baseUri = null)
{
if (contextUriFromPayload == null)
{
throw new ODataException(ODataErrorStrings.ODataJsonLightContextUriParser_NullMetadataDocumentUri);
}

// Create an absolute URI from the payload string
// TODO: Support relative context uri and resolving other relative uris
// Create an absolute URI from the payload string.
// Uri.TryCreate(Uri, string, out Uri) try creating the Uri directly from specified string value first.
// Resultant Uri is returned directly if it is an absolute Uri; otherwise, baseUri will be used to resolve the relative Uri
// into an absolute Uri. See https://github.com/Microsoft/referencesource/blob/master/System/net/System/UriExt.cs#L315
Uri contextUri;
if (!Uri.TryCreate(contextUriFromPayload, UriKind.Absolute, out contextUri))
if (!Uri.TryCreate(baseUri, contextUriFromPayload, out contextUri))
{
throw new ODataException(ODataErrorStrings.ODataJsonLightContextUriParser_TopLevelContextUrlShouldBeAbsolute(contextUriFromPayload));
throw new ODataException(ODataErrorStrings.ODataJsonLightContextUriParser_TopLevelContextUrlIsInvalid(contextUriFromPayload));
}

ODataJsonLightContextUriParser parser = new ODataJsonLightContextUriParser(model, contextUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ internal void ReadPayloadStart(
payloadKind,
this.MessageReaderSettings.ClientCustomTypeResolver,
this.JsonLightInputContext.ReadingResponse,
this.JsonLightInputContext.MessageReaderSettings.ThrowIfTypeConflictsWithMetadata);
this.JsonLightInputContext.MessageReaderSettings.ThrowIfTypeConflictsWithMetadata,
this.MessageReaderSettings.RequestUri);
}

this.contextUriParseResult = parseResult;
Expand Down Expand Up @@ -306,7 +307,9 @@ internal Task ReadPayloadStartAsync(
contextUriAnnotationValue,
payloadKind,
this.MessageReaderSettings.ClientCustomTypeResolver,
this.JsonLightInputContext.ReadingResponse);
this.JsonLightInputContext.ReadingResponse,
this.JsonLightInputContext.MessageReaderSettings.ThrowIfTypeConflictsWithMetadata,
this.MessageReaderSettings.RequestUri);
}

#if DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Core/Microsoft.OData.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ internal sealed class TextRes {
internal const string ODataJsonLightContextUriParser_NoModel = "ODataJsonLightContextUriParser_NoModel";
internal const string ODataJsonLightContextUriParser_InvalidContextUrl = "ODataJsonLightContextUriParser_InvalidContextUrl";
internal const string ODataJsonLightContextUriParser_LastSegmentIsKeySegment = "ODataJsonLightContextUriParser_LastSegmentIsKeySegment";
internal const string ODataJsonLightContextUriParser_TopLevelContextUrlShouldBeAbsolute = "ODataJsonLightContextUriParser_TopLevelContextUrlShouldBeAbsolute";
internal const string ODataJsonLightContextUriParser_TopLevelContextUrlIsInvalid = "ODataJsonLightContextUriParser_TopLevelContextUrlIsInvalid";
internal const string ODataJsonLightResourceDeserializer_DeltaRemovedAnnotationMustBeObject = "ODataJsonLightResourceDeserializer_DeltaRemovedAnnotationMustBeObject";
internal const string ODataJsonLightResourceDeserializer_ResourceTypeAnnotationNotFirst = "ODataJsonLightResourceDeserializer_ResourceTypeAnnotationNotFirst";
internal const string ODataJsonLightResourceDeserializer_ResourceInstanceAnnotationPrecededByProperty = "ODataJsonLightResourceDeserializer_ResourceInstanceAnnotationPrecededByProperty";
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Core/Microsoft.OData.Core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ ODataJsonLightContextUriParser_InvalidPayloadKindWithSelectQueryOption=A '$selec
ODataJsonLightContextUriParser_NoModel=No model was specified for the ODataMessageReader. A message reader requires a model for JSON Light payload to be specified in the ODataMessageReader constructor.
ODataJsonLightContextUriParser_InvalidContextUrl=The context URL '{0}' is invalid.
ODataJsonLightContextUriParser_LastSegmentIsKeySegment=Last segment in context URL '{0}' should not be KeySegment.
ODataJsonLightContextUriParser_TopLevelContextUrlShouldBeAbsolute=The top level context URL '{0}' should be an absolute Uri.
ODataJsonLightContextUriParser_TopLevelContextUrlIsInvalid=The top level context URL '{0}' is not a valid absolute or relative Uri.

ODataJsonLightResourceDeserializer_DeltaRemovedAnnotationMustBeObject=Invalid primitive value '{0}' for @removed annotation. @removed annotation must be a JSON object, optionally containing a 'reason' property.
ODataJsonLightResourceDeserializer_ResourceTypeAnnotationNotFirst=The 'odata.type' instance annotation in a resource object is preceded by an invalid property. In OData, the 'odata.type' instance annotation must be either the first property in the JSON object or the second if the 'odata.context' instance annotation is present.
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.OData.Core/ODataMessageReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ public ODataMessageQuotas MessageQuotas
/// </summary>
public bool ReadUntypedAsString { get; set; }

/// <summary>
/// Gets or sets current request Uri.
/// </summary>
public Uri RequestUri { get; set; }

/// <summary>
/// Func to evaluate whether an annotation should be read or skipped by the reader. The func should return true if the annotation should
/// be read and false if the annotation should be skipped. A null value indicates that all annotations should be skipped.
Expand Down Expand Up @@ -262,6 +267,7 @@ private void CopyFrom(ODataMessageReaderSettings other)
this.validations = other.validations;
this.ThrowOnDuplicatePropertyNames = other.ThrowOnDuplicatePropertyNames;
this.ThrowIfTypeConflictsWithMetadata = other.ThrowIfTypeConflictsWithMetadata;
this.RequestUri = other.RequestUri;
this.ThrowOnUndeclaredPropertyForNonOpenType = other.ThrowOnUndeclaredPropertyForNonOpenType;
this.LibraryCompatibility = other.LibraryCompatibility;
this.Version = other.Version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3495,10 +3495,10 @@ internal static string ODataJsonLightContextUriParser_LastSegmentIsKeySegment(ob
}

/// <summary>
/// A string like "The top level context URL '{0}' should be an absolute Uri."
/// A string like "The top level context URL '{0}' is not a valid absolute or relative Uri."
/// </summary>
internal static string ODataJsonLightContextUriParser_TopLevelContextUrlShouldBeAbsolute(object p0) {
return Microsoft.OData.TextRes.GetString(Microsoft.OData.TextRes.ODataJsonLightContextUriParser_TopLevelContextUrlShouldBeAbsolute, p0);
internal static string ODataJsonLightContextUriParser_TopLevelContextUrlIsInvalid(object p0) {
return Microsoft.OData.TextRes.GetString(Microsoft.OData.TextRes.ODataJsonLightContextUriParser_TopLevelContextUrlIsInvalid, p0);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
<Compile Include="..\IntegrationTests\Reader\JsonLight\InstanceAnnotationsReaderIntegrationTests.cs" />
<Compile Include="..\IntegrationTests\Reader\JsonLight\PropertyAndValueJsonLightReaderIntegrationTests.cs" />
<Compile Include="..\ScenarioTests\Reader\JsonLight\ODataJsonLightSingletonReaderTests.cs" />
<Compile Include="..\ScenarioTests\Reader\JsonLight\RelativeUriReaderTests.cs" />
<Compile Include="..\ScenarioTests\Reader\JsonLight\TimeOfDayReaderJsonLightTests.cs" />
<Compile Include="..\IntegrationTests\Evaluation\AutoComputePayloadMetadataInJsonIntegrationTests.cs" />
<Compile Include="..\ScenarioTests\Roundtrip\JsonLight\BinaryValueEncodingTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,32 @@ private EdmModel GetModel()
return model;
}

// TODO: Support relative context uri and resolving other relative uris
[Fact]
public void ParseRelativeContextUrlShouldThrowException()
public void ParseRelativeContextUrlWithBaseUrl()
{
const bool needParseSegment = true;
const bool throwIfMetadataConflict = true;

var model = new EdmModel();
var entityType = new EdmEntityType("Sample", "R");
model.AddElement(entityType);
string relativeUrl1 = "$metadata#Sample.R";
string relativeUrl2 = "/SampleService/$metadata#Sample.R";
var parseResult = ODataJsonLightContextUriParser.Parse(model, relativeUrl1, ODataPayloadKind.Unsupported, null, needParseSegment,
throwIfMetadataConflict, new Uri("http://service/SampleService/EntitySet"));
parseResult.ContextUri.OriginalString.Should().Be("http://service/SampleService/$metadata#Sample.R");

parseResult = ODataJsonLightContextUriParser.Parse(model, relativeUrl2, ODataPayloadKind.Unsupported, null, needParseSegment,
throwIfMetadataConflict, new Uri("http://service/SampleService/EntitySet"));
parseResult.ContextUri.OriginalString.Should().Be("http://service/SampleService/$metadata#Sample.R");
}

[Fact]
public void ParseRelativeContextUrlWithoutBaseUriShouldThrowException()
{
string relativeUrl = "$metadata#R";
Action parseContextUri = () => ODataJsonLightContextUriParser.Parse(new EdmModel(), relativeUrl, ODataPayloadKind.Unsupported, null, true);
parseContextUri.ShouldThrow<ODataException>().WithMessage(ErrorStrings.ODataJsonLightContextUriParser_TopLevelContextUrlShouldBeAbsolute(relativeUrl));
parseContextUri.ShouldThrow<ODataException>().WithMessage(ErrorStrings.ODataJsonLightContextUriParser_TopLevelContextUrlIsInvalid(relativeUrl));
}

[Fact]
Expand Down
Loading

0 comments on commit c92c1b8

Please sign in to comment.