Skip to content

Commit

Permalink
Update API generator to use new OpenAPI specification (opensearch-pro…
Browse files Browse the repository at this point in the history
…ject#609)

* Update RestSpecDownloader

Signed-off-by: Thomas Farr <[email protected]>

* Improve Enum formatting

Signed-off-by: Thomas Farr <[email protected]>

* Patch request body references

Signed-off-by: Thomas Farr <[email protected]>

* Description changes

Signed-off-by: Thomas Farr <[email protected]>

* Improve type mapping

Signed-off-by: Thomas Farr <[email protected]>

* Correct component and composable template APIs which do not actually accept comma-separated lists.

Signed-off-by: Thomas Farr <[email protected]>

* Unify enums `Health` & `WaitForStatus` to `HealthStatus`

Signed-off-by: Thomas Farr <[email protected]>

* Change `NodeNames` & `NodeIds` from `string` to `string[]`

Signed-off-by: Thomas Farr <[email protected]>

* Rename `GroupBy` enum to `TasksGroupBy`

Signed-off-by: Thomas Farr <[email protected]>

* Correct `Level` params

Signed-off-by: Thomas Farr <[email protected]>

* Rename `SampleType` enum to `NodesSampleType`

Signed-off-by: Thomas Farr <[email protected]>

* Remove `QueryString` suffixing

Signed-off-by: Thomas Farr <[email protected]>

* Improve enum generation

Signed-off-by: Thomas Farr <[email protected]>

* Re-generate enums

Signed-off-by: Thomas Farr <[email protected]>

---------

Signed-off-by: Thomas Farr <[email protected]>
(cherry picked from commit 553f7d1)
  • Loading branch information
Xtansia committed Aug 14, 2024
1 parent 119590e commit fa69c59
Show file tree
Hide file tree
Showing 34 changed files with 42,175 additions and 33,858 deletions.
20 changes: 4 additions & 16 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,19 @@ root=true
[*]
charset = utf-8
end_of_line = lf
indent_style = tab
indent_size = 4
insert_final_newline=true

[*.cs]
trim_trailing_whitespace=true

[*.cshtml]
indent_style = tab
indent_size = 4

[*.{fs,fsx}]
indent_style = space
indent_size = 4
insert_final_newline=true

[*.yml]
indent_style = space
indent_size = 2

[*.{md,markdown,json,js,csproj,fsproj,targets,targets,props}]
indent_style = space
[*.{md,markdown,json,js,csproj,fsproj,targets,props}]
indent_size = 2

# Dotnet code style settings:
[*.{cs,vb}]
trim_trailing_whitespace = true

# ---
# naming conventions https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-naming-conventions
Expand Down Expand Up @@ -171,7 +159,7 @@ csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
# just a suggestion do to our JSON tests that use anonymous types to
# just a suggestion do to our JSON tests that use anonymous types to
# represent json quite a bit (makes copy paste easier).
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true
Expand Down
6 changes: 4 additions & 2 deletions build/scripts/scripts.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@
<Content Include="..\..\.github\license-header-fs.txt"><Link>license-header-fs.txt</Link></Content>
</ItemGroup>
<ItemGroup>
<PackageReference Update="FSharp.Core" Version="8.0.301" />

<PackageReference Include="Bullseye" Version="5.0.0" />
<ProjectReference Include="$(SolutionRoot)\abstractions\src\OpenSearch.OpenSearch.Managed\OpenSearch.OpenSearch.Managed.csproj" />

<PackageReference Include="Fake.Core.Environment" Version="6.1.0" />
<PackageReference Include="Fake.Core.SemVer" Version="6.1.0" />
<PackageReference Include="Fake.IO.FileSystem" Version="6.1.0" />
<PackageReference Include="Fake.IO.Zip" Version="6.1.0" />
<PackageReference Include="Fake.Tools.Git" Version="5.23.1" />

<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

<PackageReference Include="Octokit" Version="13.0.1" />
Expand Down
5 changes: 3 additions & 2 deletions src/ApiGenerator/ApiGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>

<NoWarn>CS1591;NU1701</NoWarn>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CSharpier.Core" Version="0.28.2" />
<PackageReference Include="Glob" Version="1.1.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NSwag.Core.Yaml" Version="14.0.7" />
<PackageReference Include="NSwag.Core" Version="14.0.7" />
<PackageReference Include="YamlDotNet" Version="15.1.2" />
<PackageReference Include="SemanticVersioning" Version="2.0.2" />
<PackageReference Include="ShellProgressBar" Version="5.2.0" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
Expand Down
50 changes: 24 additions & 26 deletions src/ApiGenerator/Configuration/GeneratorLocations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,42 @@

namespace ApiGenerator.Configuration
{
public static class GeneratorLocations
{
// @formatter:off — disable formatter after this line
public static string OpenApiSpecFile { get; } = $@"{Root}OpenSearch.openapi.json";
public static class GeneratorLocations
{
public static string OpenApiSpecFile { get; } = $"{Root}opensearch-openapi.yaml";

public static string OpenSearchNetFolder { get; } = $@"{Root}../../src/OpenSearch.Net/";
public static string OpenSearchNetFolder { get; } = $"{Root}../../src/OpenSearch.Net/";

public static string OpenSearchClientFolder { get; } = $@"{Root}../../src/OpenSearch.Client/";
public static string OpenSearchClientFolder { get; } = $"{Root}../../src/OpenSearch.Client/";

public static string LowLevelGeneratedFolder { get; } = $"{OpenSearchNetFolder}_Generated/";

public static string HighLevelGeneratedFolder { get; } = $"{OpenSearchClientFolder}_Generated/";

// @formatter:on — enable formatter after this line
public static string HighLevel(params string[] paths) => HighLevelGeneratedFolder + string.Join("/", paths);

public static string HighLevel(params string[] paths) => HighLevelGeneratedFolder + string.Join("/", paths);
public static string LowLevel(params string[] paths) => LowLevelGeneratedFolder + string.Join("/", paths);
public static string LowLevel(params string[] paths) => LowLevelGeneratedFolder + string.Join("/", paths);

public static readonly Assembly Assembly = typeof(Generator.ApiGenerator).Assembly;
public static readonly Assembly Assembly = typeof(Generator.ApiGenerator).Assembly;

private static string _root;
public static string Root
{
get
{
if (_root != null) return _root;
private static string _root;

var directoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
public static string Root
{
get
{
if (_root != null) return _root;

var dotnetRun =
directoryInfo.Name == "ApiGenerator" &&
directoryInfo.Parent != null &&
directoryInfo.Parent.Name == "src";
var directoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());

_root = dotnetRun ? "" : @"../../../";
return _root;
}
}
var dotnetRun =
directoryInfo.Name == "ApiGenerator" &&
directoryInfo.Parent != null &&
directoryInfo.Parent.Name == "src";

}
_root = dotnetRun ? "" : "../../../";
return _root;
}
}
}
}
8 changes: 4 additions & 4 deletions src/ApiGenerator/Configuration/Overrides/GlobalOverrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class GlobalOverrides : EndpointOverridesBase

private GlobalOverrides() { }

public IDictionary<string, Dictionary<string, string>> ObsoleteEnumMembers { get; set; } = new Dictionary<string, Dictionary<string, string>>()
{
{ "VersionType", new Dictionary<string, string>() { { "force", "Force is no longer accepted by the server as of 7.5.0 and will result in an error when used" } } }
};
public IDictionary<string, string> RenameEnums { get; } = new Dictionary<string, string>
{
{ "ExpandWildcard", "ExpandWildcards" }
};

public override IDictionary<string, string> ObsoleteQueryStringParams { get; set; } = new Dictionary<string, string>
{
Expand Down
12 changes: 1 addition & 11 deletions src/ApiGenerator/Domain/ApiRequestParametersPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,7 @@ private static string CreateCSharpName(string queryStringKey, string endpointNam
if (queryStringKey == "format" && endpointName == "text_structure.find_structure")
return "TextStructureFindStructureFormat";

var cased = queryStringKey.ToPascalCase();
switch (cased)
{
case "Type":
case "Index":
case "Source":
case "Script":
return cased + "QueryString";
default:
return cased;
}
return queryStringKey.ToPascalCase();
}

private static IList<string> CreateSkipList(IEndpointOverrides local, ICollection<string> declaredKeys) =>
Expand Down
9 changes: 9 additions & 0 deletions src/ApiGenerator/Domain/Code/CsharpNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.Collections.Generic;
using System.Linq;
using ApiGenerator.Configuration;
using ApiGenerator.Configuration.Overrides;
using ApiGenerator.Generator;

namespace ApiGenerator.Domain.Code
Expand Down Expand Up @@ -103,6 +104,7 @@ public string ResponseName
public const string LowLevelClientNamespacePrefix = "LowLevel";
public const string HighLevelClientNamespacePrefix = "";
public const string ClientNamespaceSuffix = "Namespace";

private static string CreateCSharpNamespace(string endpointNamespace)
{
switch (endpointNamespace)
Expand Down Expand Up @@ -203,5 +205,12 @@ public string GenericOrNonGenericResponseName
: RequestName;

public bool CustomResponseBuilderPerRequestOverride(out string call) => CodeConfiguration.ResponseBuilderInClientCalls.TryGetValue(RequestName, out call);

public static string GetEnumName(string schemaKey)
{
var enumName = schemaKey.Replace("_common", "").SplitPascalCase().ToPascalCase();
if (GlobalOverrides.Instance.RenameEnums.TryGetValue(enumName, out var renamed)) enumName = renamed;
return enumName;
}
}
}
45 changes: 2 additions & 43 deletions src/ApiGenerator/Domain/RestApiSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace ApiGenerator.Domain
public class EnumDescription
{
public string Name { get; set; }
public bool IsFlag { get; set; }
public IEnumerable<string> Options { get; set; }
}

Expand All @@ -55,48 +56,6 @@ public class RestApiSpec
.GroupBy(e => e.CsharpNames.Namespace)
.ToImmutableSortedDictionary(kv => kv.Key, kv => kv.ToList().AsReadOnly());


private IEnumerable<EnumDescription> _enumDescriptions;
public IEnumerable<EnumDescription> EnumsInTheSpec
{
get
{
if (_enumDescriptions != null) return _enumDescriptions;

var urlParameterEnums = Endpoints
.Values
.SelectMany(e => e.Url.Params.Values)
.Where(p => !p.Skip && p.Options != null && p.Options.Any())
.Select(p => new EnumDescription
{
Name = p.ClsName,
Options = p.Options
})
.ToList();

var urlPartEnums = Endpoints
.Values
.SelectMany(e => e.Url.Parts, (e, part) => new { e, part })
.Where(p => p.part.Options != null && p.part.Options.Any())
.Select(p =>
{
var ns = p.e.CsharpNames.Namespace;
var m = p.e.CsharpNames.MethodName;
return new EnumDescription
{
Name = (!m.StartsWith(ns) ? ns : string.Empty) + m + p.part.Name.ToPascalCase(),
Options = p.part.Options
};
}).
ToList();

_enumDescriptions = urlPartEnums
.Concat(urlParameterEnums)
.DistinctBy(e => e.Name)
.ToList();

return _enumDescriptions;
}
}
public ImmutableList<EnumDescription> EnumsInTheSpec { get; set; }
}
}
1 change: 0 additions & 1 deletion src/ApiGenerator/Domain/Specification/QueryParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public string Obsolete

public Deprecation Deprecated { get; set; }

public IEnumerable<string> Options { get; set; }
public string QueryStringKey { get; set; }

public bool RenderPartial { get; set; }
Expand Down
1 change: 0 additions & 1 deletion src/ApiGenerator/Domain/Specification/UrlPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public string Description

public string Name { get; set; }
public string NameAsArgument => Name.ToCamelCase();
public IEnumerable<string> Options { get; set; }
public bool Required { get; set; }
public bool Deprecated { get; set; }
public string Type { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/ApiGenerator/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static class Extensions
/// <summary>
/// Removes _ . but not an underscore at the start of the string, unless the string is _all or removeLeadingUnderscore == true.
/// </summary>
private static readonly Regex RemovePunctuationExceptFirstUnderScore = new Regex(@"(?!^_(?!All$))[_\.]");
private static readonly Regex RemovePunctuationExceptFirstUnderScore = new(@"(?!^_(?!All$))[_\-\.: ]");

public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property) =>
items.GroupBy(property).Select(x => x.First());
Expand Down
Loading

0 comments on commit fa69c59

Please sign in to comment.