22
22
23
23
package org .springdoc .core .customizers ;
24
24
25
+ import java .lang .reflect .Field ;
26
+ import java .lang .reflect .Parameter ;
25
27
import java .util .HashMap ;
26
28
import java .util .regex .Matcher ;
27
29
import java .util .regex .Pattern ;
28
30
31
+ import io .swagger .v3 .core .util .AnnotationsUtils ;
29
32
import io .swagger .v3 .oas .models .Operation ;
33
+ import io .swagger .v3 .oas .models .media .Content ;
34
+ import io .swagger .v3 .oas .models .media .MediaType ;
35
+ import io .swagger .v3 .oas .models .media .Schema ;
36
+ import io .swagger .v3 .oas .models .parameters .RequestBody ;
37
+ import org .apache .commons .lang3 .reflect .FieldUtils ;
38
+ import org .slf4j .Logger ;
39
+ import org .slf4j .LoggerFactory ;
30
40
41
+ import org .springframework .boot .actuate .endpoint .OperationType ;
42
+ import org .springframework .boot .actuate .endpoint .annotation .AbstractDiscoveredOperation ;
43
+ import org .springframework .boot .actuate .endpoint .annotation .Selector ;
44
+ import org .springframework .boot .actuate .endpoint .invoke .OperationParameter ;
45
+ import org .springframework .boot .actuate .endpoint .invoke .reflect .OperationMethod ;
31
46
import org .springframework .web .method .HandlerMethod ;
32
47
33
48
import static org .apache .commons .lang3 .math .NumberUtils .INTEGER_ONE ;
@@ -44,6 +59,11 @@ public class ActuatorOperationCustomizer implements OperationCustomizer {
44
59
*/
45
60
private HashMap <String , Integer > methodCountMap = new HashMap <>();
46
61
62
+ private static final String OPERATION = "operation" ;
63
+
64
+ private static final String PARAMETER = "parameter" ;
65
+
66
+ private static final Logger LOGGER = LoggerFactory .getLogger (ActuatorOperationCustomizer .class );
47
67
48
68
/**
49
69
* The regex pattern for operationId lookup.
@@ -53,14 +73,39 @@ public class ActuatorOperationCustomizer implements OperationCustomizer {
53
73
@ Override
54
74
public Operation customize (Operation operation , HandlerMethod handlerMethod ) {
55
75
if (operation .getTags () != null && operation .getTags ().contains (getTag ().getName ())) {
76
+ Field operationFiled = FieldUtils .getDeclaredField (handlerMethod .getBean ().getClass (), OPERATION , true );
77
+ Object actuatorOperation ;
78
+ if (operationFiled != null ) {
79
+ try {
80
+ actuatorOperation = operationFiled .get (handlerMethod .getBean ());
81
+ operationFiled = FieldUtils .getDeclaredField (actuatorOperation .getClass (), OPERATION , true );
82
+ AbstractDiscoveredOperation discoveredOperation = (AbstractDiscoveredOperation ) operationFiled .get (actuatorOperation );
83
+ OperationMethod operationMethod = discoveredOperation .getOperationMethod ();
84
+ if (OperationType .WRITE .equals (operationMethod .getOperationType ())) {
85
+ for (OperationParameter operationParameter : operationMethod .getParameters ()) {
86
+ Field parameterField = FieldUtils .getDeclaredField (operationParameter .getClass (), PARAMETER , true );
87
+ Parameter parameter = (Parameter ) parameterField .get (operationParameter );
88
+ Schema <?> schema = AnnotationsUtils .resolveSchemaFromType (parameter .getType (), null , null );
89
+ if (parameter .getAnnotation (Selector .class ) == null ) {
90
+ operation .setRequestBody (new RequestBody ()
91
+ .content (new Content ().addMediaType (org .springframework .http .MediaType .APPLICATION_JSON_VALUE , new MediaType ().schema (schema ))));
92
+ }
93
+ }
94
+ }
95
+ }
96
+ catch (IllegalAccessException e ) {
97
+ LOGGER .warn (e .getMessage ());
98
+ }
99
+ }
100
+
56
101
String summary = handlerMethod .toString ();
57
102
Matcher matcher = pattern .matcher (summary );
58
103
String operationId = operation .getOperationId ();
59
104
while (matcher .find ()) {
60
105
operationId = matcher .group (1 );
61
106
}
62
107
if (methodCountMap .containsKey (operationId )) {
63
- Integer methodCount = methodCountMap .get (operationId )+ 1 ;
108
+ Integer methodCount = methodCountMap .get (operationId ) + 1 ;
64
109
methodCountMap .put (operationId , methodCount );
65
110
operationId = operationId + "_" + methodCount ;
66
111
}
0 commit comments