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
73 changes: 73 additions & 0 deletions src/generator/AutoRest.CSharp.Unit.Tests/Bug1763.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//

using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Xunit;
using Xunit.Abstractions;

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

/// <summary>
/// https://github.com/Azure/autorest/issues/1763
/// Verifies autorest succesfully generates code for models with properties starting with '_'
/// </summary>
[Fact]
public async Task VerifyUnderscoredClassMembers()
{
using (var fileSystem = GenerateCodeForTestFromSpec())
{
var result = await Compile(fileSystem);

// Expected Files
Assert.True(fileSystem.FileExists(@"GeneratedCode\Models\Pet.cs"));

// 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);

// 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);
var petModel = asm.ExportedTypes.First(type => type.FullName == "Test.Models.Pet" );
var idMember = petModel.GetMembers().First(member => member.Name == "_id" );
Assert.NotNull(idMember);

// now read from the file to ensure we use this. to access the members
var codeText = fileSystem.ReadFileAsText(@"GeneratedCode\Models\Pet.cs");
// get hold of the ctor
var regex = new Regex(Regex.Escape("public Pet(int _id, string name = default(string))") + @"[^}]+");
var match = regex.Match(codeText);
Assert.NotNull(match);
// verify the ctor has proper assignments
Assert.True(match.Groups[0].Value.Contains("this._id = _id"));
Assert.True(match.Groups[0].Value.Contains("this.Name = name"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
swagger: '2.0'
info:
title: TestAPI
description: test api
version: 0.0.0
paths:
"/SimpleAPI/Pets":
put:
operationId: test_PutPet
description: Put pet with name
parameters:
- in: body
name: pet
description: pet to put
required: true
schema:
"$ref": "#/definitions/Pet"
responses:
'200':
description: Pet added to system
definitions:
Pet:
description: simple pet to add
required:
- _id
properties:
_id:
type: integer
name:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace @(Settings.Namespace).@(Settings.ModelsName)
&& p.IsRequired
&& ((CompositeType)p.ModelType).ContainsConstantProperties))
{
@:@(property.Name) = new @(property.ModelTypeName)();
@:this.@(property.Name) = new @(property.ModelTypeName)();
}
}
</text>
Expand Down Expand Up @@ -88,7 +88,7 @@ namespace @(Settings.Namespace).@(Settings.ModelsName)
&& p.IsRequired
&& ((CompositeType)p.ModelType).ContainsConstantProperties))
{
@:@(property.Name) = new @(property.ModelTypeName)();
@:this.@(property.Name) = new @(property.ModelTypeName)();
}

foreach (var property in Model.Properties.Where(p => !p.IsConstant))
Expand All @@ -100,7 +100,7 @@ namespace @(Settings.Namespace).@(Settings.ModelsName)
}
else
{
@:@(property.Name) = @propName;
@:this.@(property.Name) = @propName;
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ namespace @(Settings.Namespace).@(Settings.ModelsName)
foreach (PropertyCs property in Model.Properties.Where(p => p.IsRequired && !p.IsReadOnly && !p.IsConstant && p.IsNullable() ))
{
anythingToValidate = true;
@:if (@property.Name == null)
@:if (this.@property.Name == null)
@:{
@:throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.CannotBeNull, "@property.Name");
@:}
Expand Down