@@ -64,10 +64,8 @@ public List<RequestWithMetadata> parseOpenAPI(OpenAPI openAPI) {
64
64
65
65
for (Server server : openAPI .getServers ()) {
66
66
String serverUrl = server .getUrl ();
67
- // Set a default protocol and host if they are missing
68
- // which can occur when loading files locally
69
67
if (!serverUrl .startsWith ("http://" ) && !serverUrl .startsWith ("https://" )) {
70
- serverUrl = "https://example.com" + serverUrl ; // Use the appropriate default protocol and host
68
+ serverUrl = "https://example.com" + serverUrl ;
71
69
}
72
70
final String finalServerUrl = serverUrl ;
73
71
@@ -83,23 +81,17 @@ public List<RequestWithMetadata> parseOpenAPI(OpenAPI openAPI) {
83
81
84
82
operationMap .forEach ((method , operation ) -> {
85
83
if (operation != null ) {
86
- StringJoiner stringJoiner = new StringJoiner (", " );
87
84
List <Parameter > parameters = operation .getParameters ();
88
-
89
- if (operation .getParameters () != null )
90
- parameters .forEach (parameter -> stringJoiner .add (parameter .getName ()));
91
-
92
85
RequestBody requestBody = operation .getRequestBody ();
93
86
94
87
try {
95
- URI fullUri = constructFullRequestUri ( new URI (finalServerUrl ), pathItem .getKey ());
88
+ URI baseUrl = new URIBuilder (finalServerUrl ). setPath ( pathItem .getKey ()). build ( );
96
89
97
- HttpService httpService = HttpService .httpService (fullUri .getHost (), fullUri .getPort (),
98
- fullUri .getPort () == 443 );
99
- List <HttpHeader > httpHeaders = constructHttp2RequestHeaders (method , fullUri , requestBody ,
90
+ HttpService httpService = HttpService .httpService (baseUrl .toString ());
91
+ List <HttpHeader > httpHeaders = buildHttp2RequestHeaders (method , baseUrl , requestBody ,
100
92
operation .getResponses ());
101
- List <HttpParameter > httpParameters = constructHttpRequestParameters (parameters ,
102
- requestBody , openAPI .getComponents ().getSchemas ());
93
+ List <HttpParameter > httpParameters = buildHttpRequestParameters (parameters , requestBody ,
94
+ openAPI .getComponents ().getSchemas ());
103
95
104
96
HttpRequest httpRequest = HttpRequest .http2Request (
105
97
httpService ,
@@ -113,8 +105,7 @@ public List<RequestWithMetadata> parseOpenAPI(OpenAPI openAPI) {
113
105
.httpHeader ("content-length" , String .valueOf (contentLength )));
114
106
115
107
logEntries .add (
116
- createLogEntry (httpRequest , stringJoiner .toString (),
117
- operation .getDescription ()));
108
+ createLogEntry (httpRequest , operation .getDescription ()));
118
109
} catch (URISyntaxException e ) {
119
110
throw new RuntimeException (e );
120
111
}
@@ -138,7 +129,7 @@ private String parseAccept(ApiResponses apiResponses) {
138
129
return stringJoiner .toString ();
139
130
}
140
131
141
- private List <HttpParameter > constructHttpRequestParameters (List <Parameter > parameters , RequestBody requestBody ,
132
+ private List <HttpParameter > buildHttpRequestParameters (List <Parameter > parameters , RequestBody requestBody ,
142
133
Map <String , Schema > schemas ) {
143
134
List <HttpParameter > httpParameters = new ArrayList <>();
144
135
@@ -153,7 +144,6 @@ else if ("query".equals(in))
153
144
}
154
145
}
155
146
156
- // Add request body parameters
157
147
if (requestBody != null ) {
158
148
MediaType mediaType = requestBody .getContent ().entrySet ().stream ().findFirst ().get ().getValue ();
159
149
@@ -181,7 +171,7 @@ else if ("query".equals(in))
181
171
return httpParameters ;
182
172
}
183
173
184
- private List <HttpHeader > constructHttp2RequestHeaders (String method , URI uri , RequestBody requestBody ,
174
+ private List <HttpHeader > buildHttp2RequestHeaders (String method , URI uri , RequestBody requestBody ,
185
175
ApiResponses apiResponses ) {
186
176
List <HttpHeader > httpHeaders = new ArrayList <>();
187
177
@@ -190,12 +180,10 @@ private List<HttpHeader> constructHttp2RequestHeaders(String method, URI uri, Re
190
180
httpHeaders .add (HttpHeader .httpHeader (":path" , uri .getPath ()));
191
181
httpHeaders .add (HttpHeader .httpHeader (":authority" , uri .getHost ()));
192
182
193
- // Set Accept header
194
183
String acceptHeaderValue = parseAccept (apiResponses );
195
184
if (!acceptHeaderValue .isEmpty ())
196
185
httpHeaders .add (HttpHeader .httpHeader ("accept" , acceptHeaderValue ));
197
186
198
- // Set Content-Type header
199
187
if (requestBody != null && requestBody .getContent () != null ) {
200
188
Optional <String > contentType = requestBody .getContent ().keySet ().stream ().findFirst ();
201
189
contentType .ifPresent (value -> httpHeaders .add (HttpHeader .httpHeader ("content-type" , value )));
@@ -204,27 +192,7 @@ private List<HttpHeader> constructHttp2RequestHeaders(String method, URI uri, Re
204
192
return httpHeaders ;
205
193
}
206
194
207
- private URI constructFullRequestUri (URI baseUri , String path ) throws URISyntaxException {
208
- String basePath = baseUri .getPath ();
209
- if (!basePath .endsWith ("/" ))
210
- basePath += "/" ;
211
-
212
- String formattedPath = path .startsWith ("/" ) ? path .substring (1 ) : path ;
213
- String scheme = baseUri .getScheme ();
214
- int defaultPort = scheme .equals ("http" ) ? 80 : 443 ;
215
- int port = baseUri .getPort () == -1 ? defaultPort : baseUri .getPort ();
216
-
217
- return new URIBuilder ()
218
- .setScheme (scheme )
219
- .setHost (baseUri .getHost ())
220
- .setPort (port )
221
- .setPath (basePath + formattedPath )
222
- .build ();
223
- }
224
-
225
- private RequestWithMetadata createLogEntry (HttpRequest httpRequest , String parameters ,
226
- String description ) {
227
-
195
+ private RequestWithMetadata createLogEntry (HttpRequest httpRequest , String description ) {
228
196
return new RequestWithMetadata (httpRequest , description );
229
197
}
230
198
}
0 commit comments