Skip to content

Commit b53406e

Browse files
authored
Merge pull request #26 from mservicetech/issue25
fixes #25
2 parents 6a56737 + f3b6a15 commit b53406e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<modelVersion>4.0.0</modelVersion>
2020
<groupId>com.mservicetech</groupId>
2121
<artifactId>openapi-schema-validation</artifactId>
22-
<version>2.0.6</version>
22+
<version>2.0.7</version>
2323
<packaging>jar</packaging>
2424
<description>openapi schema for openpai 3.*</description>
2525
<url>https://github.com/mservicetech/openapi-schema-validation</url>

src/main/java/com/mservicetech/openapi/validation/OpenApiValidator.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,28 @@ public class OpenApiValidator {
5252
* The default construct will try to load openapi.yml file from classpath
5353
*/
5454
public OpenApiValidator() {
55+
InputStream in = this.getClass().getClassLoader().getResourceAsStream(OPENAPI_YAML_CONFIG);;
5556
try {
56-
InputStream in = this.getClass().getClassLoader().getResourceAsStream(OPENAPI_YAML_CONFIG);
5757
if (in == null) {
5858
in = this.getClass().getClassLoader().getResourceAsStream(OPENAPI_YML_CONFIG);
5959
if (in==null) {
60-
throw new IOException("cannot load openapi spec file");
60+
throw new IOException("Cannot load openapi spec file");
6161
}
6262
}
6363
spec = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
6464
openApiHelper = new OpenApiHelper(spec);
6565
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+
}
6877
}
6978
}
7079

@@ -74,10 +83,14 @@ public OpenApiValidator() {
7483
* @param openapiPath The schema file name and path to use when validating request bodies
7584
*/
7685
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+
}
8194
}
8295

8396
/**

0 commit comments

Comments
 (0)