Skip to content

Commit 2585b20

Browse files
authored
Fix/7.x/interface searchresponse (#3888)
* Update codegen to emit interface for search response and await when doing so * update code * Update test to use interface not concrete SearchResponse
1 parent a65e737 commit 2585b20

File tree

89 files changed

+262
-238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+262
-238
lines changed

src/CodeGeneration/ApiGenerator/Domain/Code/CsharpNames.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public string ResponseName
6161
else if (ApiName.EndsWith("Exists")) return $"ExistsResponse";
6262

6363
var generatedName = $"{ApiName}Response";
64-
return CodeConfiguration.ResponseLookup.TryGetValue(generatedName, out var lookup) ? lookup.Item1 : generatedName;
64+
var name = CodeConfiguration.ResponseLookup.TryGetValue(generatedName, out var lookup) ? lookup.Item1 : generatedName;
65+
return name;
6566
}
6667
}
6768
public string RequestInterfaceName => $"I{RequestName}";
@@ -159,7 +160,16 @@ public static List<string> SplitGeneric(string generic) => (generic ?? string.Em
159160

160161
public string GenericOrNonGenericDescriptorName => GenericDescriptorName ?? DescriptorName;
161162
public string GenericOrNonGenericInterfaceName => GenericInterfaceName ?? RequestInterfaceName;
162-
public string GenericOrNonGenericResponseName => GenericResponseName ?? ResponseName;
163+
public string GenericOrNonGenericResponseName
164+
{
165+
get
166+
{
167+
var full = GenericResponseName ?? ResponseName;
168+
if (full.StartsWith("SearchResponse<"))
169+
full = "I" + full;
170+
return full;
171+
}
172+
}
163173

164174
/// <summary> If matching Request.cs only defined generic interface make the client method only accept said interface </summary>
165175
public string GenericOrNonGenericInterfacePreference => CodeConfiguration.GenericOnlyInterfaces.Contains(RequestInterfaceName)

src/CodeGeneration/ApiGenerator/Domain/Code/HighLevel/Methods/InitializerMethod.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ public InitializerMethod(CsharpNames names, string link, string summary) : base(
2626

2727
public string DispatchMethod => IsCatMethod ? "DoCat" : "DoRequest";
2828

29+
/// <summary>
30+
/// Dispatch needs a class instance so if the response is an interface transform it to the concrete implementation
31+
/// when calling into DoRequest
32+
/// </summary>
33+
private string DispatchResponseName => InterfaceResponse ? ResponseName.Substring(1, ResponseName.Length - 1) : ResponseName;
34+
2935
public string DispatchGenerics => IsCatMethod
3036
? $"<{ArgumentType},{CsharpNames.ParametersName},{CsharpNames.RequestName.Replace("Request", "Record")}>"
31-
: $"<{ArgumentType},{ResponseName}>";
37+
: $"<{ArgumentType},{DispatchResponseName}>";
3238

3339
public string DispatchParameters => IsCatMethod
3440
? "request"
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
namespace ApiGenerator.Domain.Code.HighLevel.Methods
1+
namespace ApiGenerator.Domain.Code.HighLevel.Methods
22
{
33
public abstract class MethodSyntaxBase
44
{
5-
protected MethodSyntaxBase(CsharpNames names, string link, string summary) =>
5+
protected MethodSyntaxBase(CsharpNames names, string link, string summary) =>
66
(CsharpNames, DocumentationLink, XmlDocSummary) = (names, link, summary);
77

88
public string DocumentationLink { get; }
9-
9+
1010
public string XmlDocSummary { get; }
1111

1212
protected CsharpNames CsharpNames { get; }
13-
13+
14+
public bool InterfaceResponse => ResponseName.StartsWith("ISearchResponse<");
15+
1416
public string ResponseName => CsharpNames.GenericOrNonGenericResponseName;
1517

1618
public string DocumentationCref => CsharpNames.GenericOrNonGenericInterfacePreference;
17-
19+
1820
public abstract string MethodGenerics { get; }
19-
21+
2022
public abstract string GenericWhereClause { get; }
2123
}
22-
}
24+
}

src/CodeGeneration/ApiGenerator/Views/HighLevel/Client/FluentSyntax/FluentMethod.cshtml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
FluentSyntaxBase syntax = Model.Syntax;
1111

1212
var method = !Model.Async ? syntax.MethodName : string.Format("{0}Async", syntax.MethodName);
13+
var asyncKeyword = Model.Syntax.InterfaceResponse && Model.Async ? "async " : String.Empty;
14+
var awaitKeyWord = Model.Syntax.InterfaceResponse && Model.Async ? "await ": string.Empty;
15+
var configureAwait = Model.Syntax.InterfaceResponse && Model.Async ? ".ConfigureAwait(false)" : String.Empty;
1316

1417
var requestMethodGenerics = syntax.RequestMethodGenerics;
1518
var descriptor = syntax.DescriptorName;
@@ -18,5 +21,5 @@
1821
var cancellationToken = !Model.Async ? string.Empty : ", ct";
1922
}
2023
@{ await IncludeAsync("HighLevel/Client/MethodXmlDocs.cshtml", syntax); }
21-
public @{ await IncludeAsync("HighLevel/Client/FluentSyntax/FluentMethodHeader.cshtml", Model);} @Raw("=>")
22-
@(method)@(Raw(requestMethodGenerics))(selector.InvokeOrDefault(new @(Raw(descriptor))(@Raw(selectorArgs))@(@selectorChained))@cancellationToken);
24+
public @(asyncKeyword)@{ await IncludeAsync("HighLevel/Client/FluentSyntax/FluentMethodHeader.cshtml", Model);} @Raw("=>")
25+
@(awaitKeyWord)@(method)@(Raw(requestMethodGenerics))(selector.InvokeOrDefault(new @(Raw(descriptor))(@Raw(selectorArgs))@(@selectorChained))@cancellationToken)@Raw(configureAwait);

src/CodeGeneration/ApiGenerator/Views/HighLevel/Client/InitializerSyntax/InitializerMethod.cshtml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
var dispatchMethod = !Model.Async ? syntax.DispatchMethod : string.Format("{0}Async", syntax.DispatchMethod);
1313
var dispatchGenerics = syntax.DispatchGenerics;
1414
var dispatchParameters = syntax.DispatchParameters;
15+
var asyncKeyword = Model.Syntax.InterfaceResponse && Model.Async ? "async " : String.Empty;
16+
var awaitKeyWord = Model.Syntax.InterfaceResponse && Model.Async ? "await ": string.Empty;
17+
var configureAwait = Model.Syntax.InterfaceResponse && Model.Async ? ".ConfigureAwait(false)" : String.Empty;
1518
if (Model.Async)
1619
{
1720
dispatchParameters += ", ct";
1821
}
1922
}
2023
@{ await IncludeAsync("HighLevel/Client/MethodXmlDocs.cshtml", syntax); }
21-
public @{ await IncludeAsync("HighLevel/Client/InitializerSyntax/InitializerMethodHeader.cshtml", Model); } @Raw("=>")
22-
@(dispatchMethod)@(Raw(dispatchGenerics))(@Raw(dispatchParameters));
24+
public @Raw(asyncKeyword)@{ await IncludeAsync("HighLevel/Client/InitializerSyntax/InitializerMethodHeader.cshtml", Model); } @Raw("=>")
25+
@(awaitKeyWord)@(dispatchMethod)@(Raw(dispatchGenerics))(@Raw(dispatchParameters))@Raw(configureAwait);
2326

2427

src/Nest/Descriptors.Graph.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ public GraphExploreDescriptor<TDocument> Index<TOther>()
5555
///<summary>A shortcut into calling Index(Indices.All)</summary>
5656
public GraphExploreDescriptor<TDocument> AllIndices() => Index(Indices.All);
5757
// Request parameters
58-
///<summary>
58+
///<summary>
5959
/// A document is routed to a particular shard in an index using the following formula
6060
/// <para> shard_num = hash(_routing) % num_primary_shards</para>
6161
/// <para>Elasticsearch will use the document id if not provided. </para>
6262
/// <para>For requests that are constructed from/for a document NEST will automatically infer the routing key
63-
/// if that document has a <see cref = "Nest.JoinField"/> or a routing mapping on for its type exists on <see cref = "Nest.ConnectionSettings"/></para>
64-
///</summary>
63+
/// if that document has a <see cref = "Nest.JoinField"/> or a routing mapping on for its type exists on <see cref = "Nest.ConnectionSettings"/></para>
64+
///</summary>
6565
public GraphExploreDescriptor<TDocument> Routing(Routing routing) => Qs("routing", routing);
6666
///<summary>Explicit operation timeout</summary>
6767
public GraphExploreDescriptor<TDocument> Timeout(Time timeout) => Qs("timeout", timeout);

0 commit comments

Comments
 (0)