29
29
import com .tencent .polaris .api .plugin .server .InterfaceDescriptor ;
30
30
import com .tencent .polaris .api .plugin .server .ReportServiceContractRequest ;
31
31
import com .tencent .polaris .api .plugin .server .ReportServiceContractResponse ;
32
- import io .swagger .models .HttpMethod ;
33
- import io .swagger .models .Operation ;
34
- import io .swagger .models .Path ;
35
- import io .swagger .models .Swagger ;
32
+ import io .swagger .v3 . oas . models .OpenAPI ;
33
+ import io .swagger .v3 . oas . models .Operation ;
34
+ import io .swagger .v3 . oas . models .PathItem ;
35
+ import io .swagger .v3 . oas . models .Paths ;
36
36
import org .slf4j .Logger ;
37
37
import org .slf4j .LoggerFactory ;
38
- import springfox .documentation .service .Documentation ;
39
- import springfox .documentation .spring .web .DocumentationCache ;
40
- import springfox .documentation .swagger2 .mappers .ServiceModelToSwagger2Mapper ;
38
+ import org .springdoc .api .AbstractOpenApiResource ;
39
+ import org .springdoc .api .AbstractOpenApiResourceUtil ;
40
+ import org .springdoc .webflux .api .OpenApiWebFluxUtil ;
41
+ import org .springdoc .webmvc .api .OpenApiWebMvcUtil ;
41
42
42
43
import org .springframework .boot .context .event .ApplicationReadyEvent ;
43
44
import org .springframework .context .ApplicationListener ;
52
53
public class PolarisContractReporter implements ApplicationListener <ApplicationReadyEvent > {
53
54
54
55
private final Logger LOG = LoggerFactory .getLogger (PolarisContractReporter .class );
55
- private final ServiceModelToSwagger2Mapper swagger2Mapper ;
56
- private final DocumentationCache documentationCache ;
56
+
57
+ private final org .springdoc .webmvc .api .MultipleOpenApiResource multipleOpenApiWebMvcResource ;
58
+ private final org .springdoc .webflux .api .MultipleOpenApiResource multipleOpenApiWebFluxResource ;
57
59
private final PolarisContractProperties polarisContractProperties ;
58
60
59
61
private final ProviderAPI providerAPI ;
60
62
61
63
private final PolarisDiscoveryProperties polarisDiscoveryProperties ;
62
64
63
- public PolarisContractReporter (DocumentationCache documentationCache , ServiceModelToSwagger2Mapper swagger2Mapper ,
64
- PolarisContractProperties polarisContractProperties , ProviderAPI providerAPI , PolarisDiscoveryProperties polarisDiscoveryProperties ) {
65
- this .swagger2Mapper = swagger2Mapper ;
66
- this .documentationCache = documentationCache ;
65
+ public PolarisContractReporter (org .springdoc .webmvc .api .MultipleOpenApiResource multipleOpenApiWebMvcResource ,
66
+ org .springdoc .webflux .api .MultipleOpenApiResource multipleOpenApiWebFluxResource ,
67
+ PolarisContractProperties polarisContractProperties , ProviderAPI providerAPI ,
68
+ PolarisDiscoveryProperties polarisDiscoveryProperties ) {
69
+ this .multipleOpenApiWebMvcResource = multipleOpenApiWebMvcResource ;
70
+ this .multipleOpenApiWebFluxResource = multipleOpenApiWebFluxResource ;
67
71
this .polarisContractProperties = polarisContractProperties ;
68
72
this .providerAPI = providerAPI ;
69
73
this .polarisDiscoveryProperties = polarisDiscoveryProperties ;
@@ -73,29 +77,37 @@ public PolarisContractReporter(DocumentationCache documentationCache, ServiceMod
73
77
public void onApplicationEvent (@ NonNull ApplicationReadyEvent applicationReadyEvent ) {
74
78
if (polarisContractProperties .isReportEnabled ()) {
75
79
try {
76
- Documentation documentation = documentationCache .documentationByGroup (polarisContractProperties .getGroup ());
77
- Swagger swagger = swagger2Mapper .mapDocumentation (documentation );
78
- if (swagger != null ) {
80
+ AbstractOpenApiResource openApiResource = null ;
81
+ if (multipleOpenApiWebMvcResource != null ) {
82
+ openApiResource = OpenApiWebMvcUtil .getOpenApiResourceOrThrow (multipleOpenApiWebMvcResource , polarisContractProperties .getGroup ());
83
+ }
84
+ else if (multipleOpenApiWebFluxResource != null ) {
85
+ openApiResource = OpenApiWebFluxUtil .getOpenApiResourceOrThrow (multipleOpenApiWebFluxResource , polarisContractProperties .getGroup ());
86
+ }
87
+ OpenAPI openAPI = null ;
88
+ if (openApiResource != null ) {
89
+ openAPI = AbstractOpenApiResourceUtil .getOpenApi (openApiResource );
90
+ }
91
+ if (openAPI != null ) {
79
92
ReportServiceContractRequest request = new ReportServiceContractRequest ();
80
93
request .setName (polarisDiscoveryProperties .getService ());
81
94
request .setNamespace (polarisDiscoveryProperties .getNamespace ());
82
95
request .setService (polarisDiscoveryProperties .getService ());
83
96
request .setProtocol ("http" );
84
97
request .setVersion (polarisDiscoveryProperties .getVersion ());
85
- List <InterfaceDescriptor > interfaceDescriptorList = getInterfaceDescriptorFromSwagger (swagger );
98
+ List <InterfaceDescriptor > interfaceDescriptorList = getInterfaceDescriptorFromSwagger (openAPI );
86
99
request .setInterfaceDescriptors (interfaceDescriptorList );
87
100
ReportServiceContractResponse response = providerAPI .reportServiceContract (request );
88
101
LOG .info ("Service contract [Namespace: {}. Name: {}. Service: {}. Protocol:{}. Version: {}. API counter: {}] is reported." ,
89
102
request .getNamespace (), request .getName (), request .getService (), request .getProtocol (),
90
103
request .getVersion (), request .getInterfaceDescriptors ().size ());
91
104
if (LOG .isDebugEnabled ()) {
92
- String jsonValue = JacksonUtils .serialize2Json (swagger );
105
+ String jsonValue = JacksonUtils .serialize2Json (openAPI );
93
106
LOG .debug ("OpenApi json data: {}" , jsonValue );
94
107
}
95
108
}
96
109
else {
97
- LOG .warn ("Swagger or json is null, documentationCache keys:{}, group:{}" , documentationCache .all ()
98
- .keySet (), polarisContractProperties .getGroup ());
110
+ LOG .warn ("OpenAPI or json is null, group:{}" , polarisContractProperties .getGroup ());
99
111
}
100
112
}
101
113
catch (Throwable t ) {
@@ -104,11 +116,11 @@ public void onApplicationEvent(@NonNull ApplicationReadyEvent applicationReadyEv
104
116
}
105
117
}
106
118
107
- private List <InterfaceDescriptor > getInterfaceDescriptorFromSwagger (Swagger swagger ) {
119
+ private List <InterfaceDescriptor > getInterfaceDescriptorFromSwagger (OpenAPI openAPI ) {
108
120
List <InterfaceDescriptor > interfaceDescriptorList = new ArrayList <>();
109
- Map < String , Path > paths = swagger .getPaths ();
110
- for (Map .Entry <String , Path > p : paths .entrySet ()) {
111
- Path path = p .getValue ();
121
+ Paths paths = openAPI .getPaths ();
122
+ for (Map .Entry <String , PathItem > p : paths .entrySet ()) {
123
+ PathItem path = p .getValue ();
112
124
Map <String , Operation > operationMap = getOperationMapFromPath (path );
113
125
if (CollectionUtils .isEmpty (operationMap )) {
114
126
continue ;
@@ -124,29 +136,29 @@ private List<InterfaceDescriptor> getInterfaceDescriptorFromSwagger(Swagger swag
124
136
return interfaceDescriptorList ;
125
137
}
126
138
127
- private Map <String , Operation > getOperationMapFromPath (Path path ) {
139
+ private Map <String , Operation > getOperationMapFromPath (PathItem path ) {
128
140
Map <String , Operation > operationMap = new HashMap <>();
129
141
130
142
if (path .getGet () != null ) {
131
- operationMap .put (HttpMethod .GET .name (), path .getGet ());
143
+ operationMap .put (PathItem . HttpMethod .GET .name (), path .getGet ());
132
144
}
133
145
if (path .getPut () != null ) {
134
- operationMap .put (HttpMethod .PUT .name (), path .getPut ());
146
+ operationMap .put (PathItem . HttpMethod .PUT .name (), path .getPut ());
135
147
}
136
148
if (path .getPost () != null ) {
137
- operationMap .put (HttpMethod .POST .name (), path .getPost ());
149
+ operationMap .put (PathItem . HttpMethod .POST .name (), path .getPost ());
138
150
}
139
151
if (path .getHead () != null ) {
140
- operationMap .put (HttpMethod .HEAD .name (), path .getHead ());
152
+ operationMap .put (PathItem . HttpMethod .HEAD .name (), path .getHead ());
141
153
}
142
154
if (path .getDelete () != null ) {
143
- operationMap .put (HttpMethod .DELETE .name (), path .getDelete ());
155
+ operationMap .put (PathItem . HttpMethod .DELETE .name (), path .getDelete ());
144
156
}
145
157
if (path .getPatch () != null ) {
146
- operationMap .put (HttpMethod .PATCH .name (), path .getPatch ());
158
+ operationMap .put (PathItem . HttpMethod .PATCH .name (), path .getPatch ());
147
159
}
148
160
if (path .getOptions () != null ) {
149
- operationMap .put (HttpMethod .OPTIONS .name (), path .getOptions ());
161
+ operationMap .put (PathItem . HttpMethod .OPTIONS .name (), path .getOptions ());
150
162
}
151
163
152
164
return operationMap ;
0 commit comments