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

Revert "Add x-ms-enum's default to SwaggerObject." #11

Merged
merged 2 commits into from
Sep 22, 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
5 changes: 0 additions & 5 deletions global.json

This file was deleted.

23 changes: 0 additions & 23 deletions src/Model/SwaggerObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using AutoRest.Modeler.Properties;
using Newtonsoft.Json;
using static AutoRest.Core.Utilities.DependencyInjection;
using Newtonsoft.Json.Linq;

namespace AutoRest.Modeler.Model
{
Expand Down Expand Up @@ -62,28 +61,6 @@ public virtual string Description
/// </summary>
public virtual CollectionFormat CollectionFormat { get; set; }

/// <summary>
/// Determines whether the enum should be treated as a constant.
/// If the enum contains exactly one value and is a constraint on a
/// required parameter or a required property and has modelAsString set to true.
/// Value for "modelAsString" is deduced based on the following conditions:
/// - xmsEnum extension applied like this: "x-ms-enum": { "modelAsString": true }.
/// - xmsEnum extension not applied at all. Then we treat it as if it was applied like above.
/// </summary>
[JsonIgnore]
public bool IsConstant {
get
{
var xmsEnum = Extensions.GetValue<JToken>(Core.Model.XmsExtensions.Enum.Name) as JContainer;
var modelAsString = true;
if (xmsEnum?["modelAsString"] != null)
{
modelAsString = bool.Parse(xmsEnum["modelAsString"].ToString());
}
return IsRequired && Enum != null && Enum.Count == 1 && modelAsString;
}
}

/// <summary>
/// Sets a default value to the parameter.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Model/SwaggerParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public override bool IsRequired
set { _isRequired = value; }
}

[JsonIgnore]
public bool IsConstant => IsRequired && Enum != null && Enum.Count == 1;

/// <summary>
/// The schema defining the type used for the body parameter.
/// </summary>
Expand Down
17 changes: 8 additions & 9 deletions src/ObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,9 @@ public virtual IModelType BuildServiceType(string serviceTypeName)
type.XmlProperties = (SwaggerObject as Schema)?.Xml;
type.Format = SwaggerObject.Format;
var xMsEnum = SwaggerObject.Extensions.GetValue<JToken>(Core.Model.XmsExtensions.Enum.Name);
if (SwaggerObject.Enum != null && !SwaggerObject.IsConstant)
if ((SwaggerObject.Enum != null || xMsEnum != null) && type.KnownPrimaryType == KnownPrimaryType.String && !(IsSwaggerObjectConstant(SwaggerObject)))
{
var enumType = New<EnumType>();
// Set the underlying type. This helps to determine whether the values in EnumValue are of type string, number, etc.
enumType.UnderlyingType = type;
if (SwaggerObject.Enum != null)
{
SwaggerObject.Enum.ForEach(v => enumType.Values.Add(new EnumValue { Name = v, SerializedName = v }));
Expand All @@ -70,15 +68,11 @@ public virtual IModelType BuildServiceType(string serviceTypeName)
var enumObject = xMsEnum as JContainer;
if (enumObject != null)
{
// set the enum name
enumType.SetName(enumObject["name"].ToString());

// process modelAsString
enumType.SetName(enumObject["name"].ToString() );
if (enumObject["modelAsString"] != null)
{
enumType.ModelAsString = bool.Parse(enumObject["modelAsString"].ToString());
}

var valueOverrides = enumObject["values"] as JArray;
if (valueOverrides != null)
{
Expand Down Expand Up @@ -210,7 +204,7 @@ public static void PopulateParameter(IVariable parameter, SwaggerObject swaggerO
parameter.IsRequired = swaggerObject.IsRequired;
parameter.DefaultValue = swaggerObject.Default;

if (swaggerObject.IsConstant)
if (IsSwaggerObjectConstant(swaggerObject))
{
parameter.DefaultValue = swaggerObject.Enum[0];
parameter.IsConstant = true;
Expand All @@ -225,6 +219,11 @@ public static void PopulateParameter(IVariable parameter, SwaggerObject swaggerO
SetConstraints(parameter.Constraints, swaggerObject);
}

private static bool IsSwaggerObjectConstant(SwaggerObject swaggerObject)
{
return (swaggerObject.Enum != null && swaggerObject.Enum.Count == 1 && swaggerObject.IsRequired);
}

public static void SetConstraints(Dictionary<Constraint, string> constraints, SwaggerObject swaggerObject)
{
if (constraints == null)
Expand Down
2 changes: 1 addition & 1 deletion src/autorest.modeler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="YamlDotNet.Signed" Version="4.2.1" />
<PackageReference Include="autorest.common" Version="2.2.22" />
<PackageReference Include="autorest.common" Version="2.2.21" />
<!-- <ProjectReference Include="../../autorest.common/src/autorest.common.csproj" /> -->
</ItemGroup>

Expand Down
3 changes: 1 addition & 2 deletions test/SwaggerModelerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,7 @@ public void TestConstants()
Assert.True(codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].DefaultValue.IsNullOrEmpty());

Assert.Equal("RefIntEnum", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].Name);
Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].ModelType is EnumType);
Assert.Equal(true, (codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].ModelType as EnumType).UnderlyingType.IsPrimaryType(KnownPrimaryType.Int));
Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].ModelType.IsPrimaryType(KnownPrimaryType.Int));
Assert.Equal(false, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].IsConstant);
Assert.True(codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].DefaultValue.IsNullOrEmpty());

Expand Down