Skip to content

Commit

Permalink
Re-generate ingest namespace (opensearch-project#340)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Dec 3, 2023
1 parent b80dfe2 commit 941a649
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 239 deletions.
1 change: 1 addition & 0 deletions src/ApiGenerator/Configuration/CodeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static class CodeConfiguration
{
// e.g. new Glob("nodes.*"),
new("dangling_indices.*"),
new("ingest.*"),
new("tasks.*")
};

Expand Down
58 changes: 37 additions & 21 deletions src/ApiGenerator/Generator/ApiEndpointFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Net.Mime;
using ApiGenerator.Configuration;
using ApiGenerator.Configuration.Overrides;
using ApiGenerator.Domain;
Expand Down Expand Up @@ -76,7 +77,7 @@ public static ApiEndpoint From(string name, List<(string HttpPath, OpenApiPathIt
Name = p.Name,
Required = requiredPathParams?.Contains(p.Name) ?? false,
Type = GetOpenSearchType(p.Schema),
Options = GetEnumOptions(p.Schema),
Options = GetEnumOptions(p.Schema)
})
.ToImmutableSortedDictionary(p => p.Name, p => p);

Expand All @@ -90,7 +91,7 @@ public static ApiEndpoint From(string name, List<(string HttpPath, OpenApiPathIt
Description = p.Description,
Options = GetEnumOptions(p.Schema),
Deprecated = GetDeprecation(p.Schema),
VersionAdded = p.Schema.GetExtension("x-version-added") as string,
VersionAdded = p.Schema.XVersionAdded()
});

var endpoint = new ApiEndpoint
Expand All @@ -103,14 +104,14 @@ public static ApiEndpoint From(string name, List<(string HttpPath, OpenApiPathIt
OfficialDocumentationLink = new Documentation
{
Description = variants[0].Operation.Description,
Url = variants[0].Operation.ExternalDocumentation?.Url,
Url = variants[0].Operation.ExternalDocumentation?.Url
},
Url = urlInfo,
Body = variants.Select(v => v.Operation.RequestBody).FirstOrDefault(b => b != null) is {} reqBody ? new Body
{
Description = reqBody.Description,
Required = reqBody.IsRequired
} : null,
Body = variants
.Select(v => v.Operation.RequestBody)
.FirstOrDefault(b => b != null) is { } reqBody
? new Body { Description = GetDescription(reqBody), Required = reqBody.IsRequired }
: null,
HttpMethods = variants.Select(v => v.HttpMethod.ToString().ToUpper()).Distinct().ToList(),
};

Expand Down Expand Up @@ -138,10 +139,10 @@ private static void PatchRequestParameters(ApiEndpoint endpoint) =>
?? throw new ArgumentNullException("ApiQueryParametersPatcher.Patch(endpoint.Name, endpoint.Url.Params, endpoint.Overrides)");

private static string GetOpenSearchType(JsonSchema schema)
{
while (schema.HasReference) schema = schema.Reference;
{
schema = schema.ActualSchema;

if (schema.GetExtension("x-data-type") is string dataType)
if (schema.XDataType() is {} dataType)
return dataType == "array" ? "list" : dataType;

return schema.Type switch
Expand All @@ -153,23 +154,38 @@ private static string GetOpenSearchType(JsonSchema schema)
};
}

private static IEnumerable<string> GetEnumOptions(JsonSchema schema)
{
while (schema.HasReference) schema = schema.Reference;
private static IEnumerable<string> GetEnumOptions(JsonSchema schema) =>
schema.ActualSchema.Enumeration?.Select(e => e.ToString()) ?? Enumerable.Empty<string>();

return schema.Enumeration?.Select(e => e.ToString()) ?? Enumerable.Empty<string>();
}
private static QueryParameterDeprecation GetDeprecation(IJsonExtensionObject schema) =>
(schema.XDeprecationMessage(), schema.XVersionDeprecated()) switch
{
(null, null) => null,
var (m, v) => new QueryParameterDeprecation { Description = m, Version = v }
};

private static QueryParameterDeprecation GetDeprecation(IJsonExtensionObject schema)
private static string GetDescription(OpenApiRequestBody requestBody)
{
var message = schema.GetExtension("x-deprecation-message") as string;
var version = schema.GetExtension("x-version-deprecated") as string;
if (!string.IsNullOrWhiteSpace(requestBody.Description))
return requestBody.Description;

return message != null || version != null
? new QueryParameterDeprecation { Description = message, Version = version }
return requestBody.Content.TryGetValue(MediaTypeNames.Application.Json, out var content)
? content.Schema?.ActualSchema.Description
: null;
}

private static string XDeprecationMessage(this IJsonExtensionObject schema) =>
schema.GetExtension("x-deprecation-message") as string;

private static string XVersionDeprecated(this IJsonExtensionObject schema) =>
schema.GetExtension("x-version-deprecated") as string;

private static string XVersionAdded(this IJsonExtensionObject schema) =>
schema.GetExtension("x-version-added") as string;

private static string XDataType(this IJsonExtensionObject schema) =>
schema.GetExtension("x-data-type") as string;

private static object GetExtension(this IJsonExtensionObject schema, string key) =>
schema.ExtensionData?.TryGetValue(key, out var value) ?? false ? value : null;
}
Expand Down
Loading

0 comments on commit 941a649

Please sign in to comment.