Address PR comments.
This commit is contained in:
Родитель
506e7b8722
Коммит
685888b6f4
|
@ -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.");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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});");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче