4
4
import com .fasterxml .jackson .databind .node .ArrayNode ;
5
5
6
6
import java .util .Map ;
7
+ import java .util .Optional ;
8
+ import java .util .Set ;
7
9
8
10
public class OpenAPIReader {
9
11
12
+ private static final String HTTPS = "https" ;
13
+ private static final String HTTP = "http" ;
14
+ private static final String DEFAULT_SCHEME = HTTPS ;
15
+ private static final Set <String > ALLOWED_SCHEMES = Set .of (HTTPS , HTTP );
16
+
10
17
public static String getHost (JsonNode jsonNode ) {
11
18
JsonNode host = jsonNode .get ("host" );
12
19
if (host == null ) {
@@ -19,23 +26,21 @@ public static String getHost(JsonNode jsonNode) {
19
26
private static String getScheme (JsonNode jsonNode ) {
20
27
ArrayNode array = jsonNode .withArrayProperty ("schemes" );
21
28
if (array != null && !array .isEmpty ()) {
22
- // TODO: should get the first scheme?
23
- return array . get ( 0 ). asText () ;
29
+ String firstScheme = array . get ( 0 ). asText ();
30
+ return ALLOWED_SCHEMES . contains ( firstScheme ) ? firstScheme : DEFAULT_SCHEME ;
24
31
}
25
- // TODO: should the http be the default scheme?
26
- return "http" ;
32
+ return DEFAULT_SCHEME ;
27
33
}
28
34
29
- public static JsonNode readOperation (JsonNode jsonNode , String operationId ) {
35
+ public static Optional < JsonNode > readOperation (JsonNode jsonNode , String operationId ) {
30
36
JsonNode paths = jsonNode .get ("paths" );
31
37
for (Map .Entry <String , JsonNode > entry : paths .properties ()) {
32
38
for (Map .Entry <String , JsonNode > httpMethod : entry .getValue ().properties ()) {
33
39
if (httpMethod .getValue ().get ("operationId" ).asText ().equals (operationId )) {
34
- return httpMethod .getValue ();
40
+ return Optional . ofNullable ( httpMethod .getValue () );
35
41
}
36
42
}
37
43
}
38
-
39
- return null ;
44
+ return Optional .empty ();
40
45
}
41
46
}
0 commit comments