Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 1.x] Re-generate the root namespace (#804) #812

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ namespace ApiGenerator.Configuration.Overrides.Endpoints
{
public class DeleteByQueryOverrides : EndpointOverridesBase
{
public override IEnumerable<string> SkipQueryStringParams => new[] { "max_docs", };
public override IEnumerable<string> SkipQueryStringParams => ["max_docs", "slices"];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ namespace ApiGenerator.Configuration.Overrides.Endpoints
{
public class ReindexOnServerOverrides : EndpointOverridesBase
{
public override IEnumerable<string> SkipQueryStringParams => new[] { "max_docs", };
public override IEnumerable<string> SkipQueryStringParams => ["max_docs", "slices"];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace ApiGenerator.Configuration.Overrides.Endpoints
// ReSharper disable once UnusedMember.Global
public class SearchOverrides : EndpointOverridesBase
{
public override IEnumerable<string> SkipQueryStringParams => new[]
{
public override IEnumerable<string> SkipQueryStringParams =>
[
"size",
"from",
"timeout",
Expand All @@ -45,12 +45,8 @@ public class SearchOverrides : EndpointOverridesBase
"_source_includes",
"_source_excludes",
"track_scores",
"terminate_after",
};

public override IEnumerable<string> RenderPartial => new[]
{
"track_total_hits"
};
"track_total_hits",
"terminate_after"
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ namespace ApiGenerator.Configuration.Overrides.Endpoints
{
public class UpdateByQueryOverrides : EndpointOverridesBase
{
public override IEnumerable<string> SkipQueryStringParams => new[] { "max_docs", };
public override IEnumerable<string> SkipQueryStringParams => ["max_docs", "slices"];
}
}
2 changes: 1 addition & 1 deletion src/ApiGenerator/Domain/Code/HttpMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HttpMethod
public static readonly HttpMethod Patch = new("Patch", true);
public static readonly HttpMethod Post = new("Post", true);
public static readonly HttpMethod Put = new("Put", true);
public static readonly HttpMethod[] All = { Delete, Get, Head, Patch, Post, Put };
public static readonly HttpMethod[] All = [Delete, Get, Head, Patch, Post, Put];

private readonly string _method;

Expand Down
210 changes: 119 additions & 91 deletions src/ApiGenerator/Domain/Specification/ApiEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace ApiGenerator.Domain.Specification
{
public class ApiEndpoint
{
/// <summary> The original name as declared in the spec </summary>
/// <summary> The original name as declared in the spec </summary>
public string Name { get; set; }

/// <summary> The original namespace as declared in the spec </summary>
Expand All @@ -63,110 +63,130 @@ public class ApiEndpoint

public IEndpointOverrides Overrides { get; internal set; }

private IEnumerable<QueryParameters> ParamsToGenerate => Url.Params.Values.Where(p => !p.Skip).OrderBy(p => p.ClsName);

public RequestInterface RequestInterface => new()
{
CsharpNames = CsharpNames,
UrlParts = Url.Parts,
PartialParameters =
Body == null ? Enumerable.Empty<QueryParameters>().ToList() : ParamsToGenerate.Where(p => p.RenderPartial).ToList(),
OfficialDocumentationLink = OfficialDocumentationLink?.Url
};

public RequestPartialImplementation RequestPartialImplementation => new()
{
CsharpNames = CsharpNames,
OfficialDocumentationLink = OfficialDocumentationLink?.Url,
Stability = Stability,
Paths = Url.Paths.ToList(),
Parts = Url.Parts,
Params = ParamsToGenerate.ToList(),
Constructors = Constructor.RequestConstructors(CsharpNames, Url, inheritsFromPlainRequestBase: true).ToList(),
GenericConstructors = Constructor.RequestConstructors(CsharpNames, Url, inheritsFromPlainRequestBase: false).ToList(),
HasBody = Body != null,
};

public DescriptorPartialImplementation DescriptorPartialImplementation => new()
{
CsharpNames = CsharpNames,
OfficialDocumentationLink = OfficialDocumentationLink?.Url,
Constructors = Constructor.DescriptorConstructors(CsharpNames, Url).ToList(),
Paths = Url.Paths.ToList(),
Parts = Url.Parts,
Params = ParamsToGenerate.ToList(),
HasBody = Body != null,
};

public RequestParameterImplementation RequestParameterImplementation => new()
{
CsharpNames = CsharpNames,
OfficialDocumentationLink = OfficialDocumentationLink?.Url,
Params = ParamsToGenerate.ToList(),
HttpMethod = PreferredHttpMethod
};
private IEnumerable<QueryParameters> ParamsToGenerate =>
Url.Params.Values.Where(p => !p.Skip).OrderBy(p => p.ClsName);

public string PreferredHttpMethod =>
HttpMethods.OrderByDescending(m => m switch
public RequestInterface RequestInterface =>
new()
{
CsharpNames = CsharpNames,
UrlParts = Url.Parts,
PartialParameters =
Body == null
? Enumerable.Empty<QueryParameters>().ToList()
: ParamsToGenerate.Where(p => p.RenderPartial).ToList(),
OfficialDocumentationLink = OfficialDocumentationLink?.Url,
};

public RequestPartialImplementation RequestPartialImplementation =>
new()
{
CsharpNames = CsharpNames,
OfficialDocumentationLink = OfficialDocumentationLink?.Url,
Stability = Stability,
Paths = Url.Paths.ToList(),
Parts = Url.Parts,
Params = ParamsToGenerate.ToList(),
Constructors = Constructor
.RequestConstructors(CsharpNames, Url, inheritsFromPlainRequestBase: true)
.ToList(),
GenericConstructors = Constructor
.RequestConstructors(CsharpNames, Url, inheritsFromPlainRequestBase: false)
.ToList(),
HasBody = Body != null,
};

public DescriptorPartialImplementation DescriptorPartialImplementation =>
new()
{
CsharpNames = CsharpNames,
OfficialDocumentationLink = OfficialDocumentationLink?.Url,
Constructors = Constructor.DescriptorConstructors(CsharpNames, Url).ToList(),
Paths = Url.Paths.ToList(),
Parts = Url.Parts,
Params = ParamsToGenerate.ToList(),
HasBody = Body != null,
};

public RequestParameterImplementation RequestParameterImplementation =>
new()
{
"GET" => 0,
"POST" => 1,
"PUT" or "DELETE" or "PATCH" or "HEAD" => 2, // Prefer "resource" methods over GET/POST methods
_ => -1
}).First();
CsharpNames = CsharpNames,
OfficialDocumentationLink = OfficialDocumentationLink?.Url,
Params = ParamsToGenerate.ToList(),
HttpMethod = PreferredHttpMethod,
};

public string PreferredHttpMethod =>
Name == "bulk"
? "POST"
: HttpMethods
.OrderByDescending(m =>
m switch
{
"GET" => 0,
"POST" => 1,
"PUT" or "DELETE" or "PATCH" or "HEAD" => 2, // Prefer "resource" methods over GET/POST methods
_ => -1,
}
)
.First();

public string HighLevelMethodXmlDocDescription =>
$"<c>{PreferredHttpMethod}</c> request to the <c>{Name}</c> API, read more about this API online:";

private bool BodyIsOptional => Body is not { Required: true } || HttpMethods.Contains("GET");

private Deprecation Deprecated =>
!Url.Paths.Any() && Url.AllPaths.Count > 0
? Url.DeprecatedPaths
.Select(p => p.Deprecation)
.MaxBy(d => d.Version)
: null;

private Version VersionAdded =>
Url.AllPaths
.Select(p => p.VersionAdded)
.Where(v => v != null)
.Min();

public HighLevelModel HighLevelModel => new()
{
CsharpNames = CsharpNames,
Fluent = new FluentMethod(CsharpNames, Url.Parts,
selectorIsOptional: BodyIsOptional,
link: OfficialDocumentationLink?.Url,
summary: HighLevelMethodXmlDocDescription,
deprecated: Deprecated,
versionAdded: VersionAdded
),
FluentBound = !CsharpNames.DescriptorBindsOverMultipleDocuments
? null
: new BoundFluentMethod(CsharpNames, Url.Parts,
private bool BodyIsOptional =>
Body is not { Required: true } || HttpMethods.Contains("GET");

private Deprecation Deprecated =>
!Url.Paths.Any() && Url.AllPaths.Count > 0
? Url.DeprecatedPaths.Select(p => p.Deprecation).MaxBy(d => d.Version)
: null;

private Version VersionAdded =>
Url.AllPaths.Select(p => p.VersionAdded).Where(v => v != null).Min();

public HighLevelModel HighLevelModel =>
new()
{
CsharpNames = CsharpNames,
Fluent = new FluentMethod(
CsharpNames,
Url.Parts,
selectorIsOptional: BodyIsOptional,
link: OfficialDocumentationLink?.Url,
summary: HighLevelMethodXmlDocDescription,
deprecated: Deprecated,
versionAdded: VersionAdded
deprecated: Deprecated,
versionAdded: VersionAdded
),
FluentBound = !CsharpNames.DescriptorBindsOverMultipleDocuments
? null
: new BoundFluentMethod(
CsharpNames,
Url.Parts,
selectorIsOptional: BodyIsOptional,
link: OfficialDocumentationLink?.Url,
summary: HighLevelMethodXmlDocDescription,
deprecated: Deprecated,
versionAdded: VersionAdded
),
Initializer = new InitializerMethod(
CsharpNames,
link: OfficialDocumentationLink?.Url,
summary: HighLevelMethodXmlDocDescription,
deprecated: Deprecated,
versionAdded: VersionAdded
),
Initializer = new InitializerMethod(CsharpNames,
link: OfficialDocumentationLink?.Url,
summary: HighLevelMethodXmlDocDescription,
deprecated: Deprecated,
versionAdded: VersionAdded
)
};
};

private List<LowLevelClientMethod> _lowLevelClientMethods;

public IReadOnlyCollection<LowLevelClientMethod> LowLevelClientMethods
{
get
{
if (_lowLevelClientMethods != null && _lowLevelClientMethods.Count > 0) return _lowLevelClientMethods;
if (_lowLevelClientMethods != null && _lowLevelClientMethods.Count > 0)
return _lowLevelClientMethods;

// enumerate once and cache
_lowLevelClientMethods = new List<LowLevelClientMethod>();
Expand All @@ -183,11 +203,19 @@ public IReadOnlyCollection<LowLevelClientMethod> LowLevelClientMethods
// TODO This is hack until we stop transforming the new spec format into the old
if (Name == "index" && !mapsApiArgumentHints.Contains("id"))
httpMethod = "POST";
else if (Name == "index") httpMethod = PreferredHttpMethod;
else if (Name == "index")
httpMethod = PreferredHttpMethod;

if (Body != null)
{
parts.Add(new UrlPart { Name = "body", Type = "PostData", Description = Body.Description });
parts.Add(
new UrlPart
{
Name = "body",
Type = "PostData",
Description = Body.Description,
}
);
mapsApiArgumentHints.Add("body");
}

Expand All @@ -210,7 +238,7 @@ public IReadOnlyCollection<LowLevelClientMethod> LowLevelClientMethods
Parts = parts,
Url = Url,
HasBody = Body != null,
VersionAdded = path.VersionAdded,
VersionAdded = path.VersionAdded,
};
_lowLevelClientMethods.Add(apiMethod);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ApiGenerator/Domain/Specification/UrlPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public string LowLevelTypeName
return Type;
case "list":
return "string";
case "enum":
return Name.ToPascalCase();
case "IndicesStatsMetric?":
return "string";
default:
return Type;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ApiGenerator/Generator/ApiEndpointFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ private static string GetOpenSearchType(JsonSchema schema, Action<string, bool>
{
case ("string", "list"): return second;
case ("boolean", "string"): return first;
case ("string", "number"): return first;
case ("number", _): return "string";
case (_,"number"): return "string";
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace OpenSearch.Net
</text>
}

if(Model.EndpointsPerNamespaceHighLevel.TryGetValue(CsharpNames.RootNamespace, out var endpoints))
if(Model.EndpointsPerNamespaceLowLevel.TryGetValue(CsharpNames.RootNamespace, out var endpoints))
{
foreach(var m in endpoints.SelectMany(e => e.LowLevelClientMethods))
{
Expand Down
Loading
Loading