@@ -52,19 +52,28 @@ public class OpenApiValidator {
52
52
* The default construct will try to load openapi.yml file from classpath
53
53
*/
54
54
public OpenApiValidator () {
55
+ InputStream in = this .getClass ().getClassLoader ().getResourceAsStream (OPENAPI_YAML_CONFIG );;
55
56
try {
56
- InputStream in = this .getClass ().getClassLoader ().getResourceAsStream (OPENAPI_YAML_CONFIG );
57
57
if (in == null ) {
58
58
in = this .getClass ().getClassLoader ().getResourceAsStream (OPENAPI_YML_CONFIG );
59
59
if (in ==null ) {
60
- throw new IOException ("cannot load openapi spec file" );
60
+ throw new IOException ("Cannot load openapi spec file" );
61
61
}
62
62
}
63
63
spec = new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 )).lines ().collect (Collectors .joining ("\n " ));
64
64
openApiHelper = new OpenApiHelper (spec );
65
65
schemaValidator = new SchemaValidator (openApiHelper .openApi3 );
66
- } catch (Exception e ) {
67
- logger .error ("initial failed:" + e );
66
+ } catch (IOException e ) {
67
+ logger .error ("Initial failed:" + e );
68
+ }
69
+ finally {
70
+ try {
71
+ if ( in !=null ) {
72
+ in .close ();
73
+ }
74
+ } catch (IOException e ) {
75
+ logger .error (" Failed to close input stream:" + e );
76
+ }
68
77
}
69
78
}
70
79
@@ -74,10 +83,14 @@ public OpenApiValidator() {
74
83
* @param openapiPath The schema file name and path to use when validating request bodies
75
84
*/
76
85
public OpenApiValidator (String openapiPath ) {
77
- InputStream in = this .getClass ().getClassLoader ().getResourceAsStream (openapiPath );
78
- spec = new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 )).lines ().collect (Collectors .joining ("\n " ));
79
- openApiHelper = new OpenApiHelper (spec );
80
- schemaValidator = new SchemaValidator (openApiHelper .openApi3 );
86
+ try (InputStream in = this .getClass ().getClassLoader ().getResourceAsStream (openapiPath );
87
+ BufferedReader reader = new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 ))) {
88
+ spec = reader .lines ().collect (Collectors .joining ("\n " ));
89
+ openApiHelper = new OpenApiHelper (spec );
90
+ schemaValidator = new SchemaValidator (openApiHelper .openApi3 );
91
+ } catch (IOException e ) {
92
+ logger .error ("initial failed:" + e );
93
+ }
81
94
}
82
95
83
96
/**
0 commit comments