From 4f8c6d26ad82ffd01b94252514239ae17ff88d73 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Wed, 27 Dec 2023 15:59:50 +0100 Subject: [PATCH] Fix getApiRootUri in tests. Update changelog --- CHANGELOG.md | 3 +++ .../AbstractResourceAction.java | 9 ++++---- .../resourceactions/crud/CrudActionTest.java | 4 ++-- .../portofino/src/lib/portofino.service.ts | 21 +++++++++++-------- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42d43a2e49..55cf2f1102 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Possibility to specify the project root with --dev for PortofinoDrivenBootApplication - Notes on how to connect using JNDI in `hibernate.properties` +### Changed +- Rationalized how the API root URI is computed across the codebase + ### Fixed - Search field disabled for foreign keys [#618](https://github.com/ManyDesigns/Portofino/issues/618) - Several security vulnerabilities in dependencies diff --git a/portofino-core/src/main/java/com/manydesigns/portofino/resourceactions/AbstractResourceAction.java b/portofino-core/src/main/java/com/manydesigns/portofino/resourceactions/AbstractResourceAction.java index 5ba86c00ad..913b8383f3 100644 --- a/portofino-core/src/main/java/com/manydesigns/portofino/resourceactions/AbstractResourceAction.java +++ b/portofino-core/src/main/java/com/manydesigns/portofino/resourceactions/AbstractResourceAction.java @@ -238,7 +238,7 @@ public String getApiRootUri() { } public static String getApiRootUri(ServletContext servletContext, UriInfo uriInfo) { - return getApiRootUri(servletContext, uriInfo.getBaseUri()); + return getApiRootUri(servletContext, uriInfo != null ? uriInfo.getBaseUri() : null); } public static String getApiRootUri(ServletContext servletContext, URI baseUri) { @@ -246,12 +246,13 @@ public static String getApiRootUri(ServletContext servletContext, URI baseUri) { if (apiRoot == null) { apiRoot = ""; } - if (apiRoot.contains("://")) { - //Keep as is - } else if (!apiRoot.startsWith("/")) { + if (!apiRoot.contains("://") && !apiRoot.startsWith("/")) { apiRoot = servletContext.getContextPath() + "/" + apiRoot; } if (!apiRoot.contains("://")) { + if (baseUri == null) { + throw new IllegalArgumentException("baseUri cannot be null when apiRoot is not absolute (" + apiRoot + ")"); + } String baseAddress = baseUri.getScheme() + "://" + baseUri.getAuthority(); if (!apiRoot.startsWith("/")) { baseAddress += servletContext.getContextPath(); diff --git a/portofino-crud/src/test/java/com/manydesigns/portofino/resourceactions/crud/CrudActionTest.java b/portofino-crud/src/test/java/com/manydesigns/portofino/resourceactions/crud/CrudActionTest.java index 9f3cfcca95..4959cd7995 100644 --- a/portofino-crud/src/test/java/com/manydesigns/portofino/resourceactions/crud/CrudActionTest.java +++ b/portofino-crud/src/test/java/com/manydesigns/portofino/resourceactions/crud/CrudActionTest.java @@ -262,7 +262,7 @@ public void delete() { req.setParameter("name", "name"); req.setParameter("productid", "1"); req.setParameter("category", "BIRDS"); - crudAction.httpPostMultipart(); + crudAction.httpPostMultipart().close(); assertTrue(crudAction.form.validate()); blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn"); assertNotNull(blobField.getValue()); @@ -276,7 +276,7 @@ public void delete() { fail("The blob was not saved"); } - crudAction.httpPutMultipart(); + crudAction.httpPutMultipart().close(); assertTrue(crudAction.form.validate()); blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn"); assertNotNull(blobField.getValue()); diff --git a/ui/projects/portofino/src/lib/portofino.service.ts b/ui/projects/portofino/src/lib/portofino.service.ts index 8b570cf118..2f98904ddd 100644 --- a/ui/projects/portofino/src/lib/portofino.service.ts +++ b/ui/projects/portofino/src/lib/portofino.service.ts @@ -100,16 +100,19 @@ export class PortofinoService { //Avoid refreshing the token on startup as this might hit the wrong login action const headers = {}; headers[NO_REFRESH_TOKEN_HEADER] = true; - this.http.get(this.localApiPath, { headers: headers }).subscribe(response => { - this.apiRoot = this.sanitizeApiRoot(response.apiRoot); - this.injector.get(AuthenticationStrategy).init({ - apiRoot: this.apiRoot - }); - if(response.disableAdministration) { - this.localApiPath = null; //Disable local API + this.http.get(this.localApiPath, { headers: headers }).subscribe({ + next(response) { + this.apiRoot = this.sanitizeApiRoot(response.apiRoot); + this.injector.get(AuthenticationStrategy).init({ + apiRoot: this.apiRoot + }); + if (response.disableAdministration) { + this.localApiPath = null; //Disable local API + } + }, + error() { + this.fallbackInit(); } - }, error => { - this.fallbackInit(); }); }