Skip to content

Commit 343d88a

Browse files
Fix high-level code gen for deprecated URLs (#5841) (#5843)
* Include deprecated URLs for ctor generation * Remove some outdated patches * Exclude type ctors params not included in 7.0 Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent 72ba0fc commit 343d88a

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

src/ApiGenerator/Domain/Code/HighLevel/Requests/Constructor.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public class Constructor
1414
{
1515
private static readonly string Indent = $"{Environment.NewLine}\t\t";
1616
public string AdditionalCode { get; set; } = string.Empty;
17-
public bool Parameterless { get; set; }
1817
public string Body { get; set; }
1918
public string Description { get; set; }
2019
public string Generated { get; set; }
20+
public bool Parameterless { get; set; }
2121

2222
public static IEnumerable<Constructor> DescriptorConstructors(CsharpNames names, UrlInformation url)
2323
{
@@ -47,7 +47,10 @@ string generic
4747
{
4848
var ctors = new List<Constructor>();
4949

50-
var paths = url.Paths.ToList();
50+
// Include deprecated paths to ensure these are not removed (a breaking change) during 7.x releases.
51+
// For historical reasons, constructors for deprecated paths which specified a type where removed in 7.0 and
52+
// therefore we don't include those in generation to avoid them being re-added.
53+
var paths = url.Paths.ToList().Union(url.PathsWithDeprecations.Where(x => x.Parts.All(p => p.Name != "type")));
5154

5255
if (url.IsPartless) return ctors;
5356

@@ -77,7 +80,7 @@ string generic
7780
{
7881
Parameterless = string.IsNullOrEmpty(constructorArgs),
7982
Generated = generated,
80-
Description = path.GetXmlDocs(Indent, skipResolvable: true),
83+
Description = path.GetXmlDocs(Indent, true),
8184
Body = string.Empty
8285
});
8386

@@ -89,15 +92,14 @@ string generic
8992
{
9093
Parameterless = string.IsNullOrEmpty(docPathConstArgs),
9194
Generated = $"public {typeName}({docPathConstArgs}) : this({docPathBaseArgs})",
92-
9395
AdditionalCode = $"partial void DocumentFromPath({generic} document);",
94-
Description = docPath.GetXmlDocs(Indent, skipResolvable: true, documentConstructor: true),
96+
Description = docPath.GetXmlDocs(Indent, true, true),
9597
Body = "=> DocumentFromPath(documentWithId);"
9698
});
9799
}
98100
}
99101
var constructors = ctors.GroupBy(c => c.Generated.Split(new[] { ':' }, 2)[0]).Select(g => g.Last()).ToList();
100-
if (!constructors.Any(c=>c.Parameterless))
102+
if (!constructors.Any(c => c.Parameterless))
101103
constructors.Add(new Constructor
102104
{
103105
Parameterless = true,

src/ApiGenerator/Domain/Specification/UrlInformation.cs

+23-25
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,32 @@
99

1010
namespace ApiGenerator.Domain.Specification
1111
{
12-
1312
// ReSharper disable once ClassNeverInstantiated.Global
1413
public class UrlInformation
1514
{
16-
public IDictionary<string, QueryParameters> Params { get; set; } = new SortedDictionary<string, QueryParameters>();
15+
private static readonly string[] DocumentApiParts = { "index", "id" };
1716

18-
[JsonProperty("paths")]
19-
private IReadOnlyCollection<string> OriginalPaths { get; set; }
17+
private List<UrlPath> _paths;
18+
19+
private List<UrlPath> _pathsWithDeprecation;
20+
21+
public bool IsDocumentApi => IsADocumentRoute(Parts);
22+
23+
public bool IsPartless => !Parts.Any();
2024

2125
[JsonProperty("parts")]
2226
public IDictionary<string, UrlPart> OriginalParts { get; set; }
2327

24-
[JsonProperty("deprecated_paths")]
25-
private IReadOnlyCollection<DeprecatedPath> DeprecatedPaths { get; set; }
28+
public IDictionary<string, QueryParameters> Params { get; set; } = new SortedDictionary<string, QueryParameters>();
29+
30+
// Include deprecated paths to ensure these are not removed (a breaking change) during 7.x releases.
31+
// For historical reasons, constructors for deprecated paths which specified a type where removed in 7.0 and
32+
// therefore we don't include those in generation to avoid them being re-added.
33+
public IReadOnlyCollection<UrlPart> Parts => Paths.Union(PathsWithDeprecations.Where(x => x.Parts.All(p => p.Name != "type")))
34+
.SelectMany(p => p.Parts)
35+
.DistinctBy(p => p.Name)
36+
.ToList();
2637

27-
private List<UrlPath> _paths;
2838
public IReadOnlyCollection<UrlPath> Paths
2939
{
3040
get
@@ -36,14 +46,13 @@ public IReadOnlyCollection<UrlPath> Paths
3646
}
3747
}
3848

39-
private List<UrlPath> _pathsWithDeprecation;
4049
public IReadOnlyCollection<UrlPath> PathsWithDeprecations
4150
{
4251
get
4352
{
4453
if (_pathsWithDeprecation != null && _pathsWithDeprecation.Count > 0) return _pathsWithDeprecation;
4554

46-
var paths = Paths ?? new UrlPath[] {};
55+
var paths = Paths ?? new UrlPath[] { };
4756
if (DeprecatedPaths == null || DeprecatedPaths.Count == 0) return Paths;
4857
if (OriginalParts == null) return Paths;
4958

@@ -58,8 +67,7 @@ public IReadOnlyCollection<UrlPath> PathsWithDeprecations
5867
var withoutDeprecatedAliases = DeprecatedPaths
5968
.Select(deprecatedPath => new
6069
{
61-
deprecatedPath,
62-
parts = new HashSet<string>(OriginalParts.Keys.Where(k => deprecatedPath.Path.Contains($"{{{k}}}")))
70+
deprecatedPath, parts = new HashSet<string>(OriginalParts.Keys.Where(k => deprecatedPath.Path.Contains($"{{{k}}}")))
6371
})
6472
.GroupBy(t => t.parts, HashSet<string>.CreateSetComparer())
6573
.Where(grouped => !canonicalPartNameLookup.Any(set => set.SetEquals(grouped.Key)))
@@ -74,32 +82,23 @@ public IReadOnlyCollection<UrlPath> PathsWithDeprecations
7482
var finalPathsWithDeprecations = new List<UrlPath>(_pathsWithDeprecation.Count);
7583

7684
foreach (var path in _pathsWithDeprecation)
77-
{
7885
if (path.Deprecation is null &&
7986
DeprecatedPaths.SingleOrDefault(p => p.Path.Equals(path.Path, StringComparison.OrdinalIgnoreCase)) is { } match)
80-
{
8187
finalPathsWithDeprecations.Add(new UrlPath(match, OriginalParts, Paths));
82-
}
8388
else
84-
{
8589
finalPathsWithDeprecations.Add(path);
86-
}
87-
}
8890

8991
_pathsWithDeprecation = finalPathsWithDeprecations;
9092

9193
return _pathsWithDeprecation;
9294
}
9395
}
9496

97+
[JsonProperty("deprecated_paths")]
98+
private IReadOnlyCollection<DeprecatedPath> DeprecatedPaths { get; set; }
9599

96-
public IReadOnlyCollection<UrlPart> Parts => Paths.SelectMany(p => p.Parts).DistinctBy(p => p.Name).ToList();
97-
98-
public bool IsPartless => !Parts.Any();
99-
100-
private static readonly string[] DocumentApiParts = { "index", "id" };
101-
102-
public bool IsDocumentApi => IsADocumentRoute(Parts);
100+
[JsonProperty("paths")]
101+
private IReadOnlyCollection<string> OriginalPaths { get; set; }
103102

104103
public static bool IsADocumentRoute(IReadOnlyCollection<UrlPart> parts) =>
105104
parts.Count() == DocumentApiParts.Length
@@ -115,6 +114,5 @@ public bool TryGetDocumentApiPath(out UrlPath path)
115114
path = new UrlPath(mostVerbosePath.Path, OriginalParts, mostVerbosePath.Parts);
116115
return true;
117116
}
118-
119117
}
120118
}

src/ApiGenerator/Domain/Specification/UrlPart.cs

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public string HighLevelTypeName
8989
case "id":
9090
return "Id";
9191

92+
case "scroll_id":
9293
case "policy_id":
9394
return Type == "string" ? "Id" : "Ids";
9495

0 commit comments

Comments
 (0)