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 13, 2018
1 parent ed68ebd commit 38c09ec
Show file tree
Hide file tree
Showing 6 changed files with 628 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,30 @@ 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
Uri contextUri;
if (!Uri.TryCreate(contextUriFromPayload, UriKind.Absolute, out contextUri))
if (!Uri.TryCreate(baseUri, contextUriFromPayload, out contextUri))
{
throw new ODataException(ODataErrorStrings.ODataJsonLightContextUriParser_TopLevelContextUrlShouldBeAbsolute(contextUriFromPayload));
}
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
6 changes: 6 additions & 0 deletions src/Microsoft.OData.Core/ODataMessageReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ public ODataMessageQuotas MessageQuotas
/// </summary>
internal bool ThrowIfTypeConflictsWithMetadata { get; private set; }

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

/// <summary>
/// Returns whether ThrowOnUndeclaredPropertyForNonOpenType validation setting is enabled.
/// </summary>
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 @@ -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,9 +30,28 @@ 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);
Expand Down
Loading

0 comments on commit 38c09ec

Please sign in to comment.