Bug fix: collection of constants (#297)

Bug fix: collection of constants
This commit is contained in:
Pavel Krymets 2019-12-05 16:24:17 -08:00 коммит произвёл GitHub
Родитель ebae2cae0f 4653072d6c
Коммит a5c2c78f12
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 60 добавлений и 29 удалений

Просмотреть файл

@ -1,4 +1,4 @@
param($name, [switch]$NoDebug)
param($name, [switch]$noDebug)
$ErrorActionPreference = 'Stop'
function Invoke-Block([scriptblock]$cmd) {
@ -17,28 +17,45 @@ function Invoke-Block([scriptblock]$cmd) {
}
}
$repoRoot = Resolve-Path "$PSScriptRoot\.."
$testServerTestProject = "$repoRoot\test\AutoRest.TestServer.Tests"
$testConfiguration = "$testServerTestProject\readme.md"
$testServerSwaggerPath = "$repoRoot\node_modules\@microsoft.azure\autorest.testserver\swagger"
$paths = 'url', 'body-string', 'body-complex', 'custom-baseUrl', 'custom-baseUrl-more-options'
if ($name)
{
$paths = $name
}
$debugFlags = if (!$NoDebug) { '--debug','--verbose' }
function Invoke-AutoRest($debugFlags, $testConfiguration, $outputFolder, $inputFile, $name, $namespace, $repoRoot) {
Invoke-Block {
$outputFlag = if($outputFolder) { "--output-folder=$outputFolder" } else { '' }
$inputFlag = if($inputFile) { "--input-file=$inputFile" } else { '' }
$titleFlag = if($name) { "--title=$name" } else { '' }
$namespaceFlag = if($namespace) { "--namespace=$namespace" } else { '' }
$command = "npx autorest-beta $debugFlags $testConfiguration $outputFlag $inputFlag $titleFlag $namespaceFlag"
$commandText = $command.Replace($repoRoot, "`$(SolutionDir)")
foreach ($path in $paths)
{
$outputFolder = "$testServerTestProject\$path";
$inputFile = "$testServerSwaggerPath\$path.json"
$namespace = $path.Replace('-', '_')
Invoke-Block {
$command = "npx autorest-beta $debugFlags $testConfiguration --output-folder=$outputFolder --input-file=$inputFile --title=$path --namespace=$namespace"
$command = $command.Replace($repoRoot, "`$(SolutionDir)")
Write-Host ">" $command
npx autorest-beta @debugFlags $testConfiguration --output-folder=$outputFolder --input-file=$inputFile --title=$path --namespace=$namespace
Write-Host ">" $commandText
Invoke-Expression $command
}
}
# General configuration
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot '..')
$debugFlags = if (-not $noDebug) { '--debug', '--verbose' }
# Test server test configuration
$testServerTestProject = Join-Path $repoRoot 'test' 'AutoRest.TestServer.Tests'
$testConfiguration = Join-Path $testServerTestProject 'readme.md'
$testServerSwaggerPath = Join-Path $repoRoot 'node_modules' '@microsoft.azure' 'autorest.testserver' 'swagger'
$testNames = if ($name) { $name } else { 'url', 'body-string', 'body-complex', 'custom-baseUrl', 'custom-baseUrl-more-options' }
foreach ($testName in $testNames)
{
$outputFolder = Join-Path $testServerTestProject $testName
$inputFile = Join-Path $testServerSwaggerPath "$testName.json"
$namespace = $testName.Replace('-', '_')
Invoke-AutoRest $debugFlags $testConfiguration $outputFolder $inputFile $testName $namespace $repoRoot
}
# Sample configuration
$sampleDirectory = Join-Path $repoRoot 'samples'
$projectNames = 'AppConfiguration'
foreach ($projectName in $projectNames)
{
$projectDirectory = Join-Path $sampleDirectory $projectName
$configurationPath = Join-Path $projectDirectory 'readme.md'
Invoke-AutoRest $debugFlags $configurationPath -repoRoot $repoRoot
}

Просмотреть файл

@ -12,6 +12,7 @@ namespace AutoRest.CSharp.V3.ClientModels
ItemType = itemType;
}
//TODO: This should not be defaulted to nullable false
public override bool IsNullable => false;
}
}

Просмотреть файл

@ -17,8 +17,7 @@ namespace AutoRest.CSharp.V3.CodeGen
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<OutputPath>bin</OutputPath>
<PublishDir>$(OutputPath)</PublishDir>
<!-- Some methods are marked async and don't have an await in them -->
<NoWarn>1998</NoWarn>
<NoWarn>SA1649</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
<Nullable>enable</Nullable>

Просмотреть файл

@ -56,6 +56,9 @@ namespace AutoRest.CSharp.V3.Plugins
private static ServiceClient BuildClient(OperationGroup arg) =>
new ServiceClient(arg.CSharpName(), arg.Operations.Select(BuildMethod).Where(method => method != null).ToArray()!);
private static ClientConstant? CreateDefaultValueConstant(Parameter requestParameter) =>
requestParameter.ClientDefaultValue != null ? new ClientConstant(requestParameter.ClientDefaultValue, new FrameworkTypeReference(typeof(object))) : (ClientConstant?)null;
private static ClientMethod? BuildMethod(Operation operation)
{
var httpRequest = operation.Request.Protocol.Http as HttpRequest;
@ -81,17 +84,27 @@ namespace AutoRest.CSharp.V3.Plugins
switch (requestParameter.Schema)
{
case ConstantSchema constant:
constantOrParameter = new ClientConstant(constant.Value.Value, (FrameworkTypeReference) CreateType(constant.ValueType, false));
constantOrParameter = new ClientConstant(constant.Value.Value, (FrameworkTypeReference)CreateType(constant.ValueType, false));
break;
case BinarySchema _:
// skip
continue;
//TODO: Workaround for https://github.com/Azure/autorest.csharp/pull/275
case ArraySchema arraySchema when arraySchema.ElementType is ConstantSchema constantInnerType:
constantOrParameter = new ServiceClientMethodParameter(requestParameter.CSharpName(),
new CollectionTypeReference(CreateType(constantInnerType.ValueType, false)),
CreateDefaultValueConstant(requestParameter));
break;
//TODO: Workaround for https://github.com/Azure/autorest.csharp/pull/275
case DictionarySchema dictionarySchema when dictionarySchema.ElementType is ConstantSchema constantInnerType:
constantOrParameter = new ServiceClientMethodParameter(requestParameter.CSharpName(),
new CollectionTypeReference(CreateType(constantInnerType.ValueType, false)),
CreateDefaultValueConstant(requestParameter));
break;
default:
constantOrParameter = new ServiceClientMethodParameter(requestParameter.CSharpName(),
CreateType(requestParameter.Schema, requestParameter.IsNullable()),
requestParameter.ClientDefaultValue != null ?
new ClientConstant(requestParameter.ClientDefaultValue, new FrameworkTypeReference(typeof(object))) :
(ClientConstant?)null);
CreateDefaultValueConstant(requestParameter));
break;
}
@ -210,6 +223,7 @@ namespace AutoRest.CSharp.V3.Plugins
private static ClientObjectProperty CreateProperty(Property property) =>
new ClientObjectProperty(property.CSharpName(), CreateType(property.Schema, property.IsNullable()), property.Schema.IsLazy(), property.SerializedName);
//TODO: Handle nullability properly
private static ClientTypeReference CreateType(Schema schema, bool isNullable) => schema switch
{
BinarySchema _ => (ClientTypeReference)new BinaryTypeReference(false),