Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding model properties or parameters with "enum" constraint to the codeModel as EnumType with modelAsString: true #8

Merged
merged 1 commit into from
Sep 19, 2017
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft.azure/autorest.modeler",
"version": "1.9.7",
"version": "1.9.8",
"description": "The modeler extension for classic generators in AutoRest.",
"scripts": {
"start": "dotnet src/bin/netcoreapp2.0/autorest.modeler.dll --server",
Expand Down
13 changes: 12 additions & 1 deletion src/ObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,18 @@ public virtual IModelType BuildServiceType(string serviceTypeName)
else
{
enumType.ModelAsString = true;
enumType.SetName( string.Empty);
if (SwaggerObject is SwaggerParameter)
{
enumType.SetName((SwaggerObject as SwaggerParameter).Name.ToPascalCase());
}
else
{
// For a model property serviceTypeName is ModelName_PropertyName.
// Hence we set the value after underscore as the name of the enum.
enumType.SetName(serviceTypeName.Substring(serviceTypeName.IndexOf("_") + 1).ToPascalCase());
}

Modeler.CodeModel.Add(enumType);
}
enumType.XmlProperties = (SwaggerObject as Schema)?.Xml;
return enumType;
Expand Down
16 changes: 8 additions & 8 deletions test/SwaggerModelerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public void TestDataTypes()
Assert.Equal("Double double", CreateCSharpDeclarationString(codeModel.Methods[0].Parameters[5]));
Assert.Equal("Decimal decimal", CreateCSharpDeclarationString(codeModel.Methods[0].Parameters[6]));
Assert.Equal("String string", CreateCSharpDeclarationString(codeModel.Methods[0].Parameters[7]));
Assert.Equal("enum color", CreateCSharpDeclarationString(codeModel.Methods[0].Parameters[8]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow how was enum color ever a valid declaration... I love our tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We simply wanted tests to pass.. ROFL

Assert.Equal("Color color", CreateCSharpDeclarationString(codeModel.Methods[0].Parameters[8]));
Assert.Equal("ByteArray byte", CreateCSharpDeclarationString(codeModel.Methods[0].Parameters[9]));
Assert.Equal("Boolean boolean", CreateCSharpDeclarationString(codeModel.Methods[0].Parameters[10]));
Assert.Equal("Date date", CreateCSharpDeclarationString(codeModel.Methods[0].Parameters[11]));
Expand All @@ -358,7 +358,7 @@ public void TestDataTypes()
new[] { new EnumValue { Name = "red" }, new EnumValue { Name = "blue" }, new EnumValue { Name = "green" } }
.ToList());
Assert.True(variableEnumInPath.ModelAsString);
Assert.Empty(variableEnumInPath.Name.RawValue);
Assert.Equal("Color", variableEnumInPath.Name.RawValue);

var variableEnumInQuery =
codeModel.Methods.First(m => m.Name == "List" && m.Group.IsNullOrEmpty())
Expand All @@ -372,7 +372,7 @@ public void TestDataTypes()
new EnumValue {Name = "purple"}
}.ToList());
Assert.True(variableEnumInQuery.ModelAsString);
Assert.Empty(variableEnumInQuery.Name.RawValue);
Assert.Equal("Color1", variableEnumInQuery.Name.RawValue);

var differentEnum =
codeModel.Methods.First(m => m.Name == "List" && m.Group == "DiffEnums")
Expand All @@ -382,7 +382,7 @@ public void TestDataTypes()
Assert.Equal(differentEnum.Values,
new[] { new EnumValue { Name = "cyan" }, new EnumValue { Name = "yellow" } }.ToList());
Assert.True(differentEnum.ModelAsString);
Assert.Empty(differentEnum.Name.RawValue);
Assert.Equal("Color3", differentEnum.Name.RawValue);

var sameEnum =
codeModel.Methods.First(m => m.Name == "Get" && m.Group == "SameEnums")
Expand All @@ -393,7 +393,7 @@ public void TestDataTypes()
new[] { new EnumValue { Name = "blue" }, new EnumValue { Name = "green" }, new EnumValue { Name = "red" } }
.ToList());
Assert.True(sameEnum.ModelAsString);
Assert.Empty(sameEnum.Name.RawValue);
Assert.Equal("Color21", sameEnum.Name.RawValue);

var modelEnum =
codeModel.ModelTypes.First(m => m.Name == "Product")
Expand All @@ -404,7 +404,7 @@ public void TestDataTypes()
new[] { new EnumValue { Name = "red" }, new EnumValue { Name = "blue" }, new EnumValue { Name = "green" } }
.ToList());
Assert.True(modelEnum.ModelAsString);
Assert.Empty(modelEnum.Name.RawValue);
Assert.Equal("Color2", modelEnum.Name.RawValue);

var fixedEnum =
codeModel.ModelTypes.First(m => m.Name == "Product")
Expand Down Expand Up @@ -435,7 +435,7 @@ public void TestDataTypes()
Assert.Equal("RefColors", refEnum.Name);


Assert.Equal(2, codeModel.EnumTypes.Count);
Assert.Equal(7, codeModel.EnumTypes.Count);
Assert.Equal("Colors", codeModel.EnumTypes.First().Name);
}

Expand Down Expand Up @@ -540,7 +540,7 @@ public void TestConstants()
Assert.Equal("0", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[8].DefaultValue);

Assert.Equal("RefStrEnum", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].Name);
Assert.Equal("enum", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].ModelType.Name);
Assert.Equal("RefStrEnum", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].ModelType.Name);
Assert.Equal(false, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].IsConstant);
Assert.True(codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].DefaultValue.IsNullOrEmpty());

Expand Down