Skip to content

Commit

Permalink
Better handle multi-type schemas
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
(cherry picked from commit 49f4b01)
  • Loading branch information
Xtansia committed Sep 24, 2024
1 parent 23de68d commit 7a68789
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .github/actions/run-released-opensearch/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,4 +52,5 @@ runs:
id: opensearch
uses: ./client/.github/actions/start-opensearch
with:
opensearch_args: ${{ inputs.opensearch_args }}
secured: ${{ inputs.secured }}
8 changes: 6 additions & 2 deletions .github/actions/start-opensearch/action.yml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -91,3 +94,4 @@ runs:
env:
SECURED: ${{ inputs.secured }}
RUNNER_OS: ${{ runner.os }}
OPENSEARCH_ARGS: ${{ inputs.opensearch_args }}
1 change: 1 addition & 0 deletions .github/workflows/integration-yaml-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/ApiGenerator/Domain/Code/CsharpNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,17 @@ 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");

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;
}

Expand Down
29 changes: 22 additions & 7 deletions src/ApiGenerator/Generator/ApiEndpointFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,17 @@ private static string GetOpenSearchType(JsonSchema schema, Action<string, bool>
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();
Expand All @@ -259,7 +262,19 @@ private static string GetOpenSearchType(JsonSchema schema, Action<string, bool>
if (schema.Type == JsonObjectType.Array && (schema.Item?.HasReference ?? false))
_ = GetOpenSearchType(schema.Item, trackEnumToGenerate, true);

return schema.Type switch
var types = Enum.GetValues<JsonObjectType>()
.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",
Expand Down
18 changes: 3 additions & 15 deletions src/ApiGenerator/Views/HighLevel/Requests/PlainRequestBase.cshtml
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
@using ApiGenerator.Domain
@inherits ApiGenerator.CodeTemplatePage<RestApiSpec>
@{ 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<TParameters>")
{
public abstract partial class @Raw("PlainRequestBase<TParameters>")
{
}
}
21 changes: 3 additions & 18 deletions src/OpenSearch.Client/_Generated/Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TParameters> { }
}
namespace OpenSearch.Client;

public abstract partial class PlainRequestBase<TParameters> { }

0 comments on commit 7a68789

Please sign in to comment.