Skip to content
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
14 changes: 13 additions & 1 deletion src/Elastic.ApiExplorer/OpenApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,21 @@ Cancel ctx
if (stream is null)
return null;

return await _openApiReader.ReadAsync(stream, apiConfig.SpecFileName, context.Collector).ConfigureAwait(false);
var document = await _openApiReader.ReadAsync(stream, apiConfig.SpecFileName, context.Collector).ConfigureAwait(false);
// The moniker, not the branch, marks the unreleased spec: some repositories publish main from master.
if (version.Moniker != "main" && document?.Info is { } info)
info.Description = WithSourceBranch(info.Description, version.Version);
return document;
}

/// <summary>
/// A released spec can be cut from <c>main</c> before its description names the release branch, so the page
/// would claim it is derived from <c>main</c>. Name the branch the version index actually published.
/// </summary>
// TODO: remove once the specs drop this wording and the page renders the branch from the version index (elastic/docs-eng-team#1237).
internal static string? WithSourceBranch(string? description, string branch) =>
description?.Replace("derived from the `main` branch", $"derived from the `{branch}` branch", StringComparison.Ordinal);

private static readonly OpenApiDocument CatalogDocument = new()
{
Info = new OpenApiInfo { Title = ApiCatalog.PageTitle, Version = "1.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,63 @@ public async Task ResolveDocumentsForProduct_MultiMajorIndex_ResolvesMainAndNume
);
}

[Test]
public async Task ResolveDocumentsForProduct_ReleasedSpecSaysMainBranch_NamesTheIndexBranch()
{
var collector = new DiagnosticsCollector([]);
var stack = TestHelpers.CreateStackVersionsConfiguration(currentMajor: 9);
var product = TestHelpers.CreateProduct("elasticsearch", stack.GetVersioningSystem(VersioningSystemId.Stack));
var context = TestHelpers.CreateScopedBuildContext(collector, stack, ProductsFor(product), GitForElasticsearch());
// main is published from master, as elastic/cloud does: the unreleased spec must keep its wording.
var handler = new StubHandler(
request => request.RequestUri!.AbsolutePath.EndsWith("index.json", StringComparison.Ordinal)
? IndexResponse(/*lang=json,strict*/
"""
{
"elastic/elasticsearch": {
"elasticsearch-openapi.json": {
"main": { "version": "master" },
"9": { "version": "9.4" },
"8": { "version": "8.19" }
}
}
}
"""
)
: SpecResponse(request)
);
using var versionIndexClient = new VersionIndexClient(BaseUri, handler, sleep: (_, _) => Task.CompletedTask);
var reader = CreateVersionedReader(
("master", SourceBranchSpec("main")),
("9.4", SourceBranchSpec("main")),
("8.19", SourceBranchSpec("8.19"))
);
var generator = CreateGenerator(context, versionIndexClient, reader);

var resolved = await generator.ResolveDocumentsForProduct(
"elasticsearch",
ApiConfig(product),
TestContext.Current!.Execution.CancellationToken
);

resolved
.Documents
.Select(d => $"{d.Version.Moniker}: {d.Document.Info.Description}")
.Should()
.BeEquivalentTo(
"main: This documentation is derived from the `main` branch of the repository.",
"9: This documentation is derived from the `9.4` branch of the repository.",
"8: This documentation is derived from the `8.19` branch of the repository."
);
}

private static OpenApiDocument SourceBranchSpec(string branch)
{
var document = SpecDocument("Elasticsearch");
document.Info.Description = $"This documentation is derived from the `{branch}` branch of the repository.";
return document;
}

[Test]
public async Task ResolveDocumentsForProduct_VersionNamedNext_ReportsAnErrorAndLeavesItOut()
{
Expand Down
Loading