Skip to content

Commit

Permalink
validate x-ms-enum values against enum values AND reject empty enums (A…
Browse files Browse the repository at this point in the history
  • Loading branch information
olydis authored Oct 27, 2017
1 parent 540502e commit 78d72e0
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions src/ObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,42 +63,53 @@ public virtual IModelType BuildServiceType(string serviceTypeName)
enumType.UnderlyingType = type;
if (SwaggerObject.Enum != null)
{
if (SwaggerObject.Enum.Count == 0)
{
throw new InvalidOperationException($"Found an 'enum' with no values. Please remove this (unsatisfiable) restriction or add values.");
}
SwaggerObject.Enum.ForEach(v => enumType.Values.Add(new EnumValue { Name = v, SerializedName = v }));
}
if (xMsEnum != null)
if (xMsEnum is JContainer enumObject)
{
if (xMsEnum is JContainer enumObject)
var enumName = "" + enumObject["name"];
if (string.IsNullOrEmpty(enumName))
{
throw new InvalidOperationException($"{Core.Model.XmsExtensions.Enum.Name} extension needs to specify an enum name.");
}
enumType.SetName(enumName);

if (enumObject["modelAsString"] != null)
{
enumType.ModelAsString = bool.Parse(enumObject["modelAsString"].ToString());
}
var valueOverrides = enumObject["values"] as JArray;
if (valueOverrides != null)
{
enumType.SetName("" + enumObject["name"]);
if (enumObject["modelAsString"] != null)
var valuesBefore = new HashSet<string>(enumType.Values.Select(x => x.SerializedName));
enumType.Values.Clear();
foreach (var valueOverride in valueOverrides)
{
enumType.ModelAsString = bool.Parse(enumObject["modelAsString"].ToString());
var value = valueOverride["value"];
var description = valueOverride["description"];
var name = valueOverride["name"] ?? value;
enumType.Values.Add(new EnumValue
{
Name = (string)name,
SerializedName = (string)value,
Description = (string)description
});
}
var valueOverrides = enumObject["values"] as JArray;
if (valueOverrides != null)
var valuesAfter = new HashSet<string>(enumType.Values.Select(x => x.SerializedName));
// compare values
if (!valuesBefore.SetEquals(valuesAfter))
{
enumType.Values.Clear();
foreach (var valueOverride in valueOverrides)
{
var value = valueOverride["value"];
var description = valueOverride["description"];
var name = valueOverride["name"] ?? value;
enumType.Values.Add(new EnumValue
{
Name = (string)name,
SerializedName = (string)value,
Description = (string)description
});
}
throw new InvalidOperationException($"Values specified by 'enum' mismatch those specified by 'x-ms-enum' (name: '{enumName}'): "
+ string.Join(", ", valuesBefore.Select(x => $"'{x}'"))
+ " vs "
+ string.Join(", ", valuesAfter.Select(x => $"'{x}'")));
}
}
if (string.IsNullOrEmpty(enumType.Name))
{
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture,
"{0} extension needs to specify an enum name.",
Core.Model.XmsExtensions.Enum.Name));
}

var existingEnum =
Modeler.CodeModel.EnumTypes.FirstOrDefault(
e => e.Name.RawValue.EqualsIgnoreCase(enumType.Name.RawValue));
Expand Down

0 comments on commit 78d72e0

Please sign in to comment.