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
21 changes: 18 additions & 3 deletions src/api/Elastic.Documentation.Api/OpenApi/ApiExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ namespace Elastic.Documentation.Api.OpenApi;
internal static class ApiExamples
{
/// <summary>The public host the examples call.</summary>
internal const string Server = "https://www.elastic.co";
internal const string Origin = "https://www.elastic.co";

/// <summary>Where the API is mounted on <see cref="Origin"/>; paths in the document are relative to it.</summary>
internal const string BasePath = "/docs/_api/v1";

/// <summary>The document's server: every path in it follows this URL.</summary>
internal const string Server = Origin + BasePath;

private const string ResourcePrefix = "openapi-examples/";

Expand Down Expand Up @@ -186,13 +192,13 @@ internal static JsonArray CodeSamples(IReadOnlyList<Example> examples)
return samples;
}

/// <summary>A curl command for an example request, against <see cref="Server"/>.</summary>
/// <summary>A curl command for an example request, against <see cref="Origin"/>.</summary>
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();
Expand Down Expand Up @@ -315,4 +321,13 @@ private static string AskAiStream()
];
return string.Concat(events.Select(e => $"data: {JsonSerializer.Serialize(e, AskAiEventJsonContext.Default.AskAiEvent)}\n\n"));
}

/// <summary>Makes every path relative to <see cref="Server"/>, which already names <see cref="BasePath"/>.</summary>
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;
}
}
1 change: 1 addition & 0 deletions src/api/Elastic.Documentation.Api/ServicesExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}));

Expand Down
36 changes: 23 additions & 13 deletions tests/Elastic.Documentation.Api.Tests/OpenApiDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}

Expand Down Expand Up @@ -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"));
}
}
Loading