Skip to content

Commit 37f9bd0

Browse files
committed
fix: avoid mutating resource paths in ODataRequestCount
1 parent 5f2191c commit 37f9bd0

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

datamodel/odata-client/src/main/java/com/sap/cloud/sdk/datamodel/odata/client/request/ODataRequestCount.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ODataRequestCount(
5454
@Nullable final String encodedQuery,
5555
@Nonnull final ODataProtocol protocol )
5656
{
57-
super(servicePath, resourcePath.addSegment("$count"), encodedQuery, protocol);
57+
super(servicePath, appendCountResourcePath(resourcePath), encodedQuery, protocol);
5858
}
5959

6060
/**
@@ -74,8 +74,24 @@ public ODataRequestCount(
7474
{
7575
this(
7676
servicePath,
77-
resourcePath.addSegment(query.getEntityOrPropertyName()),
77+
appendResourcePath(resourcePath, query.getEntityOrPropertyName()),
7878
query.getEncodedQueryString(),
7979
query.getProtocol());
8080
}
81+
82+
@Nonnull
83+
private static ODataResourcePath appendCountResourcePath( @Nonnull final ODataResourcePath resourcePath )
84+
{
85+
return appendResourcePath(resourcePath, "$count");
86+
}
87+
88+
@Nonnull
89+
private static
90+
ODataResourcePath
91+
appendResourcePath( @Nonnull final ODataResourcePath resourcePath, @Nonnull final String segment )
92+
{
93+
final ODataResourcePath appendedPath = new ODataResourcePath();
94+
resourcePath.getSegments().forEach(s -> appendedPath.addSegment(s._1, s._2));
95+
return appendedPath.addSegment(segment);
96+
}
8197
}

datamodel/odata-client/src/test/java/com/sap/cloud/sdk/datamodel/odata/client/request/ODataRequestCountTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,14 @@ void testConstructorWithStructuredQuery()
9292
assertThat(actual).isEqualTo(expected);
9393
assertThat(actual.getQueryString()).isEqualTo(expected.getQueryString());
9494
}
95+
96+
@Test
97+
void testConstructorDoesNotMutateResourcePath()
98+
{
99+
final ODataResourcePath resourcePath = ODataResourcePath.of(ENTITY_NAME);
100+
101+
new ODataRequestCount(SERVICE_PATH, resourcePath, "", ODataProtocol.V4);
102+
103+
assertThat(resourcePath.toString()).isEqualTo("/" + ENTITY_NAME);
104+
}
95105
}

datamodel/odata-v4/odata-v4-core/src/test/java/com/sap/cloud/sdk/datamodel/odatav4/core/NestedEntityTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ void testCountNestedTrip()
174174
assertThat(count.getRelativeUri()).hasToString("/TripPinServiceRW/People('russellwhyte')/Trips/$count");
175175
}
176176

177+
@Test
178+
void testCountNestedTripToRequestIsStable()
179+
{
180+
final Person personByKey = Person.builder().userName("russellwhyte").build();
181+
final CountRequestBuilder<Trip> countBuilder =
182+
service.forEntity(personByKey).navigateTo(Person.TO_TRIPS).count();
183+
184+
assertThat(countBuilder.toRequest().getRelativeUri())
185+
.hasToString("/TripPinServiceRW/People('russellwhyte')/Trips/$count");
186+
assertThat(countBuilder.toRequest().getRelativeUri())
187+
.hasToString("/TripPinServiceRW/People('russellwhyte')/Trips/$count");
188+
}
189+
177190
@Test
178191
void testGetByKeyNestedPlanItem()
179192
{

0 commit comments

Comments
 (0)