Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<!-- CodeGenerator projects that can be built into packages -->
<ItemGroup>
<PackageableLibraries Include="$(LibraryRoot)/src/core/AutoRest/project.json" />
<PackageableLibraries Include="$(LibraryRoot)/src/dev/AutoRest.Preview/project.json" />
</ItemGroup>

<!-- Our Custom Tasks -->
Expand Down Expand Up @@ -181,6 +182,7 @@
<-->
<ItemGroup>
<XProjFiles Include="src/core/AutoRest/AutoRest.xproj" />
<XProjFiles Include="src/dev/AutoRest.Preview/AutoRest.Preview.xproj" />
<XProjFiles Include="src/core/AutoRest.Core/AutoRest.Core.xproj" />
<XProjFiles Include="src/core/AutoRest.Core.Tests/AutoRest.Core.Tests.xproj" />
<XProjFiles Include="src/core/AutoRest.Extensions/AutoRest.Extensions.xproj" />
Expand Down
9 changes: 8 additions & 1 deletion src/core/AutoRest.Core/Model/CompositeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,15 @@ public Dictionary<string, object> ComposedExtensions
[JsonIgnore]
public override string DefaultValue => IsConstant ? "{}" : null;


/// <summary>
/// Determines if the CompositeType is Constant (ie, the value is known at compile time)
///
/// Note: Added a check to ensure that it's not polymorphic.
/// If it's polymorphic, it can't possibly be known at compile time.
/// </summary>
[JsonIgnore]
public override bool IsConstant => ComposedProperties.Any() && ComposedProperties.All(p => p.IsConstant);
public override bool IsConstant => !BaseIsPolymorphic && ComposedProperties.Any() && ComposedProperties.All(p => p.IsConstant);

[JsonIgnore]
public override IEnumerable<IChild> Children => Properties;
Expand Down
1 change: 1 addition & 0 deletions src/core/AutoRest.Extensions/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ public static void FlattenMethodParameters(CodeModel codeModel)
{
var bodyParameterType = bodyParameter.ModelType as CompositeType;
if (bodyParameterType != null &&
!bodyParameterType.BaseIsPolymorphic &&
(bodyParameterType.ComposedProperties.Count(p => !p.IsConstant && !p.IsReadOnly) <= Settings.Instance.PayloadFlatteningThreshold ||
bodyParameter.ShouldBeFlattened()))
{
Expand Down
68 changes: 68 additions & 0 deletions src/generator/AutoRest.CSharp.Unit.Tests/Bug1720.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using AutoRest.Core.Utilities;
using Microsoft.CodeAnalysis;
using Xunit;
using Xunit.Abstractions;


namespace AutoRest.CSharp.Unit.Tests
{
public class Bug1720 : BugTest
{
public Bug1720(ITestOutputHelper output) : base(output)
{
}

[Fact]
public async Task PolymorphicTypesAreNotConstants()
{
using (var fileSystem = GenerateCodeForTestFromSpec(codeGenerator: "Azure.CSharp"))
{
// Expected Files
Assert.True(fileSystem.FileExists(@"GeneratedCode\JobDefinitionsOperations.cs"));

var result = await Compile(fileSystem);
// filter the warnings
var warnings = result.Messages.Where(
each => each.Severity == DiagnosticSeverity.Warning
&& !SuppressWarnings.Contains(each.Id)).ToArray();

// filter the errors
var errors = result.Messages.Where(each => each.Severity == DiagnosticSeverity.Error).ToArray();

Write(warnings, fileSystem);
Write(errors, fileSystem);

// use this to write out all the messages, even hidden ones.
// Write(result.Messages, fileSystem);

// Don't proceed unless we have zero warnings.
Assert.Empty(warnings);
// Don't proceed unless we have zero Errors.
Assert.Empty(errors);

// Should also succeed.
Assert.True(result.Succeeded);

// try to load the assembly
var asm = Assembly.Load(result.Output.GetBuffer());
Assert.NotNull(asm);

// verify that class with the expected name is present
var ops = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.JobDefinitionsOperationsExtensions");
Assert.NotNull(ops);

// verify the method is there.
var method = ops.GetMethod("ListResults");
Assert.NotNull(method);

// verify that the parameter is there.
Assert.True(method.GetParameters().Any(each => each.Name.EqualsIgnoreCase("DataServiceResultQuery")));
}
}
}
}
Loading