Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API generator to use new OpenAPI specification #609

Merged
merged 14 commits into from
Apr 17, 2024
Merged
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
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.27.3" />
<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.48.0" />
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
14 changes: 2 additions & 12 deletions src/ApiGenerator/Domain/ApiRequestParametersPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,8 @@ 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) =>
CreateList(local, "skip", e => e.SkipQueryStringParams, 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
Loading