Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<artifactId>json-schema-validator</artifactId>
<version>${json-schema-validator-version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions src/it/multiple-validation-sets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
<include>src/main/resources/malformed.yml</include>
</includes>
</validationSet>
<validationSet>
<jsonSchema>https://swagger.io/v2/schema.json</jsonSchema>
<includes>
<include>src/main/resources/swagger-editor-example.yml</include>
</includes>
</validationSet>
</validationSets>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.github.sylvainlaurent.maven.yamljsonvalidator;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use wildcard imports.
You can read more about it here: https://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad

import java.net.URI;
import java.net.URISyntaxException;

/**
* This mojo validates YAML and JSON files for well-formedness. If JSON schema is provided, it also
Expand Down Expand Up @@ -111,23 +114,39 @@ public void execute() throws MojoExecutionException {

InputStream openJsonSchema(String jsonSchemaFile) throws MojoExecutionException {
if (jsonSchemaFile != null && jsonSchemaFile.length() > 0) {
File file = new File(jsonSchemaFile);
if (file.isFile()) {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new MojoExecutionException("Could not load schema file ["+ jsonSchemaFile + "]", e);
}
} else {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(jsonSchemaFile);
if (inputStream != null) {
return inputStream;
try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you split up these into multiple, smaller methods? It's quite hard to read it this way.

URI uri = new URI(jsonSchemaFile);
if(uri.getScheme() == null) {
File file = new File(jsonSchemaFile);
if (file.isFile()) {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new MojoExecutionException("Could not load schema file ["+ jsonSchemaFile + "]", e);
}
} else {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(jsonSchemaFile);
if (inputStream != null) {
return inputStream;
}
throw new MojoExecutionException("Could not load schema neither from filesystem nor classpath ["+ jsonSchemaFile + "]");
} catch (Exception e){
throw new MojoExecutionException("Could not load schema file from classpath ["+ jsonSchemaFile + "]", e);
}
}
} else {
try (final CloseableHttpClient httpclient = HttpClients.custom().useSystemProperties().build()) {
HttpGet get = new HttpGet();
get.setURI(uri);
try (CloseableHttpResponse response = httpclient.execute(get)) {
return response.getEntity().getContent();
}
}
throw new MojoExecutionException("Could not load schema neither from filesystem nor classpath ["+ jsonSchemaFile + "]");
} catch (Exception e){
throw new MojoExecutionException("Could not load schema file from classpath ["+ jsonSchemaFile + "]", e);
}
} catch (URISyntaxException | IOException use) {
getLog().warn("An error occurs while reading schema from URI " + jsonSchemaFile);
getLog().debug(use);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public void accept_schema_from_filepath() throws MojoExecutionException {
assertNotNull(validateMojo.openJsonSchema("src/test/resources/schema-with-ref.json"));
}

@Test
public void accept_schema_from_http() throws MojoExecutionException {
ValidateMojo validateMojo = new ValidateMojo();
assertNotNull(validateMojo.openJsonSchema("https://json.schemastore.org/geojson.json"));
}


@Test
public void accept_schema_from_classpath() throws MojoExecutionException {
Expand Down