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

Re-generated client code using latest OpenSearch API specification #839

Merged
merged 7 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private GlobalOverrides() { }
{ "rest_total_hits_as_int", "total_hits_as_integer" },
{ "docvalue_fields", "doc_value_fields" },
{ "q", "query_on_query_string" },
{ "queryString", "query_string_param" },
//make cat parameters more descriptive
{ "h", "Headers" },
{ "s", "sort_by_columns" },
Expand Down
2 changes: 1 addition & 1 deletion src/ApiGenerator/Domain/ApiRequestParametersPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static string CreateCSharpName(string queryStringKey, string endpointNam
if (queryStringKey == "format" && endpointName == "text_structure.find_structure")
return "TextStructureFindStructureFormat";

return queryStringKey.ToPascalCase();
return queryStringKey.SplitPascalCase().ToPascalCase();
}

private static IList<string> CreateSkipList(IEndpointOverrides local, ICollection<string> declaredKeys) =>
Expand Down
8 changes: 5 additions & 3 deletions src/ApiGenerator/Domain/Code/CsharpNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,25 @@ private static string CreateCSharpNamespace(string endpointNamespace)

public string PerPathMethodName(string path)
{
Func<string, bool> ms = s => Namespace != null && Namespace.StartsWith(s);
Func<string, bool> pc = path.Contains;

var method = MethodName;
// This is temporary for transition period
// TODO: remove in branch once it in opensearch is scrubbed
if (pc("{type}") && !method.Contains("Type")) method += "UsingType";

if (ms("Indices") && !pc("{index}"))
if (Namespace == "Indices" && !pc("{index}"))
return (method + "ForAll").Replace("AsyncForAll", "ForAllAsync");

if (ms("Nodes") && !pc("{node_id}"))
if (Namespace == "Nodes" && !pc("{node_id}"))
return (method + "ForAll").Replace("AsyncForAll", "ForAllAsync");

if (Namespace == "Knn" && method.StartsWith("Stats") && !pc("{node_id}"))
return method.Replace("Stats", "StatsForAll");

if (Namespace == "Ml" && method == "GetStats" && !pc("{node_id}"))
return method.Replace("GetStats", "GetStatsForAll");

return method;
}

Expand Down
40 changes: 14 additions & 26 deletions src/ApiGenerator/Domain/Specification/QueryParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,33 +150,21 @@ public string TypeHighLevel
}
}

public string TypeLowLevel
{
get
public string TypeLowLevel =>
Type switch
{
switch (Type)
{
case "boolean": return "bool?";
case "list": return "string[]";
case "int": return "int?";
case "date": return "DateTimeOffset?";
case "enum": return $"{ClsName}?";
case "number":
return new[] { "boost", "percen", "score" }.Any(s => QueryStringKey.ToLowerInvariant().Contains(s))
? "double?"
: "long?";
case "duration":
case "time":
return "TimeSpan";
case "text":
case "":
case null:
return "string";
default:
return Type;
}
}
}
"boolean" => "bool?",
"list" => "string[]",
"int" => "int?",
"long" => "long?",
"date" => "DateTimeOffset?",
"enum" => $"{ClsName}?",
"float" => "float?",
"double" => "double?",
"duration" or "time" => "TimeSpan",
"text" or "" or null => "string",
_ => Type
};


public string InitializerGenerator(string @namespace, string type, string name, string key, string setter, Version versionAdded, params string[] doc) =>
Expand Down
31 changes: 12 additions & 19 deletions src/ApiGenerator/Domain/Specification/UrlPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,19 @@ public class UrlPart

public string Argument => $"{LowLevelTypeName} {NameAsArgument}";

public string LowLevelTypeName
{
get
public string LowLevelTypeName =>
//TODO treat list with fixed options as Flags Enum
Type switch
{
//TODO treat list with fixed options as Flags Enum
switch (Type)
{
case "int": //does not occur on part
case "number": //does not occur on part
case "string":
return Type;
case "list":
return "string";
case "enum":
return Name.ToPascalCase();
default:
return Type;
}
}
}
"string" => Type,
"int" => "int?",
"long" => "long?",
"float" => "float?",
"double" => "double?",
"list" => "string",
"enum" => Name.ToPascalCase(),
_ => Type
};

public string HighLevelTypeName
{
Expand Down
36 changes: 28 additions & 8 deletions src/ApiGenerator/Generator/ApiEndpointFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Action<string, bool> trackEnumToGenerate

Body body = null;
if (variants.Select(v => v.Operation.RequestBody).FirstOrDefault() is { } requestBody)
body = new Body { Description = GetDescription(requestBody)?.SanitizeDescription(), Required = requestBody.IsRequired };
body = new Body { Description = requestBody.GetDescription()?.SanitizeDescription(), Required = requestBody.IsRequired };

return new ApiEndpoint
{
Expand Down Expand Up @@ -193,9 +193,9 @@ List<PathParameter> paramCombo
private record PathParameter(string Name, JsonSchema Schema, string Description, bool IsDeprecated)
{
public PathParameter(OpenApiParameter parameter) :
this(parameter.Name, parameter.Schema, parameter.Description, parameter.IsDeprecated) { }
this(parameter.Name, parameter.Schema, parameter.GetDescription(), parameter.IsDeprecated) { }

public PathParameter(string name, OpenApiParameter parameter, JsonSchema schema) : this(name, schema, parameter.Description,
public PathParameter(string name, OpenApiParameter parameter, JsonSchema schema) : this(name, schema, parameter.GetDescription(),
parameter.IsDeprecated) { }
}

Expand All @@ -215,7 +215,7 @@ private static QueryParameters BuildQueryParam(OpenApiParameter p, Action<string
new()
{
Type = GetOpenSearchType(p.Schema, trackEnumToGenerate),
Description = p.Description?.SanitizeDescription(),
Description = p.GetDescription()?.SanitizeDescription(),
Deprecated = GetDeprecation(p) ?? GetDeprecation(p.ActualSchema),
VersionAdded = p.XVersionAdded(),
};
Expand Down Expand Up @@ -252,8 +252,9 @@ private static string GetOpenSearchType(JsonSchema schema, Action<string, bool>
{
(_, "list") => second,
("boolean", "string") => first,
("number", _) => "string",
(_, "number") => "string",
("int", _) => "string",
(_, "double") => "string",
(_, "int") => "string",
(_, _) => throw new Exception($"Unable to determine type of: {first} and {second}")
};
}
Expand All @@ -275,7 +276,9 @@ 2 when types.Contains(JsonObjectType.Boolean) && types.Contains(JsonObjectType.S

return type switch
{
JsonObjectType.Integer => "number",
JsonObjectType.Integer when schema.Format is null or "int32" => "int",
JsonObjectType.Integer when schema.Format == "int64" => "long",
JsonObjectType.Number => schema.Format ?? "double",
JsonObjectType.Array => "list",
JsonObjectType.String when schema.Pattern == @"^(?:(-1)|([0-9\.]+)(?:d|h|m|s|ms|micros|nanos))$" => "time",
var t => t.ToString().ToLowerInvariant()
Expand All @@ -289,7 +292,7 @@ private static Deprecation GetDeprecation(IJsonExtensionObject schema) =>
var (m, v) => new Deprecation { Description = m?.SanitizeDescription(), Version = v }
};

private static string GetDescription(OpenApiRequestBody requestBody)
private static string GetDescription(this OpenApiRequestBody requestBody)
{
if (!string.IsNullOrWhiteSpace(requestBody.Description))
return requestBody.Description;
Expand All @@ -299,6 +302,23 @@ private static string GetDescription(OpenApiRequestBody requestBody)
: null;
}

private static string GetDescription(this OpenApiParameter parameter) =>
!string.IsNullOrWhiteSpace(parameter.Description)
? parameter.Description
: parameter.Schema.GetDescription();

private static string GetDescription(this JsonSchema schema)
{
while (true)
{
if (!string.IsNullOrWhiteSpace(schema.Description)) return schema.Description;

if (!schema.HasReference) return null;

schema = schema.ActualSchema;
}
}

private static string SanitizeDescription(this string description)
{
if (string.IsNullOrWhiteSpace(description)) return null;
Expand Down
10 changes: 8 additions & 2 deletions src/ApiGenerator/Generator/OpenApiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,24 @@ public static bool IsEnum(this JsonSchema schema)

public static IImmutableList<string> GetEnumValues(this JsonSchema schema)
{
var normalized = new HashSet<string>();
var values = new SortedSet<string>();
Visit(schema);
return values.ToImmutableList();

void Add(string v)
{
if (normalized.Add(v.ToLowerInvariant())) values.Add(v);
}

void Visit(JsonSchema s)
{
if (s.HasOneOf())
foreach (var oneOf in schema.OneOf) Visit(oneOf);
else if (s.Enumeration.Count > 0)
foreach (var v in s.Enumeration.Where(v => v != null)) values.Add((string) v);
foreach (var v in s.Enumeration.Where(v => v != null)) Add((string) v);
else if (s.Const<string>() != null)
values.Add(s.Const<string>());
Add(s.Const<string>());
}
}

Expand Down
Loading
Loading