Open hatzhang opened 1 year ago
hi @hatzhang, this route is only used for setting up the routing internally and that placeholder ({document}) is actually replaced by the corresponding document names when using the feature "named documents". therefore that URL is not expected to hit the browser.
could you please show how your setup looks like?
The request behaves that way, because of wrong url had been feeded to front end. In the following method of AsyncApiUiMiddleware
, asyncApiDocumentUrl
is given the value of "/m-module/%7Bdocument%7D/asynciapi/asyncapi.json"
, thus frontend will request the wrong url, thus leads to the result that the AsyncApiMiddleware
can not find the pattern of {document}
.
private async Task RespondWithAsyncApiHtml(HttpResponse response, string route)
{
using (var stream = GetType().Assembly.GetManifestResourceStream($"{GetType().Namespace}.index.html"))
using (var reader = new StreamReader(stream))
{
var indexHtml = new StringBuilder(await reader.ReadToEndAsync());
// Replace dynamic content such as the AsyncAPI document url
foreach (var replacement in new Dictionary<string, string>
{
["{{title}}"] = _options.Middleware.UiTitle,
["{{asyncApiDocumentUrl}}"] = route,
})
{
indexHtml.Replace(replacement.Key, replacement.Value);
}
response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = MediaTypeNames.Text.Html;
await response.WriteAsync(indexHtml.ToString(), Encoding.UTF8);
}
}
The setup is simple, multi-documents are leveraged:
services.AddAsyncApiSchemaGeneration(options =>
{
options.Middleware.Route = "/{document}/asyncapi/asyncapi.json";
options.Middleware.UiBaseRoute = "/{document}/asyncapi/ui/";
});
services.ConfigureNamedAsyncApi("module1", asyncApi =>
{
asyncApi.Info = new Info("module1", "0.0.1");
});
services.ConfigureNamedAsyncApi("module2", asyncApi =>
{
asyncApi.Info = new Info("module2", "0.0.1");
});
And UsePathBase before UseRouting should be able to re-create this.
app.UsePathBase("/m-module");
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapAsyncApiDocuments();
endpoints.MapAsyncApiUi();
endpoints.MapControllers();
});
And the following method should also return value of PathString, or the /ui/*
won't work.
private string GetUiIndexFullRoute(HttpRequest request)
{
if (request.PathBase != null)
{
return request.PathBase.Add(UiIndexRoute).Value;
}
return UiIndexRoute;
}
what .NET version are you using?
can you please include also the classes that represent those two named documents?
did you also try to tell saunter where to search for those classes in AddAsyncApiSchemaGeneration?
options.AssemblyMarkerTypes = new[] { typeof(Startup) };
I am using net6. And all settings should be good. If i fix those two methods, it will work. Or it shows that error.
The following test case will pass:
So in the method
GetDocumentFullRoute
ofAsyncApiUiMiddleware
, PathString's value should be returned. Or the api endpoint won't be hit as expected.