From 886b80931effed31d79549d3ae0bcf4d2c296aa1 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 22 Nov 2026 15:19:16 +1100 Subject: [PATCH] Assorted bug fixes (#932) - Fix broken URLs - Fix incorrect API version check - Remove old debug prints - Additional unit testing --- lib/api.dart | 6 +-- lib/inventree/part.dart | 19 +++++---- lib/widget/part/part_detail.dart | 10 +---- test/part_urls_test.dart | 73 ++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 test/part_urls_test.dart diff --git a/lib/api.dart b/lib/api.dart index bbe55c25..48cfa130 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -331,8 +331,8 @@ class InvenTreeAPI { bool get supportsPartRequirements => apiVersion >= 350; // Does the server support the "modern" (consolidated) parameter API? - // Ref: https://github.com/inventree/InvenTree/pull/10699 - bool get supportsModernParameters => apiVersion >= 429; + // Ref: https://github.com/inventree/InvenTree/pull/10699 (API v430) + bool get supportsModernParameters => apiVersion >= 430; // Does the support support the "primary" field on SupplierPart model? bool get supportsSupplierPartPrimaryField => apiVersion >= 456; @@ -2196,7 +2196,7 @@ class InvenTreeAPI { body["location"] = location.toString(); } - post("/api/locate/", body: body, expectedStatusCode: 200).then(( + post("locate/", body: body, expectedStatusCode: 200).then(( APIResponse response, ) { if (response.successful()) { diff --git a/lib/inventree/part.dart b/lib/inventree/part.dart index 8b50c846..738d2a27 100644 --- a/lib/inventree/part.dart +++ b/lib/inventree/part.dart @@ -215,12 +215,19 @@ class InvenTreePart extends InvenTreeModel { }); } + // API endpoints for this part (relative to the API root) + String get serialNumbersUrl => "${URL}${pk}/serial-numbers/"; + + String get requirementsUrl => "${URL}${pk}/requirements/"; + + String get pricingUrl => InvenTreeAPI().supportsPricingRole + ? "pricing/part/${pk}/" + : "${URL}${pk}/pricing/"; + // Request requirements information for this part Future getRequirements() async { try { - final response = await InvenTreeAPI().get( - "/api/part/${pk}/requirements/", - ); + final response = await InvenTreeAPI().get(requirementsUrl); if (response.isValid()) { final requirementsData = response.data; @@ -244,12 +251,8 @@ class InvenTreePart extends InvenTreeModel { return null; } - final String url = api.supportsPricingRole - ? "/api/pricing/part/${pk}/" - : "/api/part/${pk}/pricing/"; - try { - final response = await api.get(url); + final response = await api.get(pricingUrl); if (response.isValid()) { final pricingData = response.data; diff --git a/lib/widget/part/part_detail.dart b/lib/widget/part/part_detail.dart index f665f64c..c3274a97 100644 --- a/lib/widget/part/part_detail.dart +++ b/lib/widget/part/part_detail.dart @@ -813,7 +813,7 @@ class _PartDisplayState extends RefreshableState { // read the next available serial number showLoadingOverlay(); var response = await api.get( - "/api/part/${part.pk}/serial-numbers/", + part.serialNumbersUrl, expectedStatusCode: null, ); hideLoadingOverlay(); @@ -822,19 +822,11 @@ class _PartDisplayState extends RefreshableState { data["serial_numbers"] = response.data["next"] ?? response.data["latest"]; } - - print( - "response: " + - response.statusCode.toString() + - response.data.toString(), - ); } else { // Cannot set serial numbers for non-trackable parts fields.remove("serial_numbers"); } - print("data: ${data.toString()}"); - InvenTreeStockItem().createForm( context, L10().stockItemCreate, diff --git a/test/part_urls_test.dart b/test/part_urls_test.dart new file mode 100644 index 00000000..af4e31d1 --- /dev/null +++ b/test/part_urls_test.dart @@ -0,0 +1,73 @@ +/* + * Unit tests for part API endpoint URLs and version checks + * (regression tests for B14 and B15). + * + * No server is required. + */ + +import "package:flutter_test/flutter_test.dart"; + +import "package:inventree/api.dart"; +import "package:inventree/inventree/part.dart"; +import "package:inventree/user_profile.dart"; + +void main() { + final api = InvenTreeAPI(); + + tearDown(() { + api.serverInfo = {}; + api.profile = null; + }); + + // B14: endpoint URLs are relative to the API root (not the host root) + group("Part URLs:", () { + final part = InvenTreePart.fromJson({"pk": 12}); + + test("Relative paths", () { + api.serverInfo = {"apiVersion": 568}; + + for (final url in [ + part.serialNumbersUrl, + part.requirementsUrl, + part.pricingUrl, + ]) { + expect(url.startsWith("/"), isFalse, reason: url); + expect(url.contains("api/"), isFalse, reason: url); + } + + expect(part.serialNumbersUrl, equals("part/12/serial-numbers/")); + expect(part.requirementsUrl, equals("part/12/requirements/")); + }); + + test("Pricing URL depends on server version", () { + api.serverInfo = {"apiVersion": 561}; + expect(part.pricingUrl, equals("part/12/pricing/")); + + api.serverInfo = {"apiVersion": 562}; + expect(part.pricingUrl, equals("pricing/part/12/")); + }); + + test("Resolved against the server API root", () { + api.profile = UserProfile(name: "Test", server: "https://example.com"); + + expect( + api.makeApiUrl(part.serialNumbersUrl), + equals("https://example.com/api/part/12/serial-numbers/"), + ); + + expect( + api.makeApiUrl("locate/"), + equals("https://example.com/api/locate/"), + ); + }); + }); + + // B15: the generic parameter API was added in v430 (not v429) + test("Modern parameters version", () { + api.serverInfo = {"apiVersion": 429}; + expect(api.supportsModernParameters, isFalse); + + api.serverInfo = {"apiVersion": 430}; + expect(api.supportsModernParameters, isTrue); + }); +}