66import com .mservicetech .openapi .common .ParameterType ;
77import com .mservicetech .openapi .common .RequestEntity ;
88import com .mservicetech .openapi .common .ResponseEntity ;
9- import com .mservicetech . openapi . common . Status ;
9+ import com .networknt . config . Config ;
1010import com .networknt .jsonoverlay .Overlay ;
1111import com .networknt .oas .model .Header ;
1212import com .networknt .oas .model .MediaType ;
2525import com .networknt .schema .JsonNodePath ;
2626import com .networknt .schema .SchemaValidatorsConfig ;
2727
28+ import com .networknt .status .Status ;
2829import com .networknt .utility .StringUtils ;
2930import org .slf4j .Logger ;
3031import org .slf4j .LoggerFactory ;
@@ -70,12 +71,19 @@ public class OpenApiValidator {
7071 * The default construct will try to load openapi.yml file from classpath
7172 */
7273 public OpenApiValidator () {
73- InputStream in = this . getClass (). getClassLoader (). getResourceAsStream ( OPENAPI_YAML_CONFIG ); ;
74+ InputStream in = null ;
7475 try {
76+ in = this .getClass ().getClassLoader ().getResourceAsStream (OPENAPI_YAML_CONFIG );;
7577 if (in == null ) {
7678 in = this .getClass ().getClassLoader ().getResourceAsStream (OPENAPI_YML_CONFIG );
7779 if (in ==null ) {
78- throw new IOException ("Cannot load openapi spec file" );
80+ in = Config .getInstance ().getInputStreamFromFile (OPENAPI_YAML_CONFIG );
81+ if (in == null ) {
82+ in = Config .getInstance ().getInputStreamFromFile (OPENAPI_YML_CONFIG );
83+ if (in == null ) {
84+ throw new IOException ("Cannot load openapi spec file" );
85+ }
86+ }
7987 }
8088 }
8189 spec = new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 )).lines ().collect (Collectors .joining ("\n " ));
@@ -101,13 +109,29 @@ public OpenApiValidator() {
101109 * @param openapiPath The schema file name and path to use when validating request bodies
102110 */
103111 public OpenApiValidator (String openapiPath ) {
104- try (InputStream in = this .getClass ().getClassLoader ().getResourceAsStream (openapiPath );
105- BufferedReader reader = new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 ))) {
112+ InputStream in = null ;
113+ try {
114+ in = this .getClass ().getClassLoader ().getResourceAsStream (openapiPath );
115+ if (in == null ) {
116+ in = Config .getInstance ().getInputStreamFromFile (openapiPath );
117+ if (in == null ) {
118+ throw new IOException ("Cannot load openapi spec file" );
119+ }
120+ }
121+ BufferedReader reader = new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 ));
106122 spec = reader .lines ().collect (Collectors .joining ("\n " ));
107123 openApiHelper = new OpenApiHelper (spec );
108124 schemaValidator = new SchemaValidator (openApiHelper .openApi3 );
109125 } catch (IOException e ) {
110126 logger .error ("initial failed:" + e );
127+ } finally {
128+ try {
129+ if ( in !=null ) {
130+ in .close ();
131+ }
132+ } catch (IOException e ) {
133+ logger .error (" Failed to close input stream:" + e );
134+ }
111135 }
112136 }
113137
0 commit comments