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

[ODL 8]: Merge SimplifiedODataOptions into relevant reader, writer and URI parser settings classes. #2872

Merged
merged 12 commits into from
Mar 6, 2024
2 changes: 0 additions & 2 deletions src/Microsoft.OData.Core/ContainerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ public static IContainerBuilder AddDefaultODataServices(this IContainerBuilder b
builder.AddService(ServiceLifetime.Singleton, sp => ODataUriResolver.GetUriResolver(null));
builder.AddService<ODataUriParserSettings>(ServiceLifetime.Scoped);
builder.AddService<UriPathParser>(ServiceLifetime.Scoped);
builder.AddServicePrototype(new ODataSimplifiedOptions(odataVersion));
builder.AddService(ServiceLifetime.Scoped, sp => sp.GetServicePrototype<ODataSimplifiedOptions>().Clone());

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ internal bool OptionalODataPrefix
{
if (this.MessageReaderSettings.Version == ODataVersion.V4)
{
return this.ODataSimplifiedOptions.EnableReadingODataAnnotationWithoutPrefix;
return this.MessageReaderSettings.EnableReadingODataAnnotationWithoutPrefix;
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ internal bool OmitODataPrefix
{
get
{
return this.ODataSimplifiedOptions.GetOmitODataPrefix(this.MessageWriterSettings.Version ?? ODataVersion.V4);
return this.MessageWriterSettings.GetOmitODataPrefix(this.MessageWriterSettings.Version ?? ODataVersion.V4);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OData.Core/JsonLight/ODataJsonLightReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ private bool ReadAtResourceStartImplementationSynchronously()
ODataResourceMetadataBuilder builder =
this.jsonLightResourceDeserializer.MetadataContext.GetResourceMetadataBuilderForReader(
this.CurrentResourceState,
this.jsonLightInputContext.ODataSimplifiedOptions.EnableReadingKeyAsSegment,
this.jsonLightInputContext.MessageReaderSettings.EnableReadingKeyAsSegment,
this.ReadingDelta);
if (builder != currentResource.MetadataBuilder)
{
Expand Down Expand Up @@ -2343,7 +2343,7 @@ private void StartNestedResourceInfo(ODataJsonLightReaderNestedResourceInfo read
ODataResourceMetadataBuilder resourceMetadataBuilder =
this.jsonLightResourceDeserializer.MetadataContext.GetResourceMetadataBuilderForReader(
this.CurrentResourceState,
this.jsonLightInputContext.ODataSimplifiedOptions.EnableReadingKeyAsSegment,
this.jsonLightInputContext.MessageReaderSettings.EnableReadingKeyAsSegment,
this.ReadingDelta);
nestedResourceInfo.MetadataBuilder = resourceMetadataBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ private void SetEntryMediaResource(IODataJsonLightReaderResourceState resourceSt

ODataResourceMetadataBuilder builder =
this.MetadataContext.GetResourceMetadataBuilderForReader(resourceState,
this.JsonLightInputContext.ODataSimplifiedOptions.EnableReadingKeyAsSegment,
this.JsonLightInputContext.MessageReaderSettings.EnableReadingKeyAsSegment,
/*isDelta*/ false);
mediaResource.SetMetadataBuilder(builder, /*propertyName*/ null);
resource.MediaResource = mediaResource;
Expand Down Expand Up @@ -1786,7 +1786,7 @@ private ODataStreamReferenceValue ReadStreamPropertyValue(IODataJsonLightReaderR
ReadStreamInfo(streamReferenceValue, resourceState, streamPropertyName);
ODataResourceMetadataBuilder builder =
this.MetadataContext.GetResourceMetadataBuilderForReader(resourceState,
this.JsonLightInputContext.ODataSimplifiedOptions.EnableReadingKeyAsSegment,
this.JsonLightInputContext.MessageReaderSettings.EnableReadingKeyAsSegment,
/*isDelta*/ false);

// Note that we set the metadata builder even when streamProperty is null, which is the case when the stream property is undeclared.
Expand All @@ -1812,7 +1812,7 @@ private ODataStreamPropertyInfo ReadStreamPropertyInfo(IODataJsonLightReaderReso
ReadStreamInfo(streamInfo, resourceState, streamPropertyName);
ODataResourceMetadataBuilder builder =
this.MetadataContext.GetResourceMetadataBuilderForReader(resourceState,
this.JsonLightInputContext.ODataSimplifiedOptions.EnableReadingKeyAsSegment,
this.JsonLightInputContext.MessageReaderSettings.EnableReadingKeyAsSegment,
/*isDelta*/ false);

// Note that we set the metadata builder even when streamProperty is null, which is the case when the stream property is undeclared.
Expand Down Expand Up @@ -2060,7 +2060,7 @@ private void SetMetadataBuilder(IODataJsonLightReaderResourceState resourceState
{
ODataResourceMetadataBuilder builder =
this.MetadataContext.GetResourceMetadataBuilderForReader(resourceState,
this.JsonLightInputContext.ODataSimplifiedOptions.EnableReadingKeyAsSegment,
this.JsonLightInputContext.MessageReaderSettings.EnableReadingKeyAsSegment,
/*isDelta*/ false);
operation.SetMetadataBuilder(builder, this.ContextUriParseResult.MetadataDocumentUri);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OData.Core/JsonLight/ODataJsonLightWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ protected override void PrepareDeletedResourceForWriteStart(DeletedResourceScope

ODataConventionalUriBuilder uriBuilder = new ODataConventionalUriBuilder(
new Uri(this.messageWriterSettings.MetadataDocumentUri, "./"),
this.jsonLightOutputContext.ODataSimplifiedOptions.EnableWritingKeyAsSegment ? ODataUrlKeyDelimiter.Slash : ODataUrlKeyDelimiter.Parentheses
this.jsonLightOutputContext.MessageWriterSettings.EnableWritingKeyAsSegment ? ODataUrlKeyDelimiter.Slash : ODataUrlKeyDelimiter.Parentheses
);

Uri uri = uriBuilder.BuildBaseUri();
Expand Down Expand Up @@ -2331,7 +2331,7 @@ private void InnerPrepareResourceForWriteStart(ODataResourceBase resource, OData
resourceType,
selectedProperties,
this.writingResponse,
this.jsonLightOutputContext.ODataSimplifiedOptions.EnableWritingKeyAsSegment,
this.jsonLightOutputContext.MessageWriterSettings.EnableWritingKeyAsSegment,
uri,
this.messageWriterSettings);

Expand Down
17 changes: 0 additions & 17 deletions src/Microsoft.OData.Core/ODataInputContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ public abstract class ODataInputContext : IDisposable
/// <summary>The payload value converter to use.</summary>
private readonly ODataPayloadValueConverter payloadValueConverter;

/// <summary>
/// The ODataSimplifiedOptions used in reader.
/// </summary>
private readonly ODataSimplifiedOptions odataSimplifiedOptions;

/// <summary>Set to true if the input was disposed.</summary>
private bool disposed;

Expand Down Expand Up @@ -82,7 +77,6 @@ protected ODataInputContext(
this.container = messageInfo.Container;
this.edmTypeResolver = new EdmTypeReaderResolver(this.Model, this.MessageReaderSettings.ClientCustomTypeResolver);
this.payloadValueConverter = ODataPayloadValueConverter.GetPayloadValueConverter(this.container);
this.odataSimplifiedOptions = ODataSimplifiedOptions.GetODataSimplifiedOptions(this.container, messageReaderSettings.Version);
}

/// <summary>
Expand Down Expand Up @@ -173,17 +167,6 @@ internal ODataPayloadValueConverter PayloadValueConverter
}
}

/// <summary>
/// The ODataSimplifiedOptions used in reader.
/// </summary>
internal ODataSimplifiedOptions ODataSimplifiedOptions
{
get
{
return this.odataSimplifiedOptions;
}
}

/// <summary>
/// IDisposable.Dispose() implementation to cleanup unmanaged resources of the context.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions src/Microsoft.OData.Core/ODataMessageReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ public ODataMessageReaderSettings(ODataVersion odataVersion)
Validations = ValidationKinds.All;
this.ReadUntypedAsString = true;
this.MaxProtocolVersion = ODataConstants.ODataDefaultProtocolVersion;
this.EnableReadingODataAnnotationWithoutPrefix = false;
}
else
{
Validations = ValidationKinds.All & ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType;
this.ReadUntypedAsString = false;
this.MaxProtocolVersion = odataVersion;
this.EnableReadingODataAnnotationWithoutPrefix = true;
}
}

Expand Down Expand Up @@ -235,6 +237,21 @@ public ODataMessageQuotas MessageQuotas
/// </summary>
internal bool ThrowOnUndeclaredPropertyForNonOpenType { get; private set; }

/// <summary>
/// Gets or sets a value that indicates whether the reader should put key values in their own URI segment when automatically building URIs.
/// If this value is false, automatically-generated URLs will take the form "../EntitySet('KeyValue')/..".
/// If this value is true, automatically-generated URLs will take the form "../EntitySet/KeyValue/..".
/// This setting only applies to URLs that are automatically generated by the <see cref="ODataMessageReader" />. URLs explicitly provided by the server won't be modified.
/// </summary>
public bool EnableReadingKeyAsSegment { get; set; }

/// <summary>
/// true to support reading OData control information name without prefix 'odata.', otherwise false.
/// The default value is false for OData 4.0 and true for OData 4.01.
/// This setting is applied during deserialization.
/// </summary>
public bool EnableReadingODataAnnotationWithoutPrefix { get; set; }

/// <summary>
/// Creates a shallow copy of this <see cref="ODataMessageReaderSettings"/>.
/// </summary>
Expand Down Expand Up @@ -302,6 +319,8 @@ private void CopyFrom(ODataMessageReaderSettings other)
this.ArrayPool = other.ArrayPool;
this.EnablePropertyNameCaseInsensitive = other.EnablePropertyNameCaseInsensitive;
this.EnableUntypedCollections = other.EnableUntypedCollections;
this.EnableReadingKeyAsSegment = other.EnableReadingKeyAsSegment;
this.EnableReadingODataAnnotationWithoutPrefix = other.EnableReadingODataAnnotationWithoutPrefix;
}
}
}
108 changes: 107 additions & 1 deletion src/Microsoft.OData.Core/ODataMessageWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,33 @@ public sealed class ODataMessageWriterSettings
/// </summary>
private ValidationKinds validations;

/// <summary>Initializes a new instance of the <see cref="Microsoft.OData.ODataMessageWriterSettings" /> class with default settings. </summary>
/// <summary>
/// Default setting for writing control information without the 'odata' prefix.
/// </summary>
private bool enableWritingODataAnnotationWithoutPrefix;

/// <summary>
/// OData 4.0-specific setting for writing control information without the 'odata' prefix.
/// </summary>
private bool omitODataPrefix40 = false;

/// <summary>
/// OData 4.01 and greater setting for writing control information without the 'odata' prefix.
/// </summary>
private bool omitODataPrefix = true;

/// <summary>Initializes a new instance of the <see cref="Microsoft.OData.ODataMessageWriterSettings" /> class with default settings.</summary>
public ODataMessageWriterSettings()
: this(null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Microsoft.OData.ODataMessageWriterSettings" /> class with default settings for the
/// specified OData version.
/// </summary>
/// <param name="version">OData version for which to create default settings.</param>
public ODataMessageWriterSettings(ODataVersion? version)
{
this.EnableMessageStreamDisposal = true;
this.EnableCharactersCheck = false;
Expand All @@ -80,6 +105,17 @@ public ODataMessageWriterSettings()
this.MultipartNewLine = "\r\n";
this.AlwaysAddTypeAnnotationsForDerivedTypes = false;
this.BufferSize = ODataConstants.DefaultOutputBufferSize;
this.EnableWritingKeyAsSegment = false;
this.Version = version;

if (version == null || version < ODataVersion.V401)
{
this.enableWritingODataAnnotationWithoutPrefix = this.omitODataPrefix40;
}
else
{
this.enableWritingODataAnnotationWithoutPrefix = this.omitODataPrefix;
}
}

/// <summary>
Expand Down Expand Up @@ -346,6 +382,72 @@ internal Func<string, bool> ShouldIncludeAnnotation
}
}

/// <summary>
/// Gets or sets a value that indicates whether the writer should put key values in their own URI segment when automatically building URIs.
/// If this value is false, automatically-generated URLs will take the form "../EntitySet('KeyValue')/..".
/// If this value is true, automatically-generated URLs will take the form "../EntitySet/KeyValue/..".
/// This setting only applies to URLs that are automatically generated by the <see cref="ODataMessageWriter" /> and does not modify URLs explicitly provided by the user.
/// </summary>
public bool EnableWritingKeyAsSegment { get; set; }

/// <summary>
/// Get whether to write OData control information without the 'odata' prefix.
/// The default value is false for OData 4.0 and true for OData 4.01.
/// </summary>
/// <returns>true if control information should be written with the 'odata' prefix, otherwise false.</returns>
public bool GetOmitODataPrefix()
{
return this.enableWritingODataAnnotationWithoutPrefix;
}

/// <summary>
/// Returns a value indicating whether control information should be written with the 'odata' prefix for the specified OData version.
/// The default value is false for OData 4.0 and true for OData 4.01.
/// </summary>
/// <param name="version">The OData version.</param>
/// <returns>true if control information should be written with the 'odata' prefix, otherwise false</returns>
public bool GetOmitODataPrefix(ODataVersion version)
{
if (version >= ODataVersion.V401)
{
return this.omitODataPrefix;
}

return this.omitODataPrefix40;
}

/// <summary>
/// Sets a value indicating whether control information should be written with or without the 'odata' prefix.
/// </summary>
/// <remarks>
/// This method updates the setting for both OData version 4.0 and 4.01. If you want to target a specific version instead,
/// use the <see cref="SetOmitODataPrefix(bool, ODataVersion)"/> overload.
/// </remarks>
/// <param name="value">true to write control information with the 'odata' prefix, otherwise false.</param>
public void SetOmitODataPrefix(bool value)
{
this.enableWritingODataAnnotationWithoutPrefix =
this.omitODataPrefix =
this.omitODataPrefix40 = value;
}

/// <summary>
/// Sets a value indicating whether control information should be written with or without the 'odata' prefix for the specified OData version
/// </summary>
/// <param name="value">true to write control information with the 'odata' prefix, otherwise false.</param>
/// <param name="version">The OData version for which to set the omit prefix behavior.</param>
public void SetOmitODataPrefix(bool value, ODataVersion version)
{
if (version == ODataVersion.V4)
{
this.omitODataPrefix40 = value;
}
else
{
this.omitODataPrefix = value;
}
}

/// <summary>
/// Creates a shallow copy of this <see cref="ODataMessageWriterSettings"/>.
/// </summary>
Expand Down Expand Up @@ -456,6 +558,10 @@ private void CopyFrom(ODataMessageWriterSettings other)
this.ThrowOnUndeclaredPropertyForNonOpenType = other.ThrowOnUndeclaredPropertyForNonOpenType;
this.ArrayPool = other.ArrayPool;
this.BufferSize = other.BufferSize;
this.EnableWritingKeyAsSegment = other.EnableWritingKeyAsSegment;
this.enableWritingODataAnnotationWithoutPrefix = other.enableWritingODataAnnotationWithoutPrefix;
this.omitODataPrefix40 = other.omitODataPrefix40;
this.omitODataPrefix = other.omitODataPrefix;
}
}
}
17 changes: 0 additions & 17 deletions src/Microsoft.OData.Core/ODataOutputContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ public abstract class ODataOutputContext : IDisposable
/// <summary>The writer validator used in writing.</summary>
private readonly IWriterValidator writerValidator;

/// <summary>
/// The simplified options used in writing.
/// </summary>
private readonly ODataSimplifiedOptions odataSimplifiedOptions;

/// <summary>
/// Constructor.
/// </summary>
Expand All @@ -84,7 +79,6 @@ protected ODataOutputContext(
this.edmTypeResolver = EdmTypeWriterResolver.Instance;
this.payloadValueConverter = ODataPayloadValueConverter.GetPayloadValueConverter(this.container);
this.writerValidator = messageWriterSettings.Validator;
this.odataSimplifiedOptions = ODataSimplifiedOptions.GetODataSimplifiedOptions(this.container, messageWriterSettings.Version);
}

/// <summary>
Expand Down Expand Up @@ -187,17 +181,6 @@ internal IWriterValidator WriterValidator
}
}

/// <summary>
/// The ODataSimplifiedOptions used in writing
/// </summary>
internal ODataSimplifiedOptions ODataSimplifiedOptions
{
get
{
return this.odataSimplifiedOptions;
}
}

/// <summary>
/// IDisposable.Dispose() implementation to cleanup unmanaged resources of the context.
/// </summary>
Expand Down
Loading