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
16 changes: 15 additions & 1 deletion src/SwaggerDiff.AspNetCore/Extensions/SwaggerDiffExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,22 @@ public static WebApplication UseSwaggerDiff(this WebApplication app)
await using var stream = Assembly.GetManifestResourceStream(SwaggerDiffIndexResource);
if (stream != null)
{
// Inside a Map() branch, PathBase includes the RoutePrefix.
// Strip it to recover the application's actual PathBase so the
// frontend can prefix API calls correctly.
var branchPathBase = context.Request.PathBase.Value?.TrimEnd('/') ?? "";
var normalizedPrefix = options.RoutePrefix.TrimEnd('/');
var pathBase = branchPathBase.EndsWith(normalizedPrefix, StringComparison.OrdinalIgnoreCase)
? branchPathBase[..^normalizedPrefix.Length]
: branchPathBase;
pathBase = pathBase.TrimEnd('/');

using var reader = new StreamReader(stream);
var html = (await reader.ReadToEndAsync())
.Replace("{{PATH_BASE}}", pathBase);

context.Response.ContentType = "text/html";
await stream.CopyToAsync(context.Response.Body);
await context.Response.WriteAsync(html);
return;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/SwaggerDiff.AspNetCore/wwwroot/swagger-diff-tool.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
const target = document.querySelector('.topbar .download-url-wrapper');

if (target && !document.getElementById('swagger-diff-btn')) {
// Derive PathBase from the current URL.
// SwaggerUI lives at {pathBase}/swagger, so strip the swagger
// segment to get the application's PathBase prefix.
const swaggerBase = window.location.pathname.replace(/\/swagger\b.*$/, '');

const btn = document.createElement('a');
btn.href = '/swagger-diff';
btn.href = swaggerBase + '/swagger-diff';
btn.innerHTML = `<svg xmlns='http://www.w3.org/2000/svg' height='16' viewBox='0 0 24 24' width='16' fill='white'><path d='M0 0h24v24H0V0z' fill='none'/><path d='M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0s.41-1.08 0-1.49L15.5 14zM5 9.5C5 7.01 7.01 5 9.5 5S14 7.01 14 9.5 11.99 14 9.5 14 5 11.99 5 9.5z'/></svg>Swagger Diff Tool`;
btn.className = 'swagger-diff-btn';
btn.id = 'swagger-diff-btn';
Expand Down
11 changes: 7 additions & 4 deletions src/SwaggerDiff.AspNetCore/wwwroot/swagger-diff/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
<body>
<div class="container">
<div class="header-row">
<a href="/swagger" class="back-link">&#128281; Back to Swagger</a>
<a href="{{PATH_BASE}}/swagger" class="back-link">&#128281; Back to Swagger</a>
<h2 class="title">Swagger Diff Viewer</h2>
</div>

Expand Down Expand Up @@ -241,6 +241,9 @@ <h2 class="title">Swagger Diff Viewer</h2>
<div id="toast" class="toast"></div>

<script>
// PathBase injected at serve-time (empty string when no reverse proxy prefix)
const pathBase = '{{PATH_BASE}}';

// State
let versions = [];
let oldVersionName = '';
Expand Down Expand Up @@ -268,7 +271,7 @@ <h2 class="title">Swagger Diff Viewer</h2>

// Copy shareable link
function copyLink() {
const url = `${window.location.origin}/swagger-diff?old=${oldVersionName}&new=${newVersionName}&mode=${mode}`;
const url = `${window.location.origin}${window.location.pathname}?old=${oldVersionName}&new=${newVersionName}&mode=${mode}`;
navigator.clipboard.writeText(url)
.then(() => showToast('Link copied to clipboard!'));
}
Expand Down Expand Up @@ -334,7 +337,7 @@ <h2 class="title">Swagger Diff Viewer</h2>
// Fetch versions from API
async function fetchVersions() {
try {
const res = await fetch('/api-docs/versions');
const res = await fetch(pathBase + '/api-docs/versions');
const json = await res.json();
if (!json.isSuccess) throw new Error(json.message || 'Unable to load versions');
versions = json.data;
Expand All @@ -355,7 +358,7 @@ <h2 class="title">Swagger Diff Viewer</h2>
diffContainer.style.display = 'none';

try {
const res = await fetch('/api-docs/compare', {
const res = await fetch(pathBase + '/api-docs/compare', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
Expand Down