44import com .fasterxml .jackson .databind .node .ArrayNode ;
55
66import java .util .Map ;
7+ import java .util .Optional ;
8+ import java .util .Set ;
79
810public class OpenAPIReader {
911
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+
1017 public static String getHost (JsonNode jsonNode ) {
1118 JsonNode host = jsonNode .get ("host" );
1219 if (host == null ) {
@@ -19,23 +26,21 @@ public static String getHost(JsonNode jsonNode) {
1926 private static String getScheme (JsonNode jsonNode ) {
2027 ArrayNode array = jsonNode .withArrayProperty ("schemes" );
2128 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 ;
2431 }
25- // TODO: should the http be the default scheme?
26- return "http" ;
32+ return DEFAULT_SCHEME ;
2733 }
2834
29- public static JsonNode readOperation (JsonNode jsonNode , String operationId ) {
35+ public static Optional < JsonNode > readOperation (JsonNode jsonNode , String operationId ) {
3036 JsonNode paths = jsonNode .get ("paths" );
3137 for (Map .Entry <String , JsonNode > entry : paths .properties ()) {
3238 for (Map .Entry <String , JsonNode > httpMethod : entry .getValue ().properties ()) {
3339 if (httpMethod .getValue ().get ("operationId" ).asText ().equals (operationId )) {
34- return httpMethod .getValue ();
40+ return Optional . ofNullable ( httpMethod .getValue () );
3541 }
3642 }
3743 }
38-
39- return null ;
44+ return Optional .empty ();
4045 }
4146}
0 commit comments