From 78d72e05a9847edffde212153fdb65139b210c55 Mon Sep 17 00:00:00 2001 From: Johannes Bader Date: Fri, 27 Oct 2017 10:45:55 -0700 Subject: [PATCH] validate x-ms-enum values against enum values AND reject empty enums (#25) --- src/ObjectBuilder.cs | 65 ++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/src/ObjectBuilder.cs b/src/ObjectBuilder.cs index b06b19a..db5df0b 100644 --- a/src/ObjectBuilder.cs +++ b/src/ObjectBuilder.cs @@ -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(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(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));