From 53d2e5776965fedb9f92a344c4c9d48f45e14d00 Mon Sep 17 00:00:00 2001 From: Dapeng Zhang Date: Tue, 8 Oct 2024 10:30:00 +0800 Subject: [PATCH] change the default of use-write-core to true for typespec mgmt specs (#5093) * change the default of use-write-core to true for mgmt and typespec * fix format * regen --- .../Emitter.Csharp/src/emitter.ts | 4 +- .../Emitter.Csharp/src/options.ts | 2 +- .../managed-identity/Configuration.json | 3 +- ...entityTrackedResourceData.Serialization.cs | 60 ++++--------------- ...TrackedResourceProperties.Serialization.cs | 11 +++- .../models/resources/Configuration.json | 3 +- ...edProxyResourceListResult.Serialization.cs | 11 +++- ...edProxyResourceProperties.Serialization.cs | 11 +++- .../NotificationDetails.Serialization.cs | 11 +++- ...TrackedResourceListResult.Serialization.cs | 11 +++- ...TrackedResourceProperties.Serialization.cs | 11 +++- ...TrackedResourceListResult.Serialization.cs | 11 +++- ...TrackedResourceProperties.Serialization.cs | 11 +++- .../NestedProxyResourceData.Serialization.cs | 47 ++++----------- ...gletonTrackedResourceData.Serialization.cs | 60 ++++--------------- ...pLevelTrackedResourceData.Serialization.cs | 60 ++++--------------- .../MgmtTypeSpec/Configuration.json | 3 +- .../src/Generated/FooData.Serialization.cs | 60 ++++--------------- .../Models/FooListResult.Serialization.cs | 11 +++- .../Models/FooProperties.Serialization.cs | 11 +++- ...ecPrivateLinkResourceData.Serialization.cs | 47 ++++----------- ...ateLinkResourceListResult.Serialization.cs | 11 +++- ...ateLinkResourceProperties.Serialization.cs | 11 +++- 23 files changed, 178 insertions(+), 303 deletions(-) diff --git a/src/TypeSpec.Extension/Emitter.Csharp/src/emitter.ts b/src/TypeSpec.Extension/Emitter.Csharp/src/emitter.ts index d0d7afccbf2..57c5a971862 100644 --- a/src/TypeSpec.Extension/Emitter.Csharp/src/emitter.ts +++ b/src/TypeSpec.Extension/Emitter.Csharp/src/emitter.ts @@ -88,7 +88,9 @@ export async function $onEmit(context: EmitContext) { ); configurations["azure-arm"] = sdkContext.arm === false ? undefined : sdkContext.arm; - configurations["use-write-core"] = options["use-write-core"]; + configurations["use-write-core"] = + options["use-write-core"] ?? + (configurations["azure-arm"] ? true : undefined); await program.host.writeFile( resolvePath(outputFolder, configurationFileName), prettierOutput(JSON.stringify(configurations, null, 2)) diff --git a/src/TypeSpec.Extension/Emitter.Csharp/src/options.ts b/src/TypeSpec.Extension/Emitter.Csharp/src/options.ts index 05381a37261..6c131b36fa9 100644 --- a/src/TypeSpec.Extension/Emitter.Csharp/src/options.ts +++ b/src/TypeSpec.Extension/Emitter.Csharp/src/options.ts @@ -30,7 +30,7 @@ export const AzureNetEmitterOptionsSchema: JSONSchemaType ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -27,7 +36,7 @@ void IJsonModel.Write(Utf8JsonWriter writer, throw new FormatException($"The model {nameof(ManagedIdentityTrackedResourceData)} does not support writing '{format}' format."); } - writer.WriteStartObject(); + base.JsonModelWriteCore(writer, options); if (Optional.IsDefined(Properties)) { writer.WritePropertyName("properties"u8); @@ -39,55 +48,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, var serializeOptions = new JsonSerializerOptions { Converters = { new ManagedServiceIdentityTypeV3Converter() } }; JsonSerializer.Serialize(writer, Identity, serializeOptions); } - if (Optional.IsCollectionDefined(Tags)) - { - writer.WritePropertyName("tags"u8); - writer.WriteStartObject(); - foreach (var item in Tags) - { - writer.WritePropertyName(item.Key); - writer.WriteStringValue(item.Value); - } - writer.WriteEndObject(); - } - writer.WritePropertyName("location"u8); - writer.WriteStringValue(Location); - if (options.Format != "W") - { - writer.WritePropertyName("id"u8); - writer.WriteStringValue(Id); - } - if (options.Format != "W") - { - writer.WritePropertyName("name"u8); - writer.WriteStringValue(Name); - } - if (options.Format != "W") - { - writer.WritePropertyName("type"u8); - writer.WriteStringValue(ResourceType); - } - if (options.Format != "W" && Optional.IsDefined(SystemData)) - { - writer.WritePropertyName("systemData"u8); - JsonSerializer.Serialize(writer, SystemData); - } - if (options.Format != "W" && _serializedAdditionalRawData != null) - { - foreach (var item in _serializedAdditionalRawData) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - writer.WriteEndObject(); } ManagedIdentityTrackedResourceData IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/common-types/managed-identity/src/Generated/Models/ManagedIdentityTrackedResourceProperties.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/common-types/managed-identity/src/Generated/Models/ManagedIdentityTrackedResourceProperties.Serialization.cs index 93cc972e63a..5614d4e67e4 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/common-types/managed-identity/src/Generated/Models/ManagedIdentityTrackedResourceProperties.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/common-types/managed-identity/src/Generated/Models/ManagedIdentityTrackedResourceProperties.Serialization.cs @@ -18,6 +18,15 @@ internal partial class ManagedIdentityTrackedResourceProperties : IUtf8JsonSeria void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter w throw new FormatException($"The model {nameof(ManagedIdentityTrackedResourceProperties)} does not support writing '{format}' format."); } - writer.WriteStartObject(); if (options.Format != "W") { writer.WritePropertyName("provisioningState"u8); @@ -46,7 +54,6 @@ void IJsonModel.Write(Utf8JsonWriter w #endif } } - writer.WriteEndObject(); } ManagedIdentityTrackedResourceProperties IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/Configuration.json b/test/CadlRanchProjects/azure/resource-manager/models/resources/Configuration.json index 3dae92d419c..57c6dd79670 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/Configuration.json +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/Configuration.json @@ -8,5 +8,6 @@ "../../../../../../../../artifacts/bin/AutoRest.CSharp/Debug/net8.0/Generator.Shared", "../../../../../../../../artifacts/bin/AutoRest.CSharp/Debug/net8.0/Azure.Core.Shared" ], - "azure-arm": true + "azure-arm": true, + "use-write-core": true } diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NestedProxyResourceListResult.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NestedProxyResourceListResult.Serialization.cs index 043a03a3836..c59fa7904d1 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NestedProxyResourceListResult.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NestedProxyResourceListResult.Serialization.cs @@ -18,6 +18,15 @@ internal partial class NestedProxyResourceListResult : IUtf8JsonSerializable, IJ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, Mode throw new FormatException($"The model {nameof(NestedProxyResourceListResult)} does not support writing '{format}' format."); } - writer.WriteStartObject(); writer.WritePropertyName("value"u8); writer.WriteStartArray(); foreach (var item in Value) @@ -53,7 +61,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, Mode #endif } } - writer.WriteEndObject(); } NestedProxyResourceListResult IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NestedProxyResourceProperties.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NestedProxyResourceProperties.Serialization.cs index bc85a16953b..b6b7f12302d 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NestedProxyResourceProperties.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NestedProxyResourceProperties.Serialization.cs @@ -18,6 +18,15 @@ public partial class NestedProxyResourceProperties : IUtf8JsonSerializable, IJso void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, Mode throw new FormatException($"The model {nameof(NestedProxyResourceProperties)} does not support writing '{format}' format."); } - writer.WriteStartObject(); if (options.Format != "W" && Optional.IsDefined(ProvisioningState)) { writer.WritePropertyName("provisioningState"u8); @@ -51,7 +59,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, Mode #endif } } - writer.WriteEndObject(); } NestedProxyResourceProperties IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NotificationDetails.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NotificationDetails.Serialization.cs index 84f64177113..b0613ca9d41 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NotificationDetails.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/NotificationDetails.Serialization.cs @@ -18,6 +18,15 @@ public partial class NotificationDetails : IUtf8JsonSerializable, IJsonModel ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWri throw new FormatException($"The model {nameof(NotificationDetails)} does not support writing '{format}' format."); } - writer.WriteStartObject(); writer.WritePropertyName("message"u8); writer.WriteStringValue(Message); writer.WritePropertyName("urgent"u8); @@ -45,7 +53,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWri #endif } } - writer.WriteEndObject(); } NotificationDetails IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/SingletonTrackedResourceListResult.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/SingletonTrackedResourceListResult.Serialization.cs index e6bcb03c107..1dd01e57c38 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/SingletonTrackedResourceListResult.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/SingletonTrackedResourceListResult.Serialization.cs @@ -18,6 +18,15 @@ internal partial class SingletonTrackedResourceListResult : IUtf8JsonSerializabl void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, throw new FormatException($"The model {nameof(SingletonTrackedResourceListResult)} does not support writing '{format}' format."); } - writer.WriteStartObject(); writer.WritePropertyName("value"u8); writer.WriteStartArray(); foreach (var item in Value) @@ -53,7 +61,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, #endif } } - writer.WriteEndObject(); } SingletonTrackedResourceListResult IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/SingletonTrackedResourceProperties.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/SingletonTrackedResourceProperties.Serialization.cs index ca5a9deeb1f..57903c0d581 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/SingletonTrackedResourceProperties.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/SingletonTrackedResourceProperties.Serialization.cs @@ -18,6 +18,15 @@ public partial class SingletonTrackedResourceProperties : IUtf8JsonSerializable, void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, throw new FormatException($"The model {nameof(SingletonTrackedResourceProperties)} does not support writing '{format}' format."); } - writer.WriteStartObject(); if (options.Format != "W" && Optional.IsDefined(ProvisioningState)) { writer.WritePropertyName("provisioningState"u8); @@ -51,7 +59,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, #endif } } - writer.WriteEndObject(); } SingletonTrackedResourceProperties IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/TopLevelTrackedResourceListResult.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/TopLevelTrackedResourceListResult.Serialization.cs index 490baf5759d..05f81e2e0c3 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/TopLevelTrackedResourceListResult.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/TopLevelTrackedResourceListResult.Serialization.cs @@ -18,6 +18,15 @@ internal partial class TopLevelTrackedResourceListResult : IUtf8JsonSerializable void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, throw new FormatException($"The model {nameof(TopLevelTrackedResourceListResult)} does not support writing '{format}' format."); } - writer.WriteStartObject(); writer.WritePropertyName("value"u8); writer.WriteStartArray(); foreach (var item in Value) @@ -53,7 +61,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, #endif } } - writer.WriteEndObject(); } TopLevelTrackedResourceListResult IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/TopLevelTrackedResourceProperties.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/TopLevelTrackedResourceProperties.Serialization.cs index 36202f75988..aac74281cf2 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/TopLevelTrackedResourceProperties.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/Models/TopLevelTrackedResourceProperties.Serialization.cs @@ -18,6 +18,15 @@ public partial class TopLevelTrackedResourceProperties : IUtf8JsonSerializable, void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, throw new FormatException($"The model {nameof(TopLevelTrackedResourceProperties)} does not support writing '{format}' format."); } - writer.WriteStartObject(); if (options.Format != "W" && Optional.IsDefined(ProvisioningState)) { writer.WritePropertyName("provisioningState"u8); @@ -51,7 +59,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, #endif } } - writer.WriteEndObject(); } TopLevelTrackedResourceProperties IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/NestedProxyResourceData.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/NestedProxyResourceData.Serialization.cs index 693bf0485aa..41b19047f60 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/NestedProxyResourceData.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/NestedProxyResourceData.Serialization.cs @@ -20,6 +20,15 @@ public partial class NestedProxyResourceData : IUtf8JsonSerializable, IJsonModel void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -27,48 +36,12 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReade throw new FormatException($"The model {nameof(NestedProxyResourceData)} does not support writing '{format}' format."); } - writer.WriteStartObject(); + base.JsonModelWriteCore(writer, options); if (Optional.IsDefined(Properties)) { writer.WritePropertyName("properties"u8); writer.WriteObjectValue(Properties, options); } - if (options.Format != "W") - { - writer.WritePropertyName("id"u8); - writer.WriteStringValue(Id); - } - if (options.Format != "W") - { - writer.WritePropertyName("name"u8); - writer.WriteStringValue(Name); - } - if (options.Format != "W") - { - writer.WritePropertyName("type"u8); - writer.WriteStringValue(ResourceType); - } - if (options.Format != "W" && Optional.IsDefined(SystemData)) - { - writer.WritePropertyName("systemData"u8); - JsonSerializer.Serialize(writer, SystemData); - } - if (options.Format != "W" && _serializedAdditionalRawData != null) - { - foreach (var item in _serializedAdditionalRawData) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - writer.WriteEndObject(); } NestedProxyResourceData IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/SingletonTrackedResourceData.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/SingletonTrackedResourceData.Serialization.cs index ed03af8c973..54832584e3c 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/SingletonTrackedResourceData.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/SingletonTrackedResourceData.Serialization.cs @@ -20,6 +20,15 @@ public partial class SingletonTrackedResourceData : IUtf8JsonSerializable, IJson void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -27,61 +36,12 @@ void IJsonModel.Write(Utf8JsonWriter writer, Model throw new FormatException($"The model {nameof(SingletonTrackedResourceData)} does not support writing '{format}' format."); } - writer.WriteStartObject(); + base.JsonModelWriteCore(writer, options); if (Optional.IsDefined(Properties)) { writer.WritePropertyName("properties"u8); writer.WriteObjectValue(Properties, options); } - if (Optional.IsCollectionDefined(Tags)) - { - writer.WritePropertyName("tags"u8); - writer.WriteStartObject(); - foreach (var item in Tags) - { - writer.WritePropertyName(item.Key); - writer.WriteStringValue(item.Value); - } - writer.WriteEndObject(); - } - writer.WritePropertyName("location"u8); - writer.WriteStringValue(Location); - if (options.Format != "W") - { - writer.WritePropertyName("id"u8); - writer.WriteStringValue(Id); - } - if (options.Format != "W") - { - writer.WritePropertyName("name"u8); - writer.WriteStringValue(Name); - } - if (options.Format != "W") - { - writer.WritePropertyName("type"u8); - writer.WriteStringValue(ResourceType); - } - if (options.Format != "W" && Optional.IsDefined(SystemData)) - { - writer.WritePropertyName("systemData"u8); - JsonSerializer.Serialize(writer, SystemData); - } - if (options.Format != "W" && _serializedAdditionalRawData != null) - { - foreach (var item in _serializedAdditionalRawData) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - writer.WriteEndObject(); } SingletonTrackedResourceData IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/TopLevelTrackedResourceData.Serialization.cs b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/TopLevelTrackedResourceData.Serialization.cs index f8f81ee2eb1..8bea8981f17 100644 --- a/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/TopLevelTrackedResourceData.Serialization.cs +++ b/test/CadlRanchProjects/azure/resource-manager/models/resources/src/Generated/TopLevelTrackedResourceData.Serialization.cs @@ -20,6 +20,15 @@ public partial class TopLevelTrackedResourceData : IUtf8JsonSerializable, IJsonM void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -27,61 +36,12 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelR throw new FormatException($"The model {nameof(TopLevelTrackedResourceData)} does not support writing '{format}' format."); } - writer.WriteStartObject(); + base.JsonModelWriteCore(writer, options); if (Optional.IsDefined(Properties)) { writer.WritePropertyName("properties"u8); writer.WriteObjectValue(Properties, options); } - if (Optional.IsCollectionDefined(Tags)) - { - writer.WritePropertyName("tags"u8); - writer.WriteStartObject(); - foreach (var item in Tags) - { - writer.WritePropertyName(item.Key); - writer.WriteStringValue(item.Value); - } - writer.WriteEndObject(); - } - writer.WritePropertyName("location"u8); - writer.WriteStringValue(Location); - if (options.Format != "W") - { - writer.WritePropertyName("id"u8); - writer.WriteStringValue(Id); - } - if (options.Format != "W") - { - writer.WritePropertyName("name"u8); - writer.WriteStringValue(Name); - } - if (options.Format != "W") - { - writer.WritePropertyName("type"u8); - writer.WriteStringValue(ResourceType); - } - if (options.Format != "W" && Optional.IsDefined(SystemData)) - { - writer.WritePropertyName("systemData"u8); - JsonSerializer.Serialize(writer, SystemData); - } - if (options.Format != "W" && _serializedAdditionalRawData != null) - { - foreach (var item in _serializedAdditionalRawData) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - writer.WriteEndObject(); } TopLevelTrackedResourceData IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/TestProjects/MgmtTypeSpec/Configuration.json b/test/TestProjects/MgmtTypeSpec/Configuration.json index a1b6c95c139..aee00908937 100644 --- a/test/TestProjects/MgmtTypeSpec/Configuration.json +++ b/test/TestProjects/MgmtTypeSpec/Configuration.json @@ -9,5 +9,6 @@ "../../../../../artifacts/bin/AutoRest.CSharp/Debug/net8.0/Azure.Core.Shared" ], "examples-dir": "examples", - "azure-arm": true + "azure-arm": true, + "use-write-core": true } diff --git a/test/TestProjects/MgmtTypeSpec/src/Generated/FooData.Serialization.cs b/test/TestProjects/MgmtTypeSpec/src/Generated/FooData.Serialization.cs index ff493b47f27..c70e62ff4df 100644 --- a/test/TestProjects/MgmtTypeSpec/src/Generated/FooData.Serialization.cs +++ b/test/TestProjects/MgmtTypeSpec/src/Generated/FooData.Serialization.cs @@ -21,6 +21,15 @@ public partial class FooData : IUtf8JsonSerializable, IJsonModel void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -28,7 +37,7 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions o throw new FormatException($"The model {nameof(FooData)} does not support writing '{format}' format."); } - writer.WriteStartObject(); + base.JsonModelWriteCore(writer, options); if (Optional.IsDefined(Properties)) { writer.WritePropertyName("properties"u8); @@ -39,55 +48,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions o writer.WritePropertyName("extendedLocation"u8); JsonSerializer.Serialize(writer, ExtendedLocation); } - if (Optional.IsCollectionDefined(Tags)) - { - writer.WritePropertyName("tags"u8); - writer.WriteStartObject(); - foreach (var item in Tags) - { - writer.WritePropertyName(item.Key); - writer.WriteStringValue(item.Value); - } - writer.WriteEndObject(); - } - writer.WritePropertyName("location"u8); - writer.WriteStringValue(Location); - if (options.Format != "W") - { - writer.WritePropertyName("id"u8); - writer.WriteStringValue(Id); - } - if (options.Format != "W") - { - writer.WritePropertyName("name"u8); - writer.WriteStringValue(Name); - } - if (options.Format != "W") - { - writer.WritePropertyName("type"u8); - writer.WriteStringValue(ResourceType); - } - if (options.Format != "W" && Optional.IsDefined(SystemData)) - { - writer.WritePropertyName("systemData"u8); - JsonSerializer.Serialize(writer, SystemData); - } - if (options.Format != "W" && _serializedAdditionalRawData != null) - { - foreach (var item in _serializedAdditionalRawData) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - writer.WriteEndObject(); } FooData IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/FooListResult.Serialization.cs b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/FooListResult.Serialization.cs index 7e308c3302c..f88b8fed409 100644 --- a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/FooListResult.Serialization.cs +++ b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/FooListResult.Serialization.cs @@ -18,6 +18,15 @@ internal partial class FooListResult : IUtf8JsonSerializable, IJsonModel ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOpt throw new FormatException($"The model {nameof(FooListResult)} does not support writing '{format}' format."); } - writer.WriteStartObject(); writer.WritePropertyName("value"u8); writer.WriteStartArray(); foreach (var item in Value) @@ -53,7 +61,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOpt #endif } } - writer.WriteEndObject(); } FooListResult IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/FooProperties.Serialization.cs b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/FooProperties.Serialization.cs index 55e43bbce2f..91e9e3d8a18 100644 --- a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/FooProperties.Serialization.cs +++ b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/FooProperties.Serialization.cs @@ -20,6 +20,15 @@ public partial class FooProperties : IUtf8JsonSerializable, IJsonModel ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -27,7 +36,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOpt throw new FormatException($"The model {nameof(FooProperties)} does not support writing '{format}' format."); } - writer.WriteStartObject(); if (Optional.IsDefined(ServiceUri)) { writer.WritePropertyName("serviceUrl"u8); @@ -68,7 +76,6 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOpt #endif } } - writer.WriteEndObject(); } FooProperties IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceData.Serialization.cs b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceData.Serialization.cs index 5c583e26031..d55acfc36ae 100644 --- a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceData.Serialization.cs +++ b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceData.Serialization.cs @@ -19,6 +19,15 @@ public partial class MgmtTypeSpecPrivateLinkResourceData : IUtf8JsonSerializable void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -26,7 +35,7 @@ void IJsonModel.Write(Utf8JsonWriter writer throw new FormatException($"The model {nameof(MgmtTypeSpecPrivateLinkResourceData)} does not support writing '{format}' format."); } - writer.WriteStartObject(); + base.JsonModelWriteCore(writer, options); if (Optional.IsDefined(Properties)) { writer.WritePropertyName("properties"u8); @@ -38,42 +47,6 @@ void IJsonModel.Write(Utf8JsonWriter writer var serializeOptions = new JsonSerializerOptions { Converters = { new ManagedServiceIdentityTypeV3Converter() } }; JsonSerializer.Serialize(writer, Identity, serializeOptions); } - if (options.Format != "W") - { - writer.WritePropertyName("id"u8); - writer.WriteStringValue(Id); - } - if (options.Format != "W") - { - writer.WritePropertyName("name"u8); - writer.WriteStringValue(Name); - } - if (options.Format != "W") - { - writer.WritePropertyName("type"u8); - writer.WriteStringValue(ResourceType); - } - if (options.Format != "W" && Optional.IsDefined(SystemData)) - { - writer.WritePropertyName("systemData"u8); - JsonSerializer.Serialize(writer, SystemData); - } - if (options.Format != "W" && _serializedAdditionalRawData != null) - { - foreach (var item in _serializedAdditionalRawData) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - writer.WriteEndObject(); } MgmtTypeSpecPrivateLinkResourceData IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceListResult.Serialization.cs b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceListResult.Serialization.cs index be2df99cc47..79ae91f3730 100644 --- a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceListResult.Serialization.cs +++ b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceListResult.Serialization.cs @@ -18,6 +18,15 @@ internal partial class MgmtTypeSpecPrivateLinkResourceListResult : IUtf8JsonSeri void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter throw new FormatException($"The model {nameof(MgmtTypeSpecPrivateLinkResourceListResult)} does not support writing '{format}' format."); } - writer.WriteStartObject(); writer.WritePropertyName("value"u8); writer.WriteStartArray(); foreach (var item in Value) @@ -53,7 +61,6 @@ void IJsonModel.Write(Utf8JsonWriter #endif } } - writer.WriteEndObject(); } MgmtTypeSpecPrivateLinkResourceListResult IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) diff --git a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceProperties.Serialization.cs b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceProperties.Serialization.cs index f59a5d85af1..df2e97108de 100644 --- a/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceProperties.Serialization.cs +++ b/test/TestProjects/MgmtTypeSpec/src/Generated/Models/MgmtTypeSpecPrivateLinkResourceProperties.Serialization.cs @@ -18,6 +18,15 @@ public partial class MgmtTypeSpecPrivateLinkResourceProperties : IUtf8JsonSerial void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") @@ -25,7 +34,6 @@ void IJsonModel.Write(Utf8JsonWriter throw new FormatException($"The model {nameof(MgmtTypeSpecPrivateLinkResourceProperties)} does not support writing '{format}' format."); } - writer.WriteStartObject(); if (options.Format != "W" && Optional.IsDefined(GroupId)) { writer.WritePropertyName("groupId"u8); @@ -66,7 +74,6 @@ void IJsonModel.Write(Utf8JsonWriter #endif } } - writer.WriteEndObject(); } MgmtTypeSpecPrivateLinkResourceProperties IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options)