From d706b5eb6ce949c4b897e6347dc93937a2319650 Mon Sep 17 00:00:00 2001 From: John Niang Date: Mon, 26 May 2025 18:12:18 +0800 Subject: [PATCH] Add support back for using relative permalink (#7475) #### What type of PR is this? /kind bug /area core /milestone 2.21.x #### What this PR does / why we need it: This PR fixes the problem of not working for relative permalink caused by . #### Special notes for your reviewer: 1. Try to start Halo instance with `halo.use-absolute-permalink=false` and check the permalinks of posts and attachments. 1. Try to start Halo instance with `halo.use-absolute-permalink=true` and check the permalinks of posts and attachments. #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../SystemConfigFirstExternalUrlSupplier.java | 7 +++---- ...temConfigFirstExternalUrlSupplierTest.java | 21 +++++++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/run/halo/app/infra/SystemConfigFirstExternalUrlSupplier.java b/application/src/main/java/run/halo/app/infra/SystemConfigFirstExternalUrlSupplier.java index 8f354dd93..b7a3a2337 100644 --- a/application/src/main/java/run/halo/app/infra/SystemConfigFirstExternalUrlSupplier.java +++ b/application/src/main/java/run/halo/app/infra/SystemConfigFirstExternalUrlSupplier.java @@ -63,13 +63,12 @@ class SystemConfigFirstExternalUrlSupplier implements ExternalUrlSupplier { @Override public URI get() { try { - if (externalUrl != null) { - return externalUrl.toURI(); - } if (!haloProperties.isUseAbsolutePermalink()) { return URI.create(getBasePath()); } - + if (externalUrl != null) { + return externalUrl.toURI(); + } return haloProperties.getExternalUrl().toURI(); } catch (URISyntaxException e) { throw Exceptions.propagate(e); diff --git a/application/src/test/java/run/halo/app/infra/SystemConfigFirstExternalUrlSupplierTest.java b/application/src/test/java/run/halo/app/infra/SystemConfigFirstExternalUrlSupplierTest.java index 9c2c36671..97a87619a 100644 --- a/application/src/test/java/run/halo/app/infra/SystemConfigFirstExternalUrlSupplierTest.java +++ b/application/src/test/java/run/halo/app/infra/SystemConfigFirstExternalUrlSupplierTest.java @@ -55,7 +55,7 @@ class SystemConfigFirstExternalUrlSupplierTest { } @Test - void getURIWhenBasePathSetAndNotUsingAbsolutePermalink() throws MalformedURLException { + void getURIWhenBasePathSetAndNotUsingAbsolutePermalink() { when(webFluxProperties.getBasePath()).thenReturn("/blog"); when(haloProperties.isUseAbsolutePermalink()).thenReturn(false); @@ -137,10 +137,11 @@ class SystemConfigFirstExternalUrlSupplierTest { class SystemConfigSupplier { @Test - void shouldGetUrlCorrectly() throws Exception { + void shouldGetUrlWhenUseAbsolutePermalink() throws Exception { var basic = new SystemSetting.Basic(); basic.setExternalUrl("https://www.halo.run"); when(systemConfigFetcher.getBasic()).thenReturn(Mono.just(basic)); + when(haloProperties.isUseAbsolutePermalink()).thenReturn(true); externalUrl.onExtensionInitialized(null); assertEquals(URI.create("https://www.halo.run").toURL(), externalUrl.getRaw()); assertEquals(URI.create("https://www.halo.run"), externalUrl.get()); @@ -150,6 +151,22 @@ class SystemConfigFirstExternalUrlSupplierTest { externalUrl.getURL(mockRequest)); } + @Test + void shouldGetUrlWhenNotUsingAbsolutePermalink() throws MalformedURLException { + var basic = new SystemSetting.Basic(); + basic.setExternalUrl("https://www.halo.run"); + when(systemConfigFetcher.getBasic()).thenReturn(Mono.just(basic)); + when(haloProperties.isUseAbsolutePermalink()).thenReturn(false); + when(webFluxProperties.getBasePath()).thenReturn("/fake"); + externalUrl.onExtensionInitialized(null); + + assertEquals(URI.create("https://www.halo.run").toURL(), externalUrl.getRaw()); + assertEquals(URI.create("/fake"), externalUrl.get()); + var mockRequest = mock(HttpRequest.class); + assertEquals(URI.create("https://www.halo.run").toURL(), + externalUrl.getURL(mockRequest)); + } + } } \ No newline at end of file