From 0605904ce9b0a96bd9058f29fbaa5da3fc373b2f Mon Sep 17 00:00:00 2001 From: Norbert Hartl Date: Thu, 29 Oct 2026 09:58:16 +0100 Subject: [PATCH] Let the uri builder stay inside the route it was asked from A resource the current route already stands at - its receiver, or the resource the receiver stands for (extraReceiver) - takes the rest of its uri from the route instead of the canonical tree. So far only AGBaseUriBuilder in apptive-base did this; the walk only uses HOP terms, so it belongs here. Whether a resource is in scope is asked of the uri space (isResource:inScopeOf:), defaulting to identity. A space whose objects can be database proxies overrides it there instead of HOP knowing about proxies. Co-Authored-By: Claude Opus 5.5 --- .../HOPTestResourceRoute.class.st | 23 +++++++++ .../HOP-Core-Tests/HOPUriBuilderTest.class.st | 48 +++++++++++++++++++ source/HOP-Core-Tests/HOPUriStandIn.class.st | 28 +++++++++++ source/HOP-Core/HOPUriBuilder.class.st | 14 +++++- source/HOP-Core/HOPUriSpace.class.st | 9 ++++ 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 source/HOP-Core-Tests/HOPTestResourceRoute.class.st create mode 100644 source/HOP-Core-Tests/HOPUriStandIn.class.st diff --git a/source/HOP-Core-Tests/HOPTestResourceRoute.class.st b/source/HOP-Core-Tests/HOPTestResourceRoute.class.st new file mode 100644 index 0000000..614a8bc --- /dev/null +++ b/source/HOP-Core-Tests/HOPTestResourceRoute.class.st @@ -0,0 +1,23 @@ +" +A resource route that resolves its stand-in segment to a fixed object, so a test can build uris relative to a route without a model behind it. +" +Class { + #name : 'HOPTestResourceRoute', + #superclass : 'HOPResourceRoute', + #instVars : [ + 'standIn' + ], + #category : 'HOP-Core-Tests-TestData', + #package : 'HOP-Core-Tests', + #tag : 'TestData' +} + +{ #category : 'accessing' } +HOPTestResourceRoute >> standIn [ + ^ standIn +] + +{ #category : 'accessing' } +HOPTestResourceRoute >> standIn: anObject [ + standIn := anObject +] diff --git a/source/HOP-Core-Tests/HOPUriBuilderTest.class.st b/source/HOP-Core-Tests/HOPUriBuilderTest.class.st index d1b68eb..3bc07b0 100644 --- a/source/HOP-Core-Tests/HOPUriBuilderTest.class.st +++ b/source/HOP-Core-Tests/HOPUriBuilderTest.class.st @@ -56,3 +56,51 @@ HOPUriBuilderTest >> testBuildUriForResourceInAMountedUriSpace [ uri := uriSpace buildUriForResource: object. self assert: uri equals: '/mounted/objects/1/children/2' ] + +{ #category : 'tests' } +HOPUriBuilderTest >> testBuildUriForResourceThroughAStandIn [ + "an object below the resource a stand-in gives access to keeps the stand-in's uri as its + prefix when the request came through the stand-in" + | uriSpace route object child | + uriSpace := self uriSpaceWithStandIn. + object := HOPUriBuildObject new. + child := HOPUriBuildChildObject new container: object. + route := (HOPTestResourceRoute new + routeMatch: (uriSpace newRouteMatcher matchUri: '/api/standins/7' asZnUrl); + standIn: (HOPUriStandIn new standsFor: object); + yourself). + self + assert: (uriSpace newUriBuilder resource: child; resourceRoute: route; build) + equals: '/api/standins/7/children/2'. + self + assert: (uriSpace newUriBuilder resource: child; build) + equals: '/api/objects/1/children/2' +] + +{ #category : 'tests' } +HOPUriBuilderTest >> testBuildUriOutsideTheScopeOfTheRoute [ + "a resource the stand-in does not lead to is built the canonical way" + | uriSpace route child | + uriSpace := self uriSpaceWithStandIn. + child := HOPUriBuildChildObject new container: HOPUriBuildObject new. + route := (HOPTestResourceRoute new + routeMatch: (uriSpace newRouteMatcher matchUri: '/api/standins/7' asZnUrl); + standIn: (HOPUriStandIn new standsFor: HOPUriBuildObject new); + yourself). + self + assert: (uriSpace newUriBuilder resource: child; resourceRoute: route; build) + equals: '/api/objects/1/children/2' +] + +{ #category : 'running' } +HOPUriBuilderTest >> uriSpaceWithStandIn [ + "objects have their canonical place under /api/objects, a stand-in for one of them lives + under /api/standins" + | api uriSpace | + api := #api asHOPSegment. + uriSpace := (HOPUriSpace /+ api) root. + api /+ #objects /+ (HOPVariableSegment new name: #objectId; segmentNameSelector: #id; yourself) --> HOPUriBuildObject + /+ #children /+ (HOPVariableSegment new name: #childId; segmentNameSelector: #id; yourself) --> HOPUriBuildChildObject. + api /+ #standins /+ (HOPVariableSegment new name: #standInId; segmentNameSelector: #id; resourceSelector: #standIn; yourself) --> HOPUriStandIn. + ^ uriSpace +] diff --git a/source/HOP-Core-Tests/HOPUriStandIn.class.st b/source/HOP-Core-Tests/HOPUriStandIn.class.st new file mode 100644 index 0000000..715d440 --- /dev/null +++ b/source/HOP-Core-Tests/HOPUriStandIn.class.st @@ -0,0 +1,28 @@ +" +I stand in for another resource, the way a link or a hook gives access to a space: a route through me continues at the resource I stand for. +" +Class { + #name : 'HOPUriStandIn', + #superclass : 'HOPUriObject', + #instVars : [ + 'standsFor' + ], + #category : 'HOP-Core-Tests-TestData', + #package : 'HOP-Core-Tests', + #tag : 'TestData' +} + +{ #category : 'accessing' } +HOPUriStandIn >> extraReceiver [ + ^ standsFor +] + +{ #category : 'accessing' } +HOPUriStandIn >> id [ + ^ '7' +] + +{ #category : 'accessing' } +HOPUriStandIn >> standsFor: anObject [ + standsFor := anObject +] diff --git a/source/HOP-Core/HOPUriBuilder.class.st b/source/HOP-Core/HOPUriBuilder.class.st index 037aeb2..14e3d75 100644 --- a/source/HOP-Core/HOPUriBuilder.class.st +++ b/source/HOP-Core/HOPUriBuilder.class.st @@ -74,7 +74,19 @@ HOPUriBuilder >> visitUriSpace: aUriSpace [ ] { #category : 'visiting' } -HOPUriBuilder >> visitVariableSegment: aHOPVariableSegment [ +HOPUriBuilder >> visitVariableSegment: aHOPVariableSegment [ + "walking up from the resource, a resource the current route already stands at takes the + rest of its uri from the route instead of the canonical tree. This keeps a link inside + whatever the request came through, e.g. an object reached via a stand-in for it (see + Object>>extraReceiver). A route continued at another resource looks at the route it + continued from" + resourceRoute ifNotNil: [ + | effectiveRoute | + effectiveRoute := resourceRoute previous ifNil: [ resourceRoute ]. + (uriSpace isResource: resource inScopeOf: effectiveRoute receiver) ifTrue: [ + effectiveRoute receiverSegments reversed + do: [ :each | segments add: (each segmentIn: effectiveRoute) ]. + ^ self ] ]. segments add: (self valueFor: aHOPVariableSegment) . resource := self containerOf: aHOPVariableSegment . super visitVariableSegment: aHOPVariableSegment diff --git a/source/HOP-Core/HOPUriSpace.class.st b/source/HOP-Core/HOPUriSpace.class.st index 25b215c..ea1f4e2 100644 --- a/source/HOP-Core/HOPUriSpace.class.st +++ b/source/HOP-Core/HOPUriSpace.class.st @@ -113,6 +113,15 @@ HOPUriSpace >> initialize [ operationSegments := Dictionary new. ] +{ #category : 'testing' } +HOPUriSpace >> isResource: anObject inScopeOf: aReceiver [ + "anObject is in the scope of a route's receiver when it is the receiver or the resource the + receiver stands for. A space whose objects can be stand-ins for themselves, like + database proxies, compares them its own way" + + ^ anObject == aReceiver or: [ anObject == aReceiver extraReceiver ] +] + { #category : 'printing' } HOPUriSpace >> isRoot [ ^ parent isNil