Skip to content

Commit 16e6692

Browse files
committed
Handle deprecated boolean property on params
(cherry picked from commit e1e1e33)
1 parent f631757 commit 16e6692

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/ApiGenerator/Domain/Specification/QueryParameters.cs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using ApiGenerator.Generator;
9+
using Newtonsoft.Json;
10+
using Newtonsoft.Json.Linq;
911

1012
namespace ApiGenerator.Domain.Specification
1113
{
@@ -74,21 +76,23 @@ public string Obsolete
7476
get
7577
{
7678
if (!string.IsNullOrEmpty(_obsolete)) return _obsolete;
79+
if (Deprecated != null)
80+
{
81+
if (!string.IsNullOrEmpty(Deprecated.Version) && !string.IsNullOrEmpty(Deprecated.Description))
82+
return $"Deprecated as of: {Deprecated.Version}, reason: {Deprecated.Description}";
83+
if (!string.IsNullOrEmpty(Deprecated.Version))
84+
return $"Deprecated as of: {Deprecated.Version}";
85+
if (!string.IsNullOrEmpty(Deprecated.Description))
86+
return $"reason: {Deprecated.Description}";
87+
88+
return "deprecated";
89+
}
7790

78-
return Deprecated != null
79-
? $"Deprecated as of: {Deprecated.Version}, reason: {Deprecated.Description}"
80-
: null;
91+
return null;
8192
}
8293
set => _obsolete = value;
8394
}
8495

85-
public class QueryParameterDeprecation
86-
{
87-
public string Version { get; set; }
88-
89-
public string Description { get; set; }
90-
}
91-
9296
public QueryParameterDeprecation Deprecated { get; set; }
9397

9498
public IEnumerable<string> Options { get; set; }
@@ -168,4 +172,27 @@ public string TypeLowLevel
168172
public string InitializerGenerator(string @namespace, string type, string name, string key, string setter, params string[] doc) =>
169173
CodeGenerator.Property(@namespace, type, name, key, setter, Obsolete, doc);
170174
}
175+
176+
public class QueryParameterDeprecation
177+
{
178+
public string Version { get; set; }
179+
180+
public string Description { get; set; }
181+
}
182+
183+
internal class QueryParameterDeprecationConverter : JsonConverter
184+
{
185+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException();
186+
187+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
188+
{
189+
if (reader.TokenType == JsonToken.Boolean)
190+
return new QueryParameterDeprecation();
191+
192+
var jObject = JObject.Load(reader);
193+
return jObject.ToObject<QueryParameterDeprecation>(JsonSerializer.CreateDefault());
194+
}
195+
196+
public override bool CanConvert(Type objectType) => typeof(QueryParameterDeprecation).IsAssignableFrom(objectType);
197+
}
171198
}

src/ApiGenerator/Generator/ApiEndpointFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@
1111
using ApiGenerator.Domain;
1212
using ApiGenerator.Domain.Code;
1313
using ApiGenerator.Domain.Specification;
14+
using Newtonsoft.Json;
1415
using Newtonsoft.Json.Linq;
1516

1617
namespace ApiGenerator.Generator
1718
{
1819
public static class ApiEndpointFactory
1920
{
21+
private static readonly JsonSerializer Serializer = JsonSerializer.Create(
22+
new JsonSerializerSettings { Converters = new List<JsonConverter> { new QueryParameterDeprecationConverter() } });
23+
2024
public static ApiEndpoint FromFile(string jsonFile)
2125
{
2226
var officialJsonSpec = JObject.Parse(File.ReadAllText(jsonFile));
2327
TransformNewSpecStructureToOld(officialJsonSpec);
2428
PatchOfficialSpec(officialJsonSpec, jsonFile);
25-
var (name, endpoint) = officialJsonSpec.ToObject<Dictionary<string, ApiEndpoint>>().First();
29+
var (name, endpoint) = officialJsonSpec.ToObject<Dictionary<string, ApiEndpoint>>(Serializer).First();
2630

2731
endpoint.FileName = Path.GetFileName(jsonFile);
2832
endpoint.Name = name;

0 commit comments

Comments
 (0)