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

TestV2 for patch model purpose #7

Draft
wants to merge 5 commits into
base: core-mergepatch-proto2
Choose a base branch
from
Draft
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 @@ -4,3 +4,6 @@
using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Usage", "AZC0001:Use one of the following pre-approved namespace groups (https://azure.github.io/azure-sdk/registered_namespaces.html): Azure.AI, Azure.Analytics, Azure.Communication, Azure.Data, Azure.DigitalTwins, Azure.IoT, Azure.Learn, Azure.Media, Azure.Management, Azure.Messaging, Azure.ResourceManager, Azure.Search, Azure.Security, Azure.Storage, Azure.Template, Azure.Identity, Microsoft.Extensions.Azure", Justification = "<Pending>", Scope = "namespace", Target = "~N:Azure.Developer.LoadTesting")]
[assembly: SuppressMessage("Usage", "AZC0004:DO provide both asynchronous and synchronous variants for all service methods.", Justification = "<Pending>")]
[assembly: SuppressMessage("Usage", "AZC0007:DO provide a minimal constructor that takes only the parameters required to connect to the service.", Justification = "<Pending>", Scope = "member", Target = "~M:Azure.Developer.LoadTesting.TestV2Client.#ctor")]
[assembly: SuppressMessage("Usage", "AZC0002:DO ensure all service methods, both asynchronous and synchronous, take an optional CancellationToken parameter called cancellationToken.", Justification = "<Pending>", Scope = "member", Target = "~M:Azure.Developer.LoadTesting.TestV2Client.GetTestV2Async(System.String,Azure.RequestContext)~System.Threading.Tasks.Task{Azure.Response}")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Text.Json;
using System.Xml.Linq;
using Azure.Core;
using Azure.Core.Json;

namespace Azure.Developer.LoadTesting.Models
{
public partial class RoundTripModel : IUtf8JsonSerializable
{
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
{
_element.WriteTo(writer, 'P'); // TO-DO: we need another option not calling 'P' but its logic is the same as 'P'
}

/// <summary> Deserializes the model from a raw response. </summary>
/// <param name="response"> The response to deserialize the model from. </param>
internal static RoundTripModel FromResponse(Response response)
{
MutableJsonDocument jsonDocument = MutableJsonDocument.Parse(response.Content);
return new RoundTripModel(jsonDocument.RootElement);
}

/// <summary> Convert into a Utf8JsonRequestContent. </summary>
internal RequestContent ToRequestContent()
{
var content = new Utf8JsonRequestContent();
content.JsonWriter.WriteObjectValue(this);
return content;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Xml.Linq;
using Azure.Core;
using Azure.Core.Json;

namespace Azure.Developer.LoadTesting.Models
{
/// <summary>
/// model RoundTripModel {
/// requiredString: string;
/// requiredNullableInt: int32 | null;
/// requiredNullableString: string | null;
/// nonRequiredNullableInt?: int32 | null;
/// nonRequiredNullableString?: string | null;
/// }
/// </summary>
public partial class RoundTripModel
{
private MutableJsonElement _element;

/// <summary>
/// Initializes a new instance of the <see cref="RoundTripModel"/> class.
/// </summary>
public RoundTripModel(string requiredString, int? requiredNullableInt, string requiredNullableString)
{
Argument.AssertNotNull(requiredString, nameof(requiredString));

BinaryData utf8Json;
using (MemoryStream stream = new())
{
using Utf8JsonWriter writer = new Utf8JsonWriter(stream);
writer.WriteStartObject();
writer.WritePropertyName("requiredString");
writer.WriteStringValue(requiredString);
if (requiredNullableInt != null)
{
writer.WritePropertyName("requiredNullableInt");
writer.WriteNumberValue(requiredNullableInt.Value);
}
else
{
writer.WriteNull("requiredNullableInt");
}
if (requiredNullableString != null)
{
writer.WritePropertyName("requiredNullableString");
writer.WriteStringValue(requiredNullableString);
}
else
{
writer.WriteNull("requiredNullableString");
}
writer.WriteEndObject();
writer.Flush();
stream.Position = 0;
utf8Json = BinaryData.FromStream(stream);
}
_element = MutableJsonDocument.Parse(utf8Json).RootElement;
}

internal RoundTripModel(MutableJsonElement element)
{
_element = element;
}

/// <summary> Required string, illustrating a reference type property. </summary>
public string RequiredString
{
get
{
if (_element.TryGetProperty("requiredString", out MutableJsonElement value))
{
return value.GetString();
}
return default;
}
set => _element.SetProperty("requiredString", value);
}

/// <summary> Required nullable int. </summary>
public int? RequiredNullableInt
{
get
{
if (_element.TryGetProperty("requiredNullableInt", out MutableJsonElement value))
{
return value.GetInt32();
}
return default;
}
set => _element.SetProperty("requiredNullableInt", value);
}

/// <summary> Required nullable string. </summary>
public string RequiredNullableString
{
get
{
if (_element.TryGetProperty("requiredNullableString", out MutableJsonElement value))
{
return value.GetString();
}
return default;
}
set => _element.SetProperty("requiredNullableString", value);
}

/// <summary> Optional nullable int. </summary>
public int? NonRequiredNullableInt
{
get
{
if (_element.TryGetProperty("nonRequiredNullableInt", out MutableJsonElement value))
{
return value.GetInt32();
}
return null;
}
set => _element.SetProperty("nonRequiredNullableInt", value);
}

/// <summary> Optional nullable string. </summary>
public string NonRequiredNullableString
{
get
{
if (_element.TryGetProperty("nonRequiredNullableString", out MutableJsonElement value))
{
return value.GetString();
}
return null;
}
set => _element.SetProperty("nonRequiredNullableString", value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

using Azure.Core.Serialization;
using Azure.Core;
using Azure.Core.Json;
using System.Text.Json;
using System;

namespace Azure.Developer.LoadTesting.Models
{
public partial class TestV2 : IUtf8JsonSerializable, IJsonModelSerializable
{
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WritePropertyName("key"u8);
writer.WriteStringValue(Key);
writer.WritePropertyName("requiredProperty"u8);
writer.WriteNumberValue(RequiredProperty);
if (Optional.IsDefined(OptionalProperty))
{
writer.WritePropertyName("optionalProperty"u8);
writer.WriteStringValue(OptionalProperty);
}
writer.WriteEndObject();
}

void IJsonModelSerializable.Serialize(Utf8JsonWriter writer, ModelSerializerOptions options)
{
((IUtf8JsonSerializable)this).Write(writer);
}

object IJsonModelSerializable.Deserialize(ref Utf8JsonReader reader, ModelSerializerOptions options)
{
JsonDocument doc = JsonDocument.ParseValue(ref reader);
MutableJsonDocument mdoc = new MutableJsonDocument(doc, new JsonSerializerOptions());
return new TestV2(mdoc.RootElement);
}

object IModelSerializable.Deserialize(BinaryData data, ModelSerializerOptions options)
{
MutableJsonDocument jsonDocument = MutableJsonDocument.Parse(data);
return new TestV2(jsonDocument.RootElement);
}

BinaryData IModelSerializable.Serialize(ModelSerializerOptions options) => ModelSerializerHelper.SerializeToBinaryData(writer => ((IJsonModelSerializable)this).Serialize(writer, options));

internal virtual RequestContent ToRequestContent()
{
var content = new Utf8JsonRequestContent();
content.JsonWriter.WriteObjectValue(this);
return content;
}

internal static TestV2 FromResponse(Response response)
{
MutableJsonDocument jsonDocument = MutableJsonDocument.Parse(response.Content);
return new TestV2(jsonDocument.RootElement);
}
}
}
109 changes: 109 additions & 0 deletions sdk/loadtestservice/Azure.Developer.LoadTesting/src/Models/TestV2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable
using System.IO;
using System.Text.Json;
using System;
using Azure.Core.Json;
using Azure.Core;
using System.Collections.Generic;

namespace Azure.Developer.LoadTesting.Models
{
/// <summary>
/// model TestV2 {
/// @key
/// key: string; // Because it has @key so there will be a @path implictly added to it.
///
/// @visibility("read")
/// readOnlyProperty: string; // Add this propery to differentiate request from response.
/// optionalProperty?: string;
/// requiredProperty: int32;
Copy link
Owner

Choose a reason for hiding this comment

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

We should understand why this property is required for GET and PUT but not for PATCH. I don't think we have a good foundation for this question yet.

/// }
/// </summary>
public partial class TestV2
{
private MutableJsonElement _element;

/// <summary> </summary>
public TestV2(string key, int required)
{
BinaryData utf8Json;
using (MemoryStream stream = new())
{
using Utf8JsonWriter writer = new Utf8JsonWriter(stream);
writer.WriteStartObject();
writer.WritePropertyName("key");
writer.WriteStringValue(key.AsSpan());
writer.WritePropertyName("requiredProperty");
writer.WriteNumberValue(required);
writer.WriteEndObject();
writer.Flush();
stream.Position = 0;
utf8Json = BinaryData.FromStream(stream);
}
_element = MutableJsonDocument.Parse(utf8Json).RootElement;
}

internal TestV2(MutableJsonElement element)
{
_element = element;
}

/// <summary></summary>
public string Key
{
get
{
if (_element.TryGetProperty("key", out MutableJsonElement value))
{
return value.GetString();
}
return default;
}
set => _element.SetProperty("key", value);
}

/// <summary> </summary>
public int RequiredProperty
{
get
{
if (_element.TryGetProperty("requiredProperty", out MutableJsonElement value))
{
return value.GetInt32();
}
return default;
}
set => _element.SetProperty("requiredProperty", value);
}

/// <summary> </summary>
public string OptionalProperty
{
get
{
if (_element.TryGetProperty("optionalProperty", out MutableJsonElement value))
{
return value.GetString();
}
return default;
}
set => _element.SetProperty("optionalProperty", value);
}

/// <summary> </summary>
public string ReadOnlyProperty
{
get
{
if (_element.TryGetProperty("readOnlyProperty", out MutableJsonElement value))
{
return value.GetString();
}
return default;
}
}
}
}
Loading