From 685888b6f40f3a5f56cef5fcfdab36899cc27a4d Mon Sep 17 00:00:00 2001 From: Michael Yanni Date: Tue, 10 Mar 2020 17:40:39 -0700 Subject: [PATCH] Address PR comments. --- .../Generation/Writers/ClientWriter.cs | 25 +++-------------- .../Writers/CodeWriterExtensions.cs | 17 ++++++++++++ .../Writers/JsonCodeWriterExtensions.cs | 8 +++--- .../Generation/Writers/RestClientWriter.cs | 27 +++++++------------ .../Writers/XmlCodeWriterExtensions.cs | 9 +++---- .../Output/Builders/ClientBuilder.cs | 11 ++++++-- .../Output/Builders/SerializationBuilder.cs | 4 --- .../Models/Requests/BinaryRequestBody.cs | 13 +++++++++ .../Output/Models/Requests/RequestBody.cs | 6 +---- .../Models/Requests/SchemaRequestBody.cs | 18 +++++++++++++ .../Serialization/BinarySerialization.cs | 12 --------- 11 files changed, 79 insertions(+), 71 deletions(-) create mode 100644 src/AutoRest.CSharp.V3/Output/Models/Requests/BinaryRequestBody.cs create mode 100644 src/AutoRest.CSharp.V3/Output/Models/Requests/SchemaRequestBody.cs delete mode 100644 src/AutoRest.CSharp.V3/Output/Models/Serialization/BinarySerialization.cs diff --git a/src/AutoRest.CSharp.V3/Generation/Writers/ClientWriter.cs b/src/AutoRest.CSharp.V3/Generation/Writers/ClientWriter.cs index 9c246be2..99a515e5 100644 --- a/src/AutoRest.CSharp.V3/Generation/Writers/ClientWriter.cs +++ b/src/AutoRest.CSharp.V3/Generation/Writers/ClientWriter.cs @@ -265,38 +265,19 @@ namespace AutoRest.CSharp.V3.Generation.Writers ObjectSerialization serialization = objectResponseBody.Serialization; using (writer.Scope($"({responseVariable:D}, cancellationToken) =>", "{", "},")) { - switch (serialization) - { - case JsonSerialization jsonSerialization: - writer.WriteMethodDeserialization(jsonSerialization, async: false, ref valueVariable, responseVariable); - break; - case XmlElementSerialization xmlSerialization: - writer.WriteMethodDeserialization(xmlSerialization, ref valueVariable, responseVariable); - break; - default: - throw new NotSupportedException(); - } + writer.WriteDeserializationForMethods(objectResponseBody.Serialization, async: false, ref valueVariable, responseVariable); writer.Line($"return {valueVariable};"); } using (writer.Scope($"async ({responseVariable:D}, cancellationToken) =>", newLine: false)) { - switch (serialization) - { - case JsonSerialization jsonSerialization: - writer.WriteMethodDeserialization(jsonSerialization, async: true, ref valueVariable, responseVariable); - break; - case XmlElementSerialization xmlSerialization: - writer.WriteMethodDeserialization(xmlSerialization, ref valueVariable, responseVariable); - break; - default: - throw new NotSupportedException(); - } + writer.WriteDeserializationForMethods(objectResponseBody.Serialization, async: true, ref valueVariable, responseVariable); writer.Line($"return {valueVariable};"); } } else if (lroMethod.OriginalResponse.ResponseBody is StreamResponseBody) { + //TODO: https://github.com/Azure/autorest.csharp/issues/523 throw new NotSupportedException("Binary is not supported as message (not response) is required for ExtractResponseContent() call."); } } diff --git a/src/AutoRest.CSharp.V3/Generation/Writers/CodeWriterExtensions.cs b/src/AutoRest.CSharp.V3/Generation/Writers/CodeWriterExtensions.cs index 1466c0bf..ec65ef0c 100644 --- a/src/AutoRest.CSharp.V3/Generation/Writers/CodeWriterExtensions.cs +++ b/src/AutoRest.CSharp.V3/Generation/Writers/CodeWriterExtensions.cs @@ -5,6 +5,9 @@ using System; using System.Collections.Generic; using System.Linq; using AutoRest.CSharp.V3.Generation.Types; +using AutoRest.CSharp.V3.Output.Models.Serialization; +using AutoRest.CSharp.V3.Output.Models.Serialization.Json; +using AutoRest.CSharp.V3.Output.Models.Serialization.Xml; using AutoRest.CSharp.V3.Output.Models.Shared; namespace AutoRest.CSharp.V3.Generation.Writers @@ -51,5 +54,19 @@ namespace AutoRest.CSharp.V3.Generation.Writers writer.Line(); } } + + public static void WriteDeserializationForMethods(this CodeWriter writer, ObjectSerialization serialization, bool async, + ref string valueVariable, string responseVariable, string document = "document") + { + switch (serialization) + { + case JsonSerialization jsonSerialization: + writer.WriteDeserializationForMethods(jsonSerialization, async, ref valueVariable, responseVariable); + break; + case XmlElementSerialization xmlSerialization: + writer.WriteDeserializationForMethods(xmlSerialization, ref valueVariable, responseVariable); + break; + } + } } } diff --git a/src/AutoRest.CSharp.V3/Generation/Writers/JsonCodeWriterExtensions.cs b/src/AutoRest.CSharp.V3/Generation/Writers/JsonCodeWriterExtensions.cs index 03fa14e1..7ae4192f 100644 --- a/src/AutoRest.CSharp.V3/Generation/Writers/JsonCodeWriterExtensions.cs +++ b/src/AutoRest.CSharp.V3/Generation/Writers/JsonCodeWriterExtensions.cs @@ -384,22 +384,22 @@ namespace AutoRest.CSharp.V3.Generation.Writers _ => null }; - public static void WriteMethodDeserialization(this CodeWriter writer, JsonSerialization serialization, bool async, + public static void WriteDeserializationForMethods(this CodeWriter writer, JsonSerialization serialization, bool async, ref string destination, string response, string document = "document") { writer.Append($"using var {document:D} = "); if (async) { - writer.Line($"await {typeof(JsonDocument)}.ParseAsync({response:D}.ContentStream, default, cancellationToken).ConfigureAwait(false);"); + writer.Line($"await {typeof(JsonDocument)}.ParseAsync({response}.ContentStream, default, cancellationToken).ConfigureAwait(false);"); } else { - writer.Line($"{typeof(JsonDocument)}.Parse({response:D}.ContentStream);"); + writer.Line($"{typeof(JsonDocument)}.Parse({response}.ContentStream);"); } writer.ToDeserializeCall( serialization, - w => w.Append($"{document:D}.RootElement"), + w => w.Append($"{document}.RootElement"), ref destination ); } diff --git a/src/AutoRest.CSharp.V3/Generation/Writers/RestClientWriter.cs b/src/AutoRest.CSharp.V3/Generation/Writers/RestClientWriter.cs index b283a4d3..eb85e6a4 100644 --- a/src/AutoRest.CSharp.V3/Generation/Writers/RestClientWriter.cs +++ b/src/AutoRest.CSharp.V3/Generation/Writers/RestClientWriter.cs @@ -136,7 +136,7 @@ namespace AutoRest.CSharp.V3.Generation.Writers WriteHeader(writer, header); } - if (operation.Request.Body is RequestBody body) + if (operation.Request.Body is SchemaRequestBody body) { ParameterOrConstant value = body.Value; switch (body.Serialization) @@ -157,11 +157,12 @@ namespace AutoRest.CSharp.V3.Generation.Writers writerName: w => w.Append($"content.{nameof(XmlWriterContent.XmlWriter)}")); writer.Line($"request.Content = content;"); break; - case BinarySerialization _: - writer.Line($"request.Content = {typeof(RequestContent)}.Create({WriteConstantOrParameter(value, ignoreNullability: true)});"); - break; } } + else if (operation.Request.Body is BinaryRequestBody binaryBody) + { + writer.Line($"request.Content = {typeof(RequestContent)}.Create({WriteConstantOrParameter(binaryBody.Value, ignoreNullability: true)});"); + } writer.Line($"return message;"); } @@ -419,28 +420,20 @@ namespace AutoRest.CSharp.V3.Generation.Writers string responseVariable = $"{messageVariable}.Response"; if (responseBody is ObjectResponseBody objectResponseBody) { - switch (objectResponseBody.Serialization) - { - case JsonSerialization jsonSerialization: - writer.WriteMethodDeserialization(jsonSerialization, async, ref valueVariable, responseVariable); - break; - case XmlElementSerialization xmlSerialization: - writer.WriteMethodDeserialization(xmlSerialization, ref valueVariable, responseVariable); - break; - } + writer.WriteDeserializationForMethods(objectResponseBody.Serialization, async, ref valueVariable, responseVariable); } else if (responseBody is StreamResponseBody _) { - writer.Line($"var {valueVariable:D} = {messageVariable:D}.ExtractResponseContent();"); - using (writer.Scope($"if ({valueVariable:D} == null)")) + writer.Line($"var {valueVariable:D} = {messageVariable}.ExtractResponseContent();"); + using (writer.Scope($"if ({valueVariable} == null)")) { if (async) { - writer.Line($"throw await clientDiagnostics.CreateRequestFailedExceptionAsync({responseVariable:D}).ConfigureAwait(false);"); + writer.Line($"throw await clientDiagnostics.CreateRequestFailedExceptionAsync({responseVariable}).ConfigureAwait(false);"); } else { - writer.Line($"throw clientDiagnostics.CreateRequestFailedException({responseVariable:D});"); + writer.Line($"throw clientDiagnostics.CreateRequestFailedException({responseVariable});"); } } } diff --git a/src/AutoRest.CSharp.V3/Generation/Writers/XmlCodeWriterExtensions.cs b/src/AutoRest.CSharp.V3/Generation/Writers/XmlCodeWriterExtensions.cs index 74af501d..5da19740 100644 --- a/src/AutoRest.CSharp.V3/Generation/Writers/XmlCodeWriterExtensions.cs +++ b/src/AutoRest.CSharp.V3/Generation/Writers/XmlCodeWriterExtensions.cs @@ -180,8 +180,7 @@ namespace AutoRest.CSharp.V3.Generation.Writers string s = destination; - writer - .Line($"{type} {destination:D} = default;"); + writer.Line($"{type} {destination:D} = default;"); if (isElement) { @@ -377,13 +376,13 @@ namespace AutoRest.CSharp.V3.Generation.Writers } } - public static void WriteMethodDeserialization(this CodeWriter writer, XmlElementSerialization serialization, + public static void WriteDeserializationForMethods(this CodeWriter writer, XmlElementSerialization serialization, ref string destination, string response, string document = "document") { - writer.Line($"var {document:D} = {typeof(XDocument)}.Load({response:D}.ContentStream, LoadOptions.PreserveWhitespace);"); + writer.Line($"var {document:D} = {typeof(XDocument)}.Load({response}.ContentStream, LoadOptions.PreserveWhitespace);"); writer.ToDeserializeCall( serialization, - w => w.Append($"{document:D}"), + w => w.Append($"{document}"), ref destination ); } diff --git a/src/AutoRest.CSharp.V3/Output/Builders/ClientBuilder.cs b/src/AutoRest.CSharp.V3/Output/Builders/ClientBuilder.cs index 1fc5cf6c..b559f04d 100644 --- a/src/AutoRest.CSharp.V3/Output/Builders/ClientBuilder.cs +++ b/src/AutoRest.CSharp.V3/Output/Builders/ClientBuilder.cs @@ -268,8 +268,15 @@ namespace AutoRest.CSharp.V3.Output.Builders break; case ParameterLocation.Body: Debug.Assert(httpRequestWithBody != null); - var serialization = _serializationBuilder.Build(httpRequestWithBody.KnownMediaType, requestParameter.Schema, requestParameter.IsNullable()); - body = new RequestBody(constantOrParameter, serialization); + if (httpRequestWithBody.KnownMediaType == KnownMediaType.Binary) + { + body = new BinaryRequestBody(constantOrParameter); + } + else + { + var serialization = _serializationBuilder.Build(httpRequestWithBody.KnownMediaType, requestParameter.Schema, requestParameter.IsNullable()); + body = new SchemaRequestBody(constantOrParameter, serialization); + } break; case ParameterLocation.Uri: if (defaultName == "$host") diff --git a/src/AutoRest.CSharp.V3/Output/Builders/SerializationBuilder.cs b/src/AutoRest.CSharp.V3/Output/Builders/SerializationBuilder.cs index 634e83b8..239fae7d 100644 --- a/src/AutoRest.CSharp.V3/Output/Builders/SerializationBuilder.cs +++ b/src/AutoRest.CSharp.V3/Output/Builders/SerializationBuilder.cs @@ -30,8 +30,6 @@ namespace AutoRest.CSharp.V3.Output.Builders return BuildJsonObjectSerialization(objectSchema, type); case KnownMediaType.Xml: return BuildXmlObjectSerialization(objectSchema, type); - case KnownMediaType.Binary: - return new BinarySerialization(); default: throw new NotImplementedException(mediaType.ToString()); } @@ -45,8 +43,6 @@ namespace AutoRest.CSharp.V3.Output.Builders return BuildSerialization(schema, isNullable); case KnownMediaType.Xml: return BuildXmlElementSerialization(schema, isNullable, schema.XmlName ?? schema.Name, true); - case KnownMediaType.Binary: - return new BinarySerialization(); default: throw new NotImplementedException(mediaType.ToString()); } diff --git a/src/AutoRest.CSharp.V3/Output/Models/Requests/BinaryRequestBody.cs b/src/AutoRest.CSharp.V3/Output/Models/Requests/BinaryRequestBody.cs new file mode 100644 index 00000000..04189eef --- /dev/null +++ b/src/AutoRest.CSharp.V3/Output/Models/Requests/BinaryRequestBody.cs @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +namespace AutoRest.CSharp.V3.Output.Models.Requests +{ + internal class BinaryRequestBody : RequestBody + { + public BinaryRequestBody(ParameterOrConstant value) + : base(value) + { + } + } +} diff --git a/src/AutoRest.CSharp.V3/Output/Models/Requests/RequestBody.cs b/src/AutoRest.CSharp.V3/Output/Models/Requests/RequestBody.cs index 29e7f444..f203cf15 100644 --- a/src/AutoRest.CSharp.V3/Output/Models/Requests/RequestBody.cs +++ b/src/AutoRest.CSharp.V3/Output/Models/Requests/RequestBody.cs @@ -1,19 +1,15 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. -using AutoRest.CSharp.V3.Output.Models.Serialization; - namespace AutoRest.CSharp.V3.Output.Models.Requests { internal class RequestBody { public ParameterOrConstant Value { get; } - public ObjectSerialization Serialization { get; } - public RequestBody(ParameterOrConstant value, ObjectSerialization serialization) + public RequestBody(ParameterOrConstant value) { Value = value; - Serialization = serialization; } } } diff --git a/src/AutoRest.CSharp.V3/Output/Models/Requests/SchemaRequestBody.cs b/src/AutoRest.CSharp.V3/Output/Models/Requests/SchemaRequestBody.cs new file mode 100644 index 00000000..c023caac --- /dev/null +++ b/src/AutoRest.CSharp.V3/Output/Models/Requests/SchemaRequestBody.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using AutoRest.CSharp.V3.Output.Models.Serialization; + +namespace AutoRest.CSharp.V3.Output.Models.Requests +{ + internal class SchemaRequestBody : RequestBody + { + public ObjectSerialization Serialization { get; } + + public SchemaRequestBody(ParameterOrConstant value, ObjectSerialization serialization) + : base(value) + { + Serialization = serialization; + } + } +} diff --git a/src/AutoRest.CSharp.V3/Output/Models/Serialization/BinarySerialization.cs b/src/AutoRest.CSharp.V3/Output/Models/Serialization/BinarySerialization.cs deleted file mode 100644 index 23e55f4a..00000000 --- a/src/AutoRest.CSharp.V3/Output/Models/Serialization/BinarySerialization.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - - -using AutoRest.CSharp.V3.Generation.Types; - -namespace AutoRest.CSharp.V3.Output.Models.Serialization.Xml -{ - internal class BinarySerialization : ObjectSerialization - { - } -}