Skip to content

Commit

Permalink
Add x-ms-enum's default to SwaggerObject. (Azure#9)
Browse files Browse the repository at this point in the history
* Adding model properties or parameters with "enum" constraint to the codeModel as EnumType with modelAsString: true

* temp fix

* Add x-ms-enum's default to SwaggerObject.

* setting the underlying type and removing the string type check

* Test modification for EnumType.UnderlyingType

* Adding global.json so that the project can build using the right version of dotnet

* address codereview feedback
  • Loading branch information
amarzavery authored Sep 21, 2017
1 parent e83172c commit cfabb17
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "2.0.0"
}
}
23 changes: 23 additions & 0 deletions src/Model/SwaggerObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 @@ -61,6 +62,28 @@ 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: 0 additions & 3 deletions src/Model/SwaggerParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ 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: 9 additions & 8 deletions src/ObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ 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 || xMsEnum != null) && type.KnownPrimaryType == KnownPrimaryType.String && !(IsSwaggerObjectConstant(SwaggerObject)))
if (SwaggerObject.Enum != null && !SwaggerObject.IsConstant)
{
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 @@ -68,11 +70,15 @@ public virtual IModelType BuildServiceType(string serviceTypeName)
var enumObject = xMsEnum as JContainer;
if (enumObject != null)
{
enumType.SetName(enumObject["name"].ToString() );
// set the enum name
enumType.SetName(enumObject["name"].ToString());

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

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

if (IsSwaggerObjectConstant(swaggerObject))
if (swaggerObject.IsConstant)
{
parameter.DefaultValue = swaggerObject.Enum[0];
parameter.IsConstant = true;
Expand All @@ -219,11 +225,6 @@ 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.21" />
<PackageReference Include="autorest.common" Version="2.2.22" />
<!-- <ProjectReference Include="../../autorest.common/src/autorest.common.csproj" /> -->
</ItemGroup>

Expand Down
3 changes: 2 additions & 1 deletion test/SwaggerModelerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,8 @@ 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.IsPrimaryType(KnownPrimaryType.Int));
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(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

0 comments on commit cfabb17

Please sign in to comment.