Skip to content

Commit 0324fbd

Browse files
committed
Operation ids for actuator endpoints. Fixes #1233.
1 parent c6ae11f commit 0324fbd

File tree

16 files changed

+173
-152
lines changed

16 files changed

+173
-152
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/customizers/ActuatorOperationCustomizer.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323

2424
package org.springdoc.core.customizers;
2525

26+
import java.util.HashMap;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
29+
2630
import io.swagger.v3.oas.models.Operation;
2731

2832
import org.springframework.web.method.HandlerMethod;
2933

34+
import static org.apache.commons.lang3.math.NumberUtils.INTEGER_ONE;
3035
import static org.springdoc.core.ActuatorProvider.getTag;
3136

3237
/**
@@ -38,16 +43,34 @@ public class ActuatorOperationCustomizer implements OperationCustomizer {
3843
/**
3944
* The Method count.
4045
*/
41-
private int methodCount;
46+
private HashMap<String, Integer> methodCountMap = new HashMap<>();
4247

4348

49+
/**
50+
* The regex pattern for operationId lookup.
51+
*/
52+
private static final Pattern pattern = Pattern.compile(".*'([^']*)'.*");
53+
4454
@Override
4555
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
4656
if (operation.getTags() != null && operation.getTags().contains(getTag().getName())) {
4757
String summary = handlerMethod.toString();
58+
Matcher matcher = pattern.matcher(summary);
59+
String operationId = operation.getOperationId();
60+
while (matcher.find()) {
61+
operationId = matcher.group(1);
62+
}
63+
if (methodCountMap.containsKey(operationId)) {
64+
Integer methodCount = methodCountMap.get(operationId)+1;
65+
methodCountMap.put(operationId, methodCount);
66+
operationId = operationId + "_" + methodCount;
67+
}
68+
else
69+
methodCountMap.put(operationId, INTEGER_ONE);
70+
4871
if (!summary.contains("$"))
4972
operation.setSummary(summary);
50-
operation.setOperationId(operation.getOperationId() + "_" + methodCount++);
73+
operation.setOperationId(operationId);
5174
}
5275
return operation;
5376
}

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app144/SpringDocApp144Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT,
3333
properties = { "management.endpoints.web.exposure.include:*",
34-
"server.port=55555",
34+
"server.port=55554",
3535
"springdoc.use-management-port=true",
3636
"management.server.port=9090",
3737
"management.endpoints.web.base-path=/application" })

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app147/SpringDocApp147Test.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@
3535
"management.server.port=9097",
3636
"management.server.base-path=/test",
3737
"management.endpoints.web.base-path=/application" })
38-
public class SpringDocApp147Test extends AbstractSpringDocActuatorTest {
38+
public class SpringDocApp147Test extends AbstractSpringDocActuatorTest {
3939

4040
@SpringBootApplication
4141
static class SpringDocTestApp {}
4242

4343

4444
@Test
4545
public void testApp() throws Exception {
46-
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL + "/"+Constants.ACTUATOR_DEFAULT_GROUP)
46+
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL + "/" + Constants.ACTUATOR_DEFAULT_GROUP)
4747
.exchange()
4848
.expectStatus().isOk()
4949
.expectBody()
5050
.jsonPath("$.openapi").isEqualTo("3.0.1")
5151
.returnResult();
52-
String result = new String(getResult.getResponseBody());
53-
String expected = getContent("results/app147-1.json");
54-
assertEquals(expected, result, true);
52+
String result = new String(getResult.getResponseBody());
53+
String expected = getContent("results/app147-1.json");
54+
assertEquals(expected, result, true);
5555
}
5656

5757
@Test
5858
public void testApp1() throws Exception {
59-
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL + "/users")
59+
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL + "/users")
6060
.exchange()
6161
.expectStatus().isOk()
6262
.expectBody()
@@ -67,5 +67,4 @@ public void testApp1() throws Exception {
6767
assertEquals(expected, result, true);
6868
}
6969

70-
7170
}

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app148/SpringDocApp148Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ static class SpringDocTestApp {}
4646
public void testApp() throws Exception {
4747
String result =webClient.get().uri("/test/application/openapi/users").retrieve()
4848
.bodyToMono(String.class).block();
49-
5049
String expected = getContent("results/app148-1.json");
5150
assertEquals(expected, result, true);
5251
}

springdoc-openapi-webflux-core/src/test/resources/results/app144.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"servers": [
88
{
9-
"url": "http://localhost:55555",
9+
"url": "http://localhost:55554",
1010
"description": "Generated server url"
1111
}
1212
],

springdoc-openapi-webflux-core/src/test/resources/results/app146-1.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"tags": [
2727
"Actuator"
2828
],
29-
"operationId": "handle_7",
29+
"operationId": "handle_1_6",
3030
"parameters": [
3131
{
3232
"name": "name",
@@ -54,7 +54,7 @@
5454
"tags": [
5555
"Actuator"
5656
],
57-
"operationId": "handle_0",
57+
"operationId": "handle",
5858
"parameters": [
5959
{
6060
"name": "name",
@@ -84,7 +84,7 @@
8484
"tags": [
8585
"Actuator"
8686
],
87-
"operationId": "handle_1_1_2",
87+
"operationId": "handle_1_1",
8888
"responses": {
8989
"200": {
9090
"description": "OK",
@@ -104,7 +104,7 @@
104104
"tags": [
105105
"Actuator"
106106
],
107-
"operationId": "handle_3",
107+
"operationId": "handle_1_2",
108108
"responses": {
109109
"200": {
110110
"description": "OK",
@@ -124,7 +124,7 @@
124124
"tags": [
125125
"Actuator"
126126
],
127-
"operationId": "handle_4",
127+
"operationId": "handle_1_3",
128128
"parameters": [
129129
{
130130
"name": "requiredMetricName",
@@ -154,7 +154,7 @@
154154
"tags": [
155155
"Actuator"
156156
],
157-
"operationId": "handle_5",
157+
"operationId": "handle_1_4",
158158
"responses": {
159159
"200": {
160160
"description": "OK",
@@ -174,7 +174,7 @@
174174
"tags": [
175175
"Actuator"
176176
],
177-
"operationId": "handle_6",
177+
"operationId": "handle_1_5",
178178
"responses": {
179179
"200": {
180180
"description": "OK",
@@ -194,7 +194,7 @@
194194
"tags": [
195195
"Actuator"
196196
],
197-
"operationId": "handle_8",
197+
"operationId": "handle_1_7",
198198
"responses": {
199199
"200": {
200200
"description": "OK",
@@ -214,7 +214,7 @@
214214
"tags": [
215215
"Actuator"
216216
],
217-
"operationId": "handle_9",
217+
"operationId": "handle_1_8",
218218
"responses": {
219219
"200": {
220220
"description": "OK",
@@ -234,7 +234,7 @@
234234
"tags": [
235235
"Actuator"
236236
],
237-
"operationId": "handle_10",
237+
"operationId": "handle_1_9",
238238
"responses": {
239239
"200": {
240240
"description": "OK",
@@ -254,7 +254,7 @@
254254
"tags": [
255255
"Actuator"
256256
],
257-
"operationId": "handle_11",
257+
"operationId": "handle_1_10",
258258
"responses": {
259259
"200": {
260260
"description": "OK",
@@ -274,7 +274,7 @@
274274
"tags": [
275275
"Actuator"
276276
],
277-
"operationId": "handle_12",
277+
"operationId": "handle_1_11",
278278
"parameters": [
279279
{
280280
"name": "toMatch",
@@ -304,7 +304,7 @@
304304
"tags": [
305305
"Actuator"
306306
],
307-
"operationId": "handle_13",
307+
"operationId": "handle_1_12",
308308
"responses": {
309309
"200": {
310310
"description": "OK",
@@ -324,7 +324,7 @@
324324
"tags": [
325325
"Actuator"
326326
],
327-
"operationId": "handle_14",
327+
"operationId": "handle_1_13",
328328
"parameters": [
329329
{
330330
"name": "prefix",
@@ -354,7 +354,7 @@
354354
"tags": [
355355
"Actuator"
356356
],
357-
"operationId": "handle_15",
357+
"operationId": "handle_1_14",
358358
"responses": {
359359
"200": {
360360
"description": "OK",
@@ -374,7 +374,7 @@
374374
"tags": [
375375
"Actuator"
376376
],
377-
"operationId": "handle_16",
377+
"operationId": "handle_1_15",
378378
"responses": {
379379
"200": {
380380
"description": "OK",
@@ -394,7 +394,7 @@
394394
"tags": [
395395
"Actuator"
396396
],
397-
"operationId": "handle_17",
397+
"operationId": "handle_1_16",
398398
"parameters": [
399399
{
400400
"name": "cache",
@@ -422,7 +422,7 @@
422422
"tags": [
423423
"Actuator"
424424
],
425-
"operationId": "handle_22",
425+
"operationId": "handle_1_20",
426426
"parameters": [
427427
{
428428
"name": "cache",
@@ -452,7 +452,7 @@
452452
"tags": [
453453
"Actuator"
454454
],
455-
"operationId": "handle_18",
455+
"operationId": "handle_1_17",
456456
"responses": {
457457
"200": {
458458
"description": "OK",
@@ -470,7 +470,7 @@
470470
"tags": [
471471
"Actuator"
472472
],
473-
"operationId": "handle_21",
473+
"operationId": "handle_1_19",
474474
"responses": {
475475
"200": {
476476
"description": "OK",
@@ -490,7 +490,7 @@
490490
"tags": [
491491
"Actuator"
492492
],
493-
"operationId": "handle_19",
493+
"operationId": "handle_1_18",
494494
"responses": {
495495
"200": {
496496
"description": "OK",
@@ -511,7 +511,7 @@
511511
"Actuator"
512512
],
513513
"summary": "Actuator root web endpoint",
514-
"operationId": "links_20",
514+
"operationId": "links",
515515
"responses": {
516516
"200": {
517517
"description": "OK",

0 commit comments

Comments
 (0)