6
6
import com .mservicetech .openapi .common .ParameterType ;
7
7
import com .mservicetech .openapi .common .RequestEntity ;
8
8
import com .mservicetech .openapi .common .ResponseEntity ;
9
- import com .mservicetech . openapi . common . Status ;
9
+ import com .networknt . config . Config ;
10
10
import com .networknt .jsonoverlay .Overlay ;
11
11
import com .networknt .oas .model .Header ;
12
12
import com .networknt .oas .model .MediaType ;
25
25
import com .networknt .schema .JsonNodePath ;
26
26
import com .networknt .schema .SchemaValidatorsConfig ;
27
27
28
+ import com .networknt .status .Status ;
28
29
import com .networknt .utility .StringUtils ;
29
30
import org .slf4j .Logger ;
30
31
import org .slf4j .LoggerFactory ;
@@ -70,12 +71,19 @@ public class OpenApiValidator {
70
71
* The default construct will try to load openapi.yml file from classpath
71
72
*/
72
73
public OpenApiValidator () {
73
- InputStream in = this . getClass (). getClassLoader (). getResourceAsStream ( OPENAPI_YAML_CONFIG ); ;
74
+ InputStream in = null ;
74
75
try {
76
+ in = this .getClass ().getClassLoader ().getResourceAsStream (OPENAPI_YAML_CONFIG );;
75
77
if (in == null ) {
76
78
in = this .getClass ().getClassLoader ().getResourceAsStream (OPENAPI_YML_CONFIG );
77
79
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
+ }
79
87
}
80
88
}
81
89
spec = new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 )).lines ().collect (Collectors .joining ("\n " ));
@@ -101,13 +109,29 @@ public OpenApiValidator() {
101
109
* @param openapiPath The schema file name and path to use when validating request bodies
102
110
*/
103
111
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 ));
106
122
spec = reader .lines ().collect (Collectors .joining ("\n " ));
107
123
openApiHelper = new OpenApiHelper (spec );
108
124
schemaValidator = new SchemaValidator (openApiHelper .openApi3 );
109
125
} catch (IOException e ) {
110
126
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
+ }
111
135
}
112
136
}
113
137
0 commit comments