Skip to content

Commit 6da223b

Browse files
authored
Update REST API specs to v7.4.0 (#4117)
* Transform REST API spec to existing format This commit changes the in memory structure of the ApiEndpoint in 7.4.0 to the old structure expected by the ApiGenerator. * Update specs to v7.4.0 This commit updates the REST API specs to those in the v7.4.0 branch, and re-runs the ApiGenerator. New 7.4.0 APIs are excluded from code generation for now. * Patch 7.4.0 breaking changes - delete_by_query - msearch_template - search_template
1 parent 33a0149 commit 6da223b

File tree

326 files changed

+14204
-9863
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

326 files changed

+14204
-9863
lines changed

src/CodeGeneration/ApiGenerator/Configuration/CodeConfiguration.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static class CodeConfiguration
2626
"data_frame.put_data_frame_transform.json",
2727
"data_frame.start_data_frame_transform.json",
2828
"data_frame.stop_data_frame_transform.json",
29+
"data_frame.update_data_frame_transform.json",
2930

3031
"ml.evaluate_data_frame.json",
3132
"ml.delete_data_frame_analytics.json",
@@ -41,7 +42,15 @@ public static class CodeConfiguration
4142
// these APIs are new and need to be mapped
4243
"ml.set_upgrade_mode.json",
4344
"ml.find_file_structure.json",
44-
"monitoring.bulk.json"
45+
"monitoring.bulk.json",
46+
"snapshot.cleanup_repository.json",
47+
"ml.estimate_memory_usage.json",
48+
"indices.clone.json",
49+
50+
"slm.delete_lifecycle.json",
51+
"slm.execute_lifecycle.json",
52+
"slm.get_lifecycle.json",
53+
"slm.put_lifecycle.json",
4554
};
4655

4756

src/CodeGeneration/ApiGenerator/Domain/Specification/ApiEndpoint.cs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using ApiGenerator.Configuration.Overrides;
@@ -7,6 +8,7 @@
78
using ApiGenerator.Domain.Code.LowLevel;
89
using Newtonsoft.Json;
910
using Newtonsoft.Json.Converters;
11+
using Newtonsoft.Json.Linq;
1012

1113
namespace ApiGenerator.Domain.Specification
1214
{
@@ -32,7 +34,7 @@ public class ApiEndpoint
3234
public Stability Stability { get; set; }
3335

3436
[JsonProperty("documentation")]
35-
public string OfficialDocumentationLink { get; set; }
37+
public Documentation OfficialDocumentationLink { get; set; }
3638

3739
public UrlInformation Url { get; set; }
3840

@@ -48,13 +50,13 @@ public class ApiEndpoint
4850
CsharpNames = CsharpNames,
4951
UrlParts = Url.Parts,
5052
PartialParameters = Body == null ? Enumerable.Empty<QueryParameters>().ToList() : Url.Params.Values.Where(p=>p.RenderPartial && !p.Skip).ToList(),
51-
OfficialDocumentationLink = OfficialDocumentationLink
53+
OfficialDocumentationLink = OfficialDocumentationLink.Url
5254
};
5355

5456
public RequestPartialImplementation RequestPartialImplementation => new RequestPartialImplementation
5557
{
5658
CsharpNames = CsharpNames,
57-
OfficialDocumentationLink = OfficialDocumentationLink,
59+
OfficialDocumentationLink = OfficialDocumentationLink.Url,
5860
Stability = Stability,
5961
Paths = Url.Paths,
6062
Parts = Url.Parts,
@@ -67,7 +69,7 @@ public class ApiEndpoint
6769
public DescriptorPartialImplementation DescriptorPartialImplementation => new DescriptorPartialImplementation
6870
{
6971
CsharpNames = CsharpNames,
70-
OfficialDocumentationLink = OfficialDocumentationLink,
72+
OfficialDocumentationLink = OfficialDocumentationLink.Url,
7173
Constructors = Constructor.DescriptorConstructors(CsharpNames, Url).ToList(),
7274
Paths = Url.Paths,
7375
Parts = Url.Parts,
@@ -78,7 +80,7 @@ public class ApiEndpoint
7880
public RequestParameterImplementation RequestParameterImplementation => new RequestParameterImplementation
7981
{
8082
CsharpNames = CsharpNames,
81-
OfficialDocumentationLink = OfficialDocumentationLink,
83+
OfficialDocumentationLink = OfficialDocumentationLink.Url,
8284
Params = Url.Params.Values.Where(p=>!p.Skip).ToList(),
8385
HttpMethod = PreferredHttpMethod
8486
};
@@ -101,16 +103,16 @@ public string PreferredHttpMethod
101103
CsharpNames = CsharpNames,
102104
Fluent = new FluentMethod(CsharpNames, Url.Parts,
103105
selectorIsOptional: Body == null || !Body.Required || HttpMethods.Contains("GET"),
104-
link: OfficialDocumentationLink,
106+
link: OfficialDocumentationLink.Url,
105107
summary: HighLevelMethodXmlDocDescription
106108
),
107109
FluentBound = !CsharpNames.DescriptorBindsOverMultipleDocuments ? null : new BoundFluentMethod(CsharpNames, Url.Parts,
108110
selectorIsOptional: Body == null || !Body.Required || HttpMethods.Contains("GET"),
109-
link: OfficialDocumentationLink,
111+
link: OfficialDocumentationLink.Url,
110112
summary: HighLevelMethodXmlDocDescription
111113
),
112114
Initializer = new InitializerMethod(CsharpNames,
113-
link: OfficialDocumentationLink,
115+
link: OfficialDocumentationLink.Url,
114116
summary: HighLevelMethodXmlDocDescription
115117
)
116118
};
@@ -145,7 +147,7 @@ public IReadOnlyCollection<LowLevelClientMethod> LowLevelClientMethods
145147
CsharpNames = CsharpNames,
146148
PerPathMethodName = methodName,
147149
HttpMethod = httpMethod,
148-
OfficialDocumentationLink = OfficialDocumentationLink,
150+
OfficialDocumentationLink = OfficialDocumentationLink.Url,
149151
Stability = Stability,
150152
DeprecatedPath = path.Deprecation,
151153
Path = path.Path,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace ApiGenerator.Domain.Specification
6+
{
7+
[JsonConverter(typeof(DocumentationConverter))]
8+
public class Documentation
9+
{
10+
public string Description { get; set; }
11+
public string Url { get; set; }
12+
}
13+
14+
public class DocumentationConverter : JsonConverter
15+
{
16+
public override bool CanWrite { get; } = false;
17+
18+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotSupportedException();
19+
20+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
21+
{
22+
23+
var documentation = new Documentation();
24+
25+
if (reader.TokenType == JsonToken.String)
26+
{
27+
documentation.Url = (string)reader.Value;
28+
return documentation;
29+
}
30+
31+
while (reader.Read())
32+
{
33+
if (reader.TokenType == JsonToken.EndObject)
34+
break;
35+
36+
var prop = (string)reader.Value;
37+
switch (prop)
38+
{
39+
case "url":
40+
documentation.Url = reader.ReadAsString();
41+
break;
42+
case "description":
43+
documentation.Description = reader.ReadAsString();
44+
break;
45+
default:
46+
throw new Exception($"Property '{prop}' unexpected in documentation object");
47+
}
48+
}
49+
return documentation;
50+
}
51+
52+
public override bool CanConvert(Type objectType) => true;
53+
}
54+
}

src/CodeGeneration/ApiGenerator/Generator/ApiEndpointFactory.cs

+79-6
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,37 @@
77
using ApiGenerator.Domain;
88
using ApiGenerator.Domain.Code;
99
using ApiGenerator.Domain.Specification;
10+
using Microsoft.Extensions.DependencyModel;
1011
using Newtonsoft.Json.Linq;
1112

12-
namespace ApiGenerator.Generator
13+
namespace ApiGenerator.Generator
1314
{
1415
public static class ApiEndpointFactory
1516
{
1617
public static ApiEndpoint FromFile(string jsonFile)
1718
{
1819
var officialJsonSpec = JObject.Parse(File.ReadAllText(jsonFile));
20+
TransformNewSpecStructureToOld(officialJsonSpec);
1921
PatchOfficialSpec(officialJsonSpec, jsonFile);
2022
var (name, endpoint) = officialJsonSpec.ToObject<Dictionary<string, ApiEndpoint>>().First();
21-
23+
2224
endpoint.FileName = Path.GetFileName(jsonFile);
2325
endpoint.Name = name;
2426
var tokens = name.Split(".");
25-
27+
2628
endpoint.MethodName = tokens.Last();
2729
if (tokens.Length > 1)
2830
endpoint.Namespace = tokens[0];
2931
//todo side effect
3032
endpoint.CsharpNames = new CsharpNames(name, endpoint.MethodName, endpoint.Namespace);
31-
33+
3234
LoadOverridesOnEndpoint(endpoint);
3335
PatchRequestParameters(endpoint);
3436

3537
EnforceRequiredOnParts(jsonFile, endpoint.Url);
3638
return endpoint;
3739
}
38-
40+
3941
/// <summary>
4042
/// This makes sure required is configured correctly by inspecting the paths.
4143
/// Will emit a warning if the spec file got this wrong
@@ -70,7 +72,7 @@ private static void PatchRequestParameters(ApiEndpoint endpoint)
7072
var newParams = ApiQueryParametersPatcher.Patch(endpoint.Name, endpoint.Url.Params, endpoint.Overrides);
7173
endpoint.Url.Params = newParams;
7274
}
73-
75+
7476
/// <summary>
7577
/// Finds a patch file in patches and union merges this with the official spec.
7678
/// This allows us to check in tweaks should breaking changes occur in the spec before we catch them
@@ -102,5 +104,76 @@ void ReplaceOptions(string path)
102104
ReplaceOptions("*.url.parts.metric.options");
103105
ReplaceOptions("*.url.parts.index_metric.options");
104106
}
107+
108+
/// <summary>
109+
/// Changes the structure of new REST API spec in 7.4.0 to one that matches prior spec structure.
110+
/// </summary>
111+
private static void TransformNewSpecStructureToOld(JObject original)
112+
{
113+
var name = (JProperty)original.First;
114+
var spec = (JObject)name.Value;
115+
116+
// old spec structure, nothing to change
117+
if (spec.ContainsKey("methods"))
118+
return;
119+
120+
var methods = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
121+
JObject parts = null;
122+
var paths = new List<string>();
123+
var deprecatedPaths = new List<JObject>();
124+
125+
foreach (var path in spec["url"]["paths"].Cast<JObject>())
126+
{
127+
if (path.ContainsKey("deprecated"))
128+
{
129+
var deprecated = new JObject
130+
{
131+
["version"] = path["deprecated"]["version"].Value<string>(),
132+
["path"] = path["path"].Value<string>(),
133+
["description"] = path["deprecated"]["description"].Value<string>()
134+
};
135+
136+
deprecatedPaths.Add(deprecated);
137+
}
138+
else
139+
paths.Add(path["path"].Value<string>());
140+
141+
if (path.ContainsKey("parts"))
142+
{
143+
if (parts == null)
144+
parts = path["parts"].Value<JObject>();
145+
else
146+
parts.Merge(path["parts"].Value<JObject>(), new JsonMergeSettings
147+
{
148+
MergeArrayHandling = MergeArrayHandling.Union
149+
});
150+
}
151+
152+
foreach (var method in path["methods"].Cast<JValue>())
153+
methods.Add(method.Value<string>());
154+
}
155+
156+
157+
158+
var newUrl = new JObject
159+
{
160+
["paths"] = new JArray(paths.ToArray()),
161+
};
162+
163+
if (spec.ContainsKey("params"))
164+
{
165+
newUrl["params"] = spec["params"];
166+
spec.Remove("params");
167+
}
168+
169+
if (parts != null)
170+
newUrl["parts"] = parts;
171+
172+
if (deprecatedPaths.Any())
173+
newUrl["deprecated_paths"] = new JArray(deprecatedPaths.ToArray());
174+
175+
spec["url"] = newUrl;
176+
spec["methods"] = new JArray(methods.ToArray());
177+
}
105178
}
106179
}

0 commit comments

Comments
 (0)