diff --git a/src/Microsoft.OData.Client/ALinq/ExpressionWriter.cs b/src/Microsoft.OData.Client/ALinq/ExpressionWriter.cs index 14fcfad70d..31789f787b 100644 --- a/src/Microsoft.OData.Client/ALinq/ExpressionWriter.cs +++ b/src/Microsoft.OData.Client/ALinq/ExpressionWriter.cs @@ -75,7 +75,7 @@ private ExpressionWriter(DataServiceContext context, bool inPath) this.builder = new StringBuilder(); this.expressionStack = new Stack(); this.expressionStack.Push(null); - this.uriVersion = Util.ODataVersion4; + this.uriVersion = context.MaxProtocolVersionAsVersion; this.scopeCount = 0; this.writingFunctionsInQuery = false; } diff --git a/src/Microsoft.OData.Client/ALinq/ProjectionAnalyzer.cs b/src/Microsoft.OData.Client/ALinq/ProjectionAnalyzer.cs index 35d221a9cf..62297cc9fa 100644 --- a/src/Microsoft.OData.Client/ALinq/ProjectionAnalyzer.cs +++ b/src/Microsoft.OData.Client/ALinq/ProjectionAnalyzer.cs @@ -229,7 +229,10 @@ private static void Analyze(MemberInitExpression mie, SelectExpandPathBuilder pb /// Context of expression to analyze. private static void AnalyzeResourceExpression(LambdaExpression lambda, ResourceExpression resource, DataServiceContext context) { - SelectExpandPathBuilder pb = new SelectExpandPathBuilder(); + SelectExpandPathBuilder pb = new SelectExpandPathBuilder() + { + UriVersion = context.MaxProtocolVersionAsVersion + }; ProjectionAnalyzer.Analyze(lambda, pb, context); resource.Projection = new ProjectionQueryOptionExpression(lambda.Body.Type, lambda, pb.ProjectionPaths.ToList()); resource.ExpandPaths = pb.ExpandPaths.Union(resource.ExpandPaths, StringComparer.Ordinal).ToList(); diff --git a/src/Microsoft.OData.Client/ALinq/ResourceBinder.cs b/src/Microsoft.OData.Client/ALinq/ResourceBinder.cs index c99fc39a85..eba2781bdd 100644 --- a/src/Microsoft.OData.Client/ALinq/ResourceBinder.cs +++ b/src/Microsoft.OData.Client/ALinq/ResourceBinder.cs @@ -1712,7 +1712,7 @@ internal static bool MatchPropertyAccess(Expression e, DataServiceContext contex { instance = null; propertyPath = null; - uriVersion = Util.ODataVersion4; + uriVersion = context.MaxProtocolVersionAsVersion; MemberExpression me = StripTo(e); member = me; @@ -1774,7 +1774,7 @@ internal static bool MatchPropertyAccess(Expression e, DataServiceContext contex { instance = null; propertyPath = null; - uriVersion = Util.ODataVersion4; + uriVersion = context.MaxProtocolVersionAsVersion; MemberExpression me = StripTo(e); member = me; @@ -2946,7 +2946,7 @@ internal static void DisallowExpressionEndWithTypeAs(Expression exp, string meth internal static void ValidateExpandPath(Expression input, DataServiceContext context, out string expandPath, out Version uriVersion) { expandPath = null; - uriVersion = Util.ODataVersion4; + uriVersion = context.MaxProtocolVersionAsVersion; LambdaExpression le; if (PatternRules.MatchSingleArgumentLambda(input, out le)) { diff --git a/src/Microsoft.OData.Client/ALinq/SelectExpandPathBuilder.cs b/src/Microsoft.OData.Client/ALinq/SelectExpandPathBuilder.cs index 7fb628a5db..8de1a2e3ad 100644 --- a/src/Microsoft.OData.Client/ALinq/SelectExpandPathBuilder.cs +++ b/src/Microsoft.OData.Client/ALinq/SelectExpandPathBuilder.cs @@ -75,7 +75,6 @@ internal class SelectExpandPathBuilder public SelectExpandPathBuilder() { firstSegmentInNewPath = true; - this.uriVersion = Util.ODataVersion4; } /// @@ -107,7 +106,11 @@ public Version UriVersion { get { - return this.uriVersion; + return this.uriVersion ?? Util.ODataVersion4; + } + set + { + this.uriVersion = value; } } diff --git a/src/Microsoft.OData.Client/ALinq/UriWriter.cs b/src/Microsoft.OData.Client/ALinq/UriWriter.cs index 20ab6426b2..5bc8f2d566 100644 --- a/src/Microsoft.OData.Client/ALinq/UriWriter.cs +++ b/src/Microsoft.OData.Client/ALinq/UriWriter.cs @@ -51,7 +51,7 @@ private UriWriter(DataServiceContext context) { Debug.Assert(context != null, "context != null"); this.context = context; - this.uriVersion = Util.ODataVersion4; + this.uriVersion = context.MaxProtocolVersionAsVersion; } /// diff --git a/src/Microsoft.OData.Client/BaseSaveResult.cs b/src/Microsoft.OData.Client/BaseSaveResult.cs index 247c8c163b..e19f21da14 100644 --- a/src/Microsoft.OData.Client/BaseSaveResult.cs +++ b/src/Microsoft.OData.Client/BaseSaveResult.cs @@ -905,7 +905,7 @@ protected ODataRequestMessageWrapper CreateRequest(LinkDescriptor binding) HeaderCollection headers = new HeaderCollection(); - headers.SetRequestVersion(Util.ODataVersion4, this.RequestInfo.MaxProtocolVersionAsVersion); + headers.SetRequestVersion(this.RequestInfo.MaxProtocolVersionAsVersion, this.RequestInfo.MaxProtocolVersionAsVersion); this.RequestInfo.Format.SetRequestAcceptHeader(headers); // if (EntityStates.Deleted || (EntityState.Modifed && null == TargetResource)) @@ -936,7 +936,8 @@ protected ODataRequestMessageWrapper CreateRequest(EntityDescriptor entityDescri ClientEdmModel model = this.RequestInfo.Model; ClientTypeAnnotation clientType = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType())); - Version requestVersion = DetermineRequestVersion(clientType); + //Version requestVersion = DetermineRequestVersion(clientType); + Version requestVersion = this.RequestInfo.MaxProtocolVersionAsVersion; string httpMethod = this.GetHttpMethod(state, ref requestVersion); HeaderCollection headers = new HeaderCollection(); diff --git a/src/Microsoft.OData.Client/BatchSaveResult.cs b/src/Microsoft.OData.Client/BatchSaveResult.cs index f5fe794c94..98142e786b 100644 --- a/src/Microsoft.OData.Client/BatchSaveResult.cs +++ b/src/Microsoft.OData.Client/BatchSaveResult.cs @@ -298,7 +298,7 @@ private ODataRequestMessageWrapper CreateBatchRequest() { Uri requestUri = UriUtil.CreateUri(this.RequestInfo.BaseUriResolver.GetBaseUriWithSlash(), UriUtil.CreateUri("$batch", UriKind.Relative)); HeaderCollection headers = new HeaderCollection(); - headers.SetRequestVersion(Util.ODataVersion4, this.RequestInfo.MaxProtocolVersionAsVersion); + headers.SetRequestVersion(this.RequestInfo.MaxProtocolVersionAsVersion, this.RequestInfo.MaxProtocolVersionAsVersion); if (useJsonBatch) { headers.SetHeader(XmlConstants.HttpContentType, CreateApplicationJsonContentType()); diff --git a/src/Microsoft.OData.Client/DataServiceClientFormat.cs b/src/Microsoft.OData.Client/DataServiceClientFormat.cs index b13e37ecbc..2d36e7cf1b 100644 --- a/src/Microsoft.OData.Client/DataServiceClientFormat.cs +++ b/src/Microsoft.OData.Client/DataServiceClientFormat.cs @@ -326,8 +326,8 @@ private void SetRequestContentTypeHeader(HeaderCollection headers, string mediaT { if (mediaType == MimeApplicationJsonODataLight) { - // set the request version to 4.0 - headers.SetRequestVersion(Util.ODataVersion4, this.context.MaxProtocolVersionAsVersion); + // set the request version to 4.0 or 4.01 depending on what is set in tha DataServiceContext. + headers.SetRequestVersion(this.context.MaxProtocolVersionAsVersion, this.context.MaxProtocolVersionAsVersion); } headers.SetHeaderIfUnset(XmlConstants.HttpContentType, mediaType); diff --git a/src/Microsoft.OData.Client/DataServiceContext.cs b/src/Microsoft.OData.Client/DataServiceContext.cs index a54797afb2..00ba13b77e 100644 --- a/src/Microsoft.OData.Client/DataServiceContext.cs +++ b/src/Microsoft.OData.Client/DataServiceContext.cs @@ -3648,7 +3648,7 @@ private LoadPropertyResult CreateLoadPropertyRequest(object entity, string prope } } - requestVersion = Util.ODataVersion4; + requestVersion = this.MaxProtocolVersionAsVersion; HeaderCollection headers = new HeaderCollection(); @@ -3812,7 +3812,7 @@ private GetReadStreamResult CreateGetReadStreamResult( } else { - version = Util.ODataVersion4; + version = this.MaxProtocolVersionAsVersion; if (!entityDescriptor.TryGetNamedStreamInfo(name, out streamDescriptor)) { throw new ArgumentException(Strings.Context_EntityDoesNotContainNamedStream(name), nameof(name)); diff --git a/src/Microsoft.OData.Client/DataServiceRequest.cs b/src/Microsoft.OData.Client/DataServiceRequest.cs index 6788ebc089..00ddb53f8d 100644 --- a/src/Microsoft.OData.Client/DataServiceRequest.cs +++ b/src/Microsoft.OData.Client/DataServiceRequest.cs @@ -195,7 +195,7 @@ internal TElement GetValue(DataServiceContext context, Func().WithMessage("options", ComparisonMode.Substring); } #endif + [Fact] + public void DataServiceContextCanInitializev401WithoutException() + { + DataServiceContext dsContext = null; + Action test = () => dsContext = new DataServiceContext(new Uri("http://base.org/"), ODataProtocolVersion.V401); + test.ShouldNotThrow(); + dsContext.MaxProtocolVersion.ShouldBeEquivalentTo(ODataProtocolVersion.V401); + } [Fact] public void DataServiceContextDoesNotRestrictBaseUriScheme() diff --git a/test/FunctionalTests/Tests/DataServices/UnitTests/ClientUnitTests/PublicPlaces.vb b/test/FunctionalTests/Tests/DataServices/UnitTests/ClientUnitTests/PublicPlaces.vb index 5ed71037ba..caef26b9b8 100644 --- a/test/FunctionalTests/Tests/DataServices/UnitTests/ClientUnitTests/PublicPlaces.vb +++ b/test/FunctionalTests/Tests/DataServices/UnitTests/ClientUnitTests/PublicPlaces.vb @@ -321,7 +321,7 @@ Partial Public Class ClientModule New MaxProtocolVersionTestCase() { New MaxProtocolVersionTestCase() With {.MaxProtocolVersion = ODataProtocolVersion.V4}, New MaxProtocolVersionTestCase() With {.MaxProtocolVersion = CType(-1, ODataProtocolVersion), .ExpectedExceptionMessage = "Specified argument was out of the range of valid values." & vbCrLf & "Parameter name: maxProtocolVersion"}, - New MaxProtocolVersionTestCase() With {.MaxProtocolVersion = CType(ODataProtocolVersion.V4 + 1, ODataProtocolVersion), .ExpectedExceptionMessage = "Specified argument was out of the range of valid values." & vbCrLf & "Parameter name: maxProtocolVersion"} + New MaxProtocolVersionTestCase() With {.MaxProtocolVersion = CType(ODataProtocolVersion.V4 + 1, ODataProtocolVersion)} }, Sub(testCase) Dim context = New DataServiceContext(New Uri("http://host/service"))