From 7a687898f5783c64a63122613420e288be82600f Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Wed, 25 Sep 2024 03:59:23 +1200 Subject: [PATCH] Better handle multi-type schemas Signed-off-by: Thomas Farr (cherry picked from commit 49f4b018be6728ed2f566c1defebddf0b63b866c) --- .../run-released-opensearch/action.yml | 4 +++ .github/actions/start-opensearch/action.yml | 8 +++-- .github/workflows/integration-yaml-tests.yml | 1 + src/ApiGenerator/Domain/Code/CsharpNames.cs | 5 +++- .../Generator/ApiEndpointFactory.cs | 29 ++++++++++++++----- .../Requests/PlainRequestBase.cshtml | 18 ++---------- src/OpenSearch.Client/_Generated/Requests.cs | 21 ++------------ 7 files changed, 43 insertions(+), 43 deletions(-) diff --git a/.github/actions/run-released-opensearch/action.yml b/.github/actions/run-released-opensearch/action.yml index 34f1227f6f..c8b8b45a99 100644 --- a/.github/actions/run-released-opensearch/action.yml +++ b/.github/actions/run-released-opensearch/action.yml @@ -7,6 +7,9 @@ inputs: secured: description: Whether to enable the security plugin required: true + opensearch_args: + description: Additional arguments to pass to the OpenSearch process + default: '' outputs: opensearch_url: description: The URL where the OpenSearch node is accessible @@ -49,4 +52,5 @@ runs: id: opensearch uses: ./client/.github/actions/start-opensearch with: + opensearch_args: ${{ inputs.opensearch_args }} secured: ${{ inputs.secured }} diff --git a/.github/actions/start-opensearch/action.yml b/.github/actions/start-opensearch/action.yml index 02dd2b613c..f2afd5783e 100644 --- a/.github/actions/start-opensearch/action.yml +++ b/.github/actions/start-opensearch/action.yml @@ -1,6 +1,9 @@ name: Start OpenSearch description: Configures and starts an OpenSearch daemon inputs: + opensearch_args: + description: Additional arguments to pass to the OpenSearch process + default: '' secured: description: Whether to enable the security plugin default: 'false' @@ -74,9 +77,9 @@ runs: fi if [[ "$RUNNER_OS" != "Windows" ]]; then - $OPENSEARCH_HOME/bin/opensearch & + $OPENSEARCH_HOME/bin/opensearch ${OPENSEARCH_ARGS} & else - $OPENSEARCH_HOME/bin/opensearch.bat -d & + $OPENSEARCH_HOME/bin/opensearch.bat ${OPENSEARCH_ARGS} -d & fi for attempt in {1..20}; do @@ -91,3 +94,4 @@ runs: env: SECURED: ${{ inputs.secured }} RUNNER_OS: ${{ runner.os }} + OPENSEARCH_ARGS: ${{ inputs.opensearch_args }} diff --git a/.github/workflows/integration-yaml-tests.yml b/.github/workflows/integration-yaml-tests.yml index 50654a0b80..034d2bdc7f 100644 --- a/.github/workflows/integration-yaml-tests.yml +++ b/.github/workflows/integration-yaml-tests.yml @@ -52,6 +52,7 @@ jobs: with: version: ${{ matrix.version }} secured: true + opensearch_args: ${{ matrix.version == '2.8.0' && '-Eopensearch.experimental.feature.search_pipeline.enabled=true' || '' }} - name: Run YAML tests working-directory: client diff --git a/src/ApiGenerator/Domain/Code/CsharpNames.cs b/src/ApiGenerator/Domain/Code/CsharpNames.cs index ea9b23d2ea..a100d0e54e 100644 --- a/src/ApiGenerator/Domain/Code/CsharpNames.cs +++ b/src/ApiGenerator/Domain/Code/CsharpNames.cs @@ -123,7 +123,7 @@ public string PerPathMethodName(string path) var method = MethodName; // This is temporary for transition period // TODO: remove in branch once it in opensearch is scrubbed - if (path.Contains("{type}") && !method.Contains("Type")) method += "UsingType"; + if (pc("{type}") && !method.Contains("Type")) method += "UsingType"; if (ms("Indices") && !pc("{index}")) return (method + "ForAll").Replace("AsyncForAll", "ForAllAsync"); @@ -131,6 +131,9 @@ public string PerPathMethodName(string path) if (ms("Nodes") && !pc("{node_id}")) return (method + "ForAll").Replace("AsyncForAll", "ForAllAsync"); + if (Namespace == "Knn" && method.StartsWith("Stats") && !pc("{node_id}")) + return method.Replace("Stats", "StatsForAll"); + return method; } diff --git a/src/ApiGenerator/Generator/ApiEndpointFactory.cs b/src/ApiGenerator/Generator/ApiEndpointFactory.cs index dc728188d0..22d6aaf551 100644 --- a/src/ApiGenerator/Generator/ApiEndpointFactory.cs +++ b/src/ApiGenerator/Generator/ApiEndpointFactory.cs @@ -238,14 +238,17 @@ private static string GetOpenSearchType(JsonSchema schema, Action if (first.EndsWith("?")) return first; if (first == second) return first; - switch (first, second) + return (first, second) switch { - case ("string", "list"): return second; - case ("boolean", "string"): return first; - case ("number", _): return "string"; - case (_,"number"): return "string"; - } + (_, "list") => second, + ("boolean", "string") => first, + ("number", _) => "string", + (_, "number") => "string", + (_, _) => throw new Exception($"Unable to determine type of: {first} and {second}") + }; } + + throw new Exception("Unable to determine type of oneOf"); } var enumOptions = schema.Enumeration.Where(e => e != null).Select(e => e.ToString()).ToList(); @@ -259,7 +262,19 @@ private static string GetOpenSearchType(JsonSchema schema, Action if (schema.Type == JsonObjectType.Array && (schema.Item?.HasReference ?? false)) _ = GetOpenSearchType(schema.Item, trackEnumToGenerate, true); - return schema.Type switch + var types = Enum.GetValues() + .Where(t => t != JsonObjectType.None && schema.Type.HasFlag(t)) + .ToHashSet(); + + var type = types.Count switch + { + 0 => throw new Exception("No type specified"), + 1 => types.First(), + 2 when types.Contains(JsonObjectType.Boolean) && types.Contains(JsonObjectType.String) => JsonObjectType.String, + _ => throw new Exception($"Unable to determine type of: {string.Join(", ", types)}") + }; + + return type switch { JsonObjectType.Integer => "number", JsonObjectType.Array => "list", diff --git a/src/ApiGenerator/Views/HighLevel/Requests/PlainRequestBase.cshtml b/src/ApiGenerator/Views/HighLevel/Requests/PlainRequestBase.cshtml index 8a0fbafbc6..a78cb750ab 100644 --- a/src/ApiGenerator/Views/HighLevel/Requests/PlainRequestBase.cshtml +++ b/src/ApiGenerator/Views/HighLevel/Requests/PlainRequestBase.cshtml @@ -1,21 +1,9 @@ @using ApiGenerator.Domain @inherits ApiGenerator.CodeTemplatePage @{ await IncludeLegacyGeneratorNotice(); } -// ReSharper disable RedundantUsingDirective -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Linq.Expressions; -using System.Runtime.Serialization; -using OpenSearch.Net; -using OpenSearch.Net.Utf8Json; -@{ await IncludeAsync("LowLevel/Client/Usings.cshtml", Model);} -// ReSharper disable UnusedTypeParameter -namespace OpenSearch.Client +namespace OpenSearch.Client; + +public abstract partial class @Raw("PlainRequestBase") { - public abstract partial class @Raw("PlainRequestBase") - { - } } diff --git a/src/OpenSearch.Client/_Generated/Requests.cs b/src/OpenSearch.Client/_Generated/Requests.cs index fe9dbf42d7..f15144a167 100644 --- a/src/OpenSearch.Client/_Generated/Requests.cs +++ b/src/OpenSearch.Client/_Generated/Requests.cs @@ -41,22 +41,7 @@ // Windows : build.bat codegen // // ----------------------------------------------- -// ReSharper disable RedundantUsingDirective -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Runtime.Serialization; -using System.Text; -using OpenSearch.Net; -using OpenSearch.Net.Specification.CatApi; -using OpenSearch.Net.Specification.ClusterApi; -using OpenSearch.Net.Specification.HttpApi; -using OpenSearch.Net.Specification.IndicesApi; -using OpenSearch.Net.Utf8Json; -// ReSharper disable UnusedTypeParameter -namespace OpenSearch.Client -{ - public abstract partial class PlainRequestBase { } -} +namespace OpenSearch.Client; + +public abstract partial class PlainRequestBase { }