From 11147d125ce9834b9d26d8039611ccbc94520fe4 Mon Sep 17 00:00:00 2001 From: Andrea De Pirro Date: Wed, 18 Nov 2026 14:55:04 +0100 Subject: [PATCH] Serve the docs API's OpenAPI paths relative to www.elastic.co/docs/_api/v1 Co-Authored-By: Claude Opus 5.5 --- .../OpenApi/ApiExamples.cs | 21 +++++++++-- .../ServicesExtension.cs | 1 + .../OpenApiDocumentTests.cs | 36 ++++++++++++------- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/api/Elastic.Documentation.Api/OpenApi/ApiExamples.cs b/src/api/Elastic.Documentation.Api/OpenApi/ApiExamples.cs index 403a00278d..ad35a67305 100644 --- a/src/api/Elastic.Documentation.Api/OpenApi/ApiExamples.cs +++ b/src/api/Elastic.Documentation.Api/OpenApi/ApiExamples.cs @@ -20,7 +20,13 @@ namespace Elastic.Documentation.Api.OpenApi; internal static class ApiExamples { /// The public host the examples call. - internal const string Server = "https://www.elastic.co"; + internal const string Origin = "https://www.elastic.co"; + + /// Where the API is mounted on ; paths in the document are relative to it. + internal const string BasePath = "/docs/_api/v1"; + + /// The document's server: every path in it follows this URL. + internal const string Server = Origin + BasePath; private const string ResourcePrefix = "openapi-examples/"; @@ -186,13 +192,13 @@ internal static JsonArray CodeSamples(IReadOnlyList examples) return samples; } - /// A curl command for an example request, against . + /// A curl command for an example request, against . internal static string Curl(Example example) { var curl = new StringBuilder("curl"); if (example.Method != "GET") _ = curl.Append(" -X ").Append(example.Method); - _ = curl.Append(" '").Append(Server).Append(example.PathAndQuery).Append('\''); + _ = curl.Append(" '").Append(Origin).Append(example.PathAndQuery).Append('\''); if (example.Body is { } body) _ = curl.Append(" \\\n -H 'Content-Type: application/json' \\\n -d '").Append(body.ToJsonString()).Append('\''); return curl.ToString(); @@ -315,4 +321,13 @@ private static string AskAiStream() ]; return string.Concat(events.Select(e => $"data: {JsonSerializer.Serialize(e, AskAiEventJsonContext.Default.AskAiEvent)}\n\n")); } + + /// Makes every path relative to , which already names . + internal static void RelativizePaths(OpenApiDocument document) + { + var paths = new OpenApiPaths(); + foreach (var (path, item) in document.Paths) + paths[path.StartsWith(BasePath + "/", StringComparison.Ordinal) ? path[BasePath.Length..] : path] = item; + document.Paths = paths; + } } diff --git a/src/api/Elastic.Documentation.Api/ServicesExtension.cs b/src/api/Elastic.Documentation.Api/ServicesExtension.cs index a9c3dbd689..b90f1ba0e0 100644 --- a/src/api/Elastic.Documentation.Api/ServicesExtension.cs +++ b/src/api/Elastic.Documentation.Api/ServicesExtension.cs @@ -87,6 +87,7 @@ private static void AddElasticDocsApiServices(this IServiceCollection services, Description = "Search, Ask AI, change feeds, feedback and the API reference behind elastic.co/docs." }; document.Servers = [new OpenApiServer { Url = OpenApi.ApiExamples.Server }]; + OpenApi.ApiExamples.RelativizePaths(document); return Task.CompletedTask; })); diff --git a/tests/Elastic.Documentation.Api.Tests/OpenApiDocumentTests.cs b/tests/Elastic.Documentation.Api.Tests/OpenApiDocumentTests.cs index 0124cb9ea9..a6903f47dd 100644 --- a/tests/Elastic.Documentation.Api.Tests/OpenApiDocumentTests.cs +++ b/tests/Elastic.Documentation.Api.Tests/OpenApiDocumentTests.cs @@ -50,18 +50,18 @@ public async Task Document_DescribesEveryEndpointWithItsStatusCodes() .ToList(); operations.Should().Equal( - "DELETE /docs/_api/v1/page-feedback/{feedbackId} 204,500,503", - "GET /docs/_api/v1/apis 200,500", - "GET /docs/_api/v1/apis/{api} 200,404,500", - "GET /docs/_api/v1/apis/{api}/groups 200,400,404,500", - "GET /docs/_api/v1/apis/{api}/operations 200,400,404,500,503", - "GET /docs/_api/v1/apis/{api}/operations/{operationId} 200,400,404,500", - "GET /docs/_api/v1/changes 200,400,500,503", - "GET /docs/_api/v1/navigation-search 200,400,500,503", - "GET /docs/_api/v1/search 200,400,500,503", - "POST /docs/_api/v1/ask-ai/message-feedback 204,400,500,503", - "POST /docs/_api/v1/ask-ai/stream 200,400,500", - "PUT /docs/_api/v1/page-feedback/{feedbackId} 204,400,500,503" + "DELETE /page-feedback/{feedbackId} 204,500,503", + "GET /apis 200,500", + "GET /apis/{api} 200,404,500", + "GET /apis/{api}/groups 200,400,404,500", + "GET /apis/{api}/operations 200,400,404,500,503", + "GET /apis/{api}/operations/{operationId} 200,400,404,500", + "GET /changes 200,400,500,503", + "GET /navigation-search 200,400,500,503", + "GET /search 200,400,500,503", + "POST /ask-ai/message-feedback 204,400,500,503", + "POST /ask-ai/stream 200,400,500", + "PUT /page-feedback/{feedbackId} 204,400,500,503" ); } @@ -122,6 +122,16 @@ public async Task Document_NamesTheApiAndThePublicServer() document.GetProperty("info").GetProperty("title").GetString().Should().Be("Elastic Docs API"); // The CDN no longer gates the API behind a cookie, so the description names none. document.GetProperty("info").GetProperty("description").GetString().Should().NotContainEquivalentOf("cookie"); - document.GetProperty("servers")[0].GetProperty("url").GetString().Should().Be("https://www.elastic.co"); + document.GetProperty("servers")[0].GetProperty("url").GetString().Should().Be("https://www.elastic.co/docs/_api/v1"); + } + + [Test] + public async Task Paths_AreRelativeToTheServer() + { + var document = await Document(); + + var paths = document.GetProperty("paths").EnumerateObject().Select(p => p.Name).ToList(); + paths.Should().Contain(["/search", "/apis/{api}/operations/{operationId}"]); + paths.Should().AllSatisfy(p => p.Should().NotStartWith("/docs")); } }