Skip to content
Merged
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
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net461.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ protected RequestContent() { }
public static Azure.Core.RequestContent Create(byte[] bytes) { throw null; }
public static Azure.Core.RequestContent Create(byte[] bytes, int index, int length) { throw null; }
public static Azure.Core.RequestContent Create(System.IO.Stream stream) { throw null; }
public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IJsonModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
public static Azure.Core.RequestContent Create(object serializable) { throw null; }
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.JsonPropertyNames propertyNameFormat, string dateTimeFormat = "o") { throw null; }
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.ObjectSerializer? serializer) { throw null; }
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net472.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ protected RequestContent() { }
public static Azure.Core.RequestContent Create(byte[] bytes) { throw null; }
public static Azure.Core.RequestContent Create(byte[] bytes, int index, int length) { throw null; }
public static Azure.Core.RequestContent Create(System.IO.Stream stream) { throw null; }
public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IJsonModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
public static Azure.Core.RequestContent Create(object serializable) { throw null; }
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.JsonPropertyNames propertyNameFormat, string dateTimeFormat = "o") { throw null; }
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.ObjectSerializer? serializer) { throw null; }
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net5.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ protected RequestContent() { }
public static Azure.Core.RequestContent Create(byte[] bytes) { throw null; }
public static Azure.Core.RequestContent Create(byte[] bytes, int index, int length) { throw null; }
public static Azure.Core.RequestContent Create(System.IO.Stream stream) { throw null; }
public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IJsonModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
public static Azure.Core.RequestContent Create(object serializable) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net6.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ protected RequestContent() { }
public static Azure.Core.RequestContent Create(byte[] bytes) { throw null; }
public static Azure.Core.RequestContent Create(byte[] bytes, int index, int length) { throw null; }
public static Azure.Core.RequestContent Create(System.IO.Stream stream) { throw null; }
public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IJsonModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
public static Azure.Core.RequestContent Create(object serializable) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ protected RequestContent() { }
public static Azure.Core.RequestContent Create(byte[] bytes) { throw null; }
public static Azure.Core.RequestContent Create(byte[] bytes, int index, int length) { throw null; }
public static Azure.Core.RequestContent Create(System.IO.Stream stream) { throw null; }
public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IJsonModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
public static Azure.Core.RequestContent Create(System.Net.ClientModel.Core.IModel<object> model, System.Net.ClientModel.ModelReaderWriterOptions? options = null) { throw null; }
public static Azure.Core.RequestContent Create(object serializable) { throw null; }
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.JsonPropertyNames propertyNameFormat, string dateTimeFormat = "o") { throw null; }
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.ObjectSerializer? serializer) { throw null; }
Expand Down
48 changes: 47 additions & 1 deletion sdk/core/Azure.Core/src/RequestContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net.ClientModel;
using System.Net.ClientModel.Core;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -82,6 +83,24 @@ public abstract class RequestContent : PipelineContent
/// <returns>An instance of <see cref="RequestContent"/> that wraps a <see cref="DynamicData"/>.</returns>
public static RequestContent Create(DynamicData content) => new DynamicDataContent(content);

/// <summary>
/// Creates an instance of <see cref="RequestContent"/> that wraps a <see cref="IModel{T}"/>.
/// </summary>
/// <param name="model">The <see cref="IModel{T}"/> to write.</param>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <returns>An instance of <see cref="RequestContent"/> that wraps a a <see cref="IModel{T}"/>.</returns>
public static RequestContent Create(IModel<object> model, ModelReaderWriterOptions? options = default)
=> new PipelineContentContent(CreateContent(model, options ?? ModelReaderWriterOptions.DefaultWireOptions));

/// <summary>
/// Creates an instance of <see cref="RequestContent"/> that wraps a <see cref="IJsonModel{T}"/>.
/// </summary>
/// <param name="model">The <see cref="IJsonModel{T}"/> to write.</param>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <returns>An instance of <see cref="RequestContent"/> that wraps a <see cref="IJsonModel{T}"/>.</returns>
public static RequestContent Create(IJsonModel<object> model, ModelReaderWriterOptions? options = default)
=> new PipelineContentContent(CreateContent(model, options ?? ModelReaderWriterOptions.DefaultWireOptions));

/// <summary>
/// Creates an instance of <see cref="RequestContent"/> that wraps a serialized version of an object.
/// </summary>
Expand Down Expand Up @@ -140,6 +159,34 @@ public static RequestContent Create(object serializable, JsonPropertyNames prope
/// <param name="content">The <see cref="DynamicData"/> to use.</param>
public static implicit operator RequestContent(DynamicData content) => Create(content);

private sealed class PipelineContentContent : RequestContent
{
private readonly PipelineContent _content;
public PipelineContentContent(PipelineContent content)
{
_content = content;
}

public override void Dispose()
{
_content?.Dispose();
}

public override bool TryComputeLength(out long length)
{
return _content.TryComputeLength(out length);
}

public override void WriteTo(Stream stream, CancellationToken cancellationToken)
{
_content.WriteTo(stream, cancellationToken);
}
public override async Task WriteToAsync(Stream stream, CancellationToken cancellationToken)
{
await _content.WriteToAsync(stream, cancellationToken).ConfigureAwait(false);
}
}

private sealed class StreamContent : RequestContent
{
private const int CopyToBufferSize = 81920;
Expand Down Expand Up @@ -203,7 +250,6 @@ public override void Dispose()
_stream.Dispose();
}
}

private sealed class ArrayContent : RequestContent
{
private readonly byte[] _bytes;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Net.ClientModel;
using System.Net.ClientModel.Core;
using System.Reflection;
using Azure.Core.Tests.ModelReaderWriterTests.Models;
using NUnit.Framework;

namespace Azure.Core.Tests.ModelReaderWriterTests
{
public class ModelReaderWriterRequestContentTests
{
private const string json = "{\"kind\":\"X\",\"name\":\"Name\",\"xProperty\":100}";
private ModelX _modelX;

[OneTimeSetUp]
public void OneTimeSetUp()
{
_modelX = ModelReaderWriter.Read<ModelX>(BinaryData.FromString(json));
}

[Test]
public void CanCalculateLength()
{
//use IModelSerializable
var content = RequestContent.Create((IModel<ModelX>)_modelX);
AssertContentType(content, "ModelWriterContent");
content.TryComputeLength(out long lengthNonJson);
Assert.Greater(lengthNonJson, 0);

//use IModelJsonSerializable
var jsonContent = RequestContent.Create((IJsonModel<ModelX>)_modelX);
AssertContentType(jsonContent, "JsonModelWriterContent");
content.TryComputeLength(out long lengthJson);
Assert.Greater(lengthJson, 0);

Assert.AreEqual(lengthNonJson, lengthJson);

//use default
jsonContent = RequestContent.Create(_modelX);
AssertContentType(jsonContent, "JsonModelWriterContent");
content.TryComputeLength(out lengthJson);
Assert.Greater(lengthJson, 0);

Assert.AreEqual(lengthNonJson, lengthJson);
}

private static void AssertContentType(RequestContent content, string expectedContent)
{
Assert.AreEqual("PipelineContentContent", content.GetType().Name);
var field = content.GetType().GetField("_content", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.IsNotNull(field);
Assert.AreEqual(expectedContent, field.GetValue(content).GetType().Name);
}

[Test]
public void ValidatePrivateClassType()
{
IModel<ModelX> modelX = _modelX;

RequestContent content = RequestContent.Create(modelX);
AssertContentType(content, "ModelWriterContent");
}
}
}
33 changes: 33 additions & 0 deletions sdk/core/Azure.Core/tests/common/Azure.Core.Tests.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,38 @@
<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)ArrayBufferWriter.cs" LinkBase="Shared/Core" />
</ItemGroup>

<ItemGroup>
<None Update="TestData\AvailabilitySetData\AvailabilitySetDataWithVMs.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\AvailabilitySetData\AvailabilitySetData.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\AvailabilitySetData\AvailabilitySetDataWithVMsWireFormat.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\AvailabilitySetData\AvailabilitySetDataWireFormat.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\ModelXml.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\ModelXmlX.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\ResourceProviderData\ResourceProviderData-Collapsed.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\ResourceProviderData\ResourceProviderData.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\ModelX\ModelX.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\ModelX\ModelXWireFormat.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading