in src/AutoRest.CSharp/Common/Output/Expressions/Snippets.Convert.cs [43:139]
public static ValueExpression GetConversionToProtocol(this Parameter convenience, CSharpType toType, string? contentType)
{
// deal with the cases of converting to RequestContent
if (toType.EqualsIgnoreNullable(Configuration.ApiTypes.RequestContentType))
{
return GetConversionToRequestContent(convenience, contentType);
}
TypedValueExpression expression = new ParameterReference(convenience);
// converting to anything else should be path, query, head parameters
if (expression.Type is { IsFrameworkType: false, Implementation: EnumType enumType })
{
if (convenience.Type.IsNullable)
{
expression = expression.NullConditional();
}
expression = new EnumExpression(enumType, expression).ToSerial();
}
return expression;
static ValueExpression GetConversionToRequestContent(Parameter convenience, string? contentType)
{
switch (convenience.Type)
{
case { IsFrameworkType: true }:
return GetConversionFromFrameworkToRequestContent(convenience, contentType);
case { IsFrameworkType: false, Implementation: EnumType enumType }:
TypedValueExpression enumExpression = new ParameterReference(convenience);
if (convenience.IsOptionalInSignature)
{
enumExpression = enumExpression.NullableStructValue();
}
var convenienceEnum = new EnumExpression(enumType, enumExpression);
return Extensible.RequestContent.Create(BinaryDataExpression.FromObjectAsJson(convenienceEnum.ToSerial()));
case { IsFrameworkType: false, Implementation: ModelTypeProvider model }:
TypedValueExpression modelExpression = new ParameterReference(convenience);
if (convenience.IsOptionalInSignature)
{
modelExpression = modelExpression.NullConditional();
}
var serializableObjectExpression = new SerializableObjectTypeExpression(model, modelExpression);
if (contentType != null && BodyMediaTypeHelper.FromString(contentType) == BodyMediaType.Multipart)
{
return serializableObjectExpression.ToMultipartRequestContent();
}
return serializableObjectExpression.ToRequestContent();
default:
throw new InvalidOperationException($"Unhandled type: {convenience.Type}");
}
}
static ValueExpression GetConversionFromFrameworkToRequestContent(Parameter parameter, string? contentType)
{
if (parameter.Type.IsReadWriteDictionary)
{
var expression = RequestContentHelperProvider.Instance.FromDictionary(parameter);
if (parameter.IsOptionalInSignature)
{
expression = new TernaryConditionalOperator(NotEqual(parameter, Null), expression, Null);
}
return expression;
}
if (parameter.Type.IsList)
{
var content = (ValueExpression)parameter;
content = parameter.Type.IsReadOnlyMemory
? content.Property(nameof(ReadOnlyMemory<byte>.Span)) // for ReadOnlyMemory, we need to get the Span and pass it through
: content;
var expression = RequestContentHelperProvider.Instance.FromEnumerable(content);
if (parameter.IsOptionalInSignature)
{
expression = new TernaryConditionalOperator(NotEqual(parameter, Null), expression, Null);
}
return expression;
}
if (parameter.Type.IsFrameworkType == true && parameter.Type.FrameworkType == typeof(AzureLocation))
{
return RequestContentHelperProvider.Instance.FromObject(((ValueExpression)parameter).InvokeToString());
}
BodyMediaType? mediaType = contentType == null ? null : BodyMediaTypeHelper.FromString(contentType);
if (parameter.RequestLocation == RequestLocation.Body && mediaType == BodyMediaType.Binary)
{
return parameter;
}
// TODO: Here we only consider the case when body is string type. We will add support for other types.
if (parameter.RequestLocation == RequestLocation.Body && mediaType == BodyMediaType.Text && parameter.Type.FrameworkType == typeof(string))
{
return parameter;
}
return RequestContentHelperProvider.Instance.FromObject(parameter);
}
}