Skip to content

Commit d910fdb

Browse files
committed
Implement formatter for GetCertificates response
This commit implements a formatter for the GetCertificatesResponse. The response JSON is an array of the format [ { "path" : "node01.crt", "format" : "PEM", "alias" : null, "subject_dn" : "CN=node01", "serial_number" : "<serial>", "has_private_key" : true, "expiry" : "2022-04-11T02:40:31.000Z" }, { "path" : "ca.crt", "format" : "PEM", "alias" : null, "subject_dn" : "CN=Elastic Certificate Tool Autogenerated CA", "serial_number" : "<serial>", "has_private_key" : false, "expiry" : "2022-04-11T02:40:31.000Z" } ] In the event of an error, the response could be an object, so the formatter should handle this.
1 parent 84e0852 commit d910fdb

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

src/Nest/CommonAbstractions/Response/DictionaryResponseBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public abstract class DictionaryResponseBase<TKey, TValue> : ResponseBase, IDict
2020
EmptyReadOnly<TKey, TValue>.Dictionary;
2121
}
2222

23-
internal class DictionaryResponseFormatterHelpers
23+
internal class ResponseFormatterHelpers
2424
{
2525
internal static readonly AutomataDictionary ServerErrorFields = new AutomataDictionary
2626
{
@@ -43,7 +43,7 @@ public TResponse Deserialize(ref JsonReader reader, IJsonFormatterResolver forma
4343
while (reader.ReadIsInObject(ref count))
4444
{
4545
var property = reader.ReadPropertyNameSegmentRaw();
46-
if (DictionaryResponseFormatterHelpers.ServerErrorFields.TryGetValue(property, out var errorValue))
46+
if (ResponseFormatterHelpers.ServerErrorFields.TryGetValue(property, out var errorValue))
4747
{
4848
switch (errorValue)
4949
{

src/Nest/CommonAbstractions/Response/ResolvableDictionaryProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public TResponse Deserialize(ref JsonReader reader, IJsonFormatterResolver forma
8989
while (reader.ReadIsInObject(ref count))
9090
{
9191
var property = reader.ReadPropertyNameSegmentRaw();
92-
if (DictionaryResponseFormatterHelpers.ServerErrorFields.TryGetValue(property, out var errorValue))
92+
if (ResponseFormatterHelpers.ServerErrorFields.TryGetValue(property, out var errorValue))
9393
{
9494
switch (errorValue)
9595
{

src/Nest/Modules/SnapshotAndRestore/Repositories/GetRepository/GetRepositoryResponseFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public GetRepositoryResponse Deserialize(ref JsonReader reader, IJsonFormatterRe
1515
while (reader.ReadIsInObject(ref count))
1616
{
1717
var property = reader.ReadPropertyNameSegmentRaw();
18-
if (DictionaryResponseFormatterHelpers.ServerErrorFields.TryGetValue(property, out var errorValue))
18+
if (ResponseFormatterHelpers.ServerErrorFields.TryGetValue(property, out var errorValue))
1919
{
2020
switch (errorValue)
2121
{

src/Nest/XPack/Ssl/GetCertificates/GetCertificatesResponse.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Runtime.Serialization;
4+
using Elasticsearch.Net;
45

56
namespace Nest
67
{
8+
[JsonFormatter(typeof(GetCertificatesResponseFormatter))]
79
public class GetCertificatesResponse : ResponseBase
810
{
11+
[IgnoreDataMember]
912
public IReadOnlyCollection<ClusterCertificateInformation> Certificates { get; internal set; } =
1013
EmptyReadOnly<ClusterCertificateInformation>.Collection;
1114
}
@@ -34,4 +37,60 @@ public class ClusterCertificateInformation
3437
[DataMember(Name = "expiry")]
3538
public DateTimeOffset Expiry { get; internal set; }
3639
}
40+
41+
internal class GetCertificatesResponseFormatter : IJsonFormatter<GetCertificatesResponse>
42+
{
43+
private static readonly ReadOnlyCollectionFormatter<ClusterCertificateInformation> Formatter =
44+
new ReadOnlyCollectionFormatter<ClusterCertificateInformation>();
45+
46+
public void Serialize(ref JsonWriter writer, GetCertificatesResponse value, IJsonFormatterResolver formatterResolver) =>
47+
throw new NotImplementedException();
48+
49+
public GetCertificatesResponse Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
50+
{
51+
var response = new GetCertificatesResponse();
52+
53+
if (reader.ReadIsNull())
54+
return response;
55+
56+
switch (reader.GetCurrentJsonToken())
57+
{
58+
case JsonToken.BeginArray:
59+
response.Certificates = Formatter.Deserialize(ref reader, formatterResolver);
60+
break;
61+
case JsonToken.BeginObject:
62+
var count = 0;
63+
while (reader.ReadIsInObject(ref count))
64+
{
65+
var property = reader.ReadPropertyNameSegmentRaw();
66+
if (ResponseFormatterHelpers.ServerErrorFields.TryGetValue(property, out var errorValue))
67+
{
68+
switch (errorValue)
69+
{
70+
case 0:
71+
if (reader.GetCurrentJsonToken() == JsonToken.String)
72+
response.Error = new Error { Reason = reader.ReadString() };
73+
else
74+
{
75+
var formatter = formatterResolver.GetFormatter<Error>();
76+
response.Error = formatter.Deserialize(ref reader, formatterResolver);
77+
}
78+
break;
79+
case 1:
80+
if (reader.GetCurrentJsonToken() == JsonToken.Number)
81+
response.StatusCode = reader.ReadInt32();
82+
else
83+
reader.ReadNextBlock();
84+
break;
85+
}
86+
}
87+
else
88+
reader.ReadNextBlock();
89+
}
90+
break;
91+
}
92+
93+
return response;
94+
}
95+
}
3796
}

0 commit comments

Comments
 (0)