Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative context Uri resolution #1211

Open
wants to merge 2 commits into
base: release-7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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))
Copy link
Contributor

@AlanWong-MS AlanWong-MS Jul 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if contextUriFromPayload is absolute? I'm reading the API here and it says that the second Uri is relative. #ByDesign

Copy link
Contributor Author

@biaol-odata biaol-odata Jul 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it might be some doc issue. The actual implementation of Uri.TryCreate(Uri, string, out Uri) first tries to convert the string to absolute uri first if possible; otherwise it tries to use the baseUri to resolve the string representing relative url to get the result. See here: https://github.com/Microsoft/referencesource/blob/master/System/net/System/UriExt.cs#L315
Added inline comment.


In reply to: 202220739 [](ancestors = 202220739)

Copy link
Contributor

@AlanWong-MS AlanWong-MS Jul 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this! #ByDesign

{
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; }
Copy link
Member

@mikepizzo mikepizzo Jul 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the relationship between the new RequestUri property and the existing BaseUri property? When does RequestUri get set? Can we just use BaseUri? #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseUri is for Atom format and may be deprecated in the future. See

/// This URI will be used in ATOM format only, it would be overridden by &lt;xml:base /&gt; element in ATOM payload.
.


In reply to: 203896122 [](ancestors = 203896122)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason it was to be deprecated was because it was only used in Atom. The reason it was only used in Atom was because we previously didn't support relative URLs in JSON. Now that we support relative URLs in JSON, it seems like we should use this (existing) property, rather than deprecate it and add a new one for the same purpose.


In reply to: 204170332 [](ancestors = 204170332,203896122)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using BaseUrl but it fails in multiple cases when the baseurl is the $metadata url, which is common, among other failing cases.


In reply to: 205241623 [](ancestors = 205241623,204170332,203896122)


/// <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