Skip to content

Commit

Permalink
Fix #3316 allow generated enum members to be obsoleted (#3369)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6007a70)
  • Loading branch information
Mpdreamz committed Sep 3, 2018
1 parent 941e16e commit 7e3526f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public abstract class EndpointOverridesBase: IEndpointOverrides

public virtual IDictionary<string, string> ObsoleteQueryStringParams { get; set; } = new Dictionary<string, string>();


public virtual CsharpMethod PatchMethod(CsharpMethod method) => method;
}
}
6 changes: 6 additions & 0 deletions src/CodeGeneration/ApiGenerator/Overrides/GlobalOverrides.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using ApiGenerator.Overrides.Descriptors;

Expand All @@ -11,6 +12,11 @@ public class GlobalOverrides : EndpointOverridesBase
"script_fields",
"docvalue_fields"
};
public IDictionary<string, Dictionary<string, string>> ObsoleteEnumMembers { get; set; } = new Dictionary<string, Dictionary<string, string>>
{
{ "NodesStatsIndexMetric", new Dictionary<string, string>{{"suggest", "As of 5.0 this option always returned an empty object in the response"}}},
{ "IndicesStatsMetric", new Dictionary<string, string>{{"suggest", "Suggest stats have folded under the search stats, this alias will be removed"}}}
};

public override IDictionary<string, string> RenameQueryStringParams { get; } = new Dictionary<string, string>
{
Expand Down
9 changes: 8 additions & 1 deletion src/CodeGeneration/ApiGenerator/Views/Enums.Generated.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
@using System.Linq
@using ApiGenerator.Domain
@using ApiGenerator
@using ApiGenerator.Overrides
@functions {
private const string RawSize = "Raw";
private const string SizeEnum = "Size";
private static GlobalOverrides GlobalOverrides = new GlobalOverrides();

private string CreateEnum(string enumName, string value, int? i)
{
var enumValue = (enumName == SizeEnum && value == string.Empty) ? RawSize : value.ToPascalCase(true);
return string.Format("[EnumMember(Value = \"{0}\")] {1}{2}", value, enumValue, i.HasValue ? " = 1 << " + i.Value : null);
var enumCsharp = string.Format("[EnumMember(Value = \"{0}\")] {1}{2}", value, enumValue, i.HasValue ? " = 1 << " + i.Value : null);
if (GlobalOverrides.ObsoleteEnumMembers.TryGetValue(enumName, out var d) && d.TryGetValue(value, out var obsolete))
{
return string.Format("[Obsolete(\"{0}\")]\r\n\t\t{1}", obsolete, enumCsharp);
}
return enumCsharp;
}
private string CreateCase(string e, string o)
{
Expand Down
Loading

0 comments on commit 7e3526f

Please sign in to comment.