json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

fixes #572

Open mahirgamal opened this issue 3 years ago • 1 comments

Hi,

I am trying this code: "$ref": "https://github.com/adewg/ICAR/blob/3340b2041f73afe47079426588bcef1eaf2b004e/enums/icarDepartureKindType.json",

but always getting this error: com.networknt.schema.JsonSchemaException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 7, column: 2] at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:393) at com.networknt.schema.RefValidator.getRefSchema(RefValidator.java:79) at com.networknt.schema.RefValidator.(RefValidator.java:42) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) at com.networknt.schema.ValidatorTypeCode.newValidator(ValidatorTypeCode.java:135) at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:279) at com.networknt.schema.ValidationContext.newValidator(ValidationContext.java:57) at com.networknt.schema.JsonSchema.read(JsonSchema.java:203) at com.networknt.schema.JsonSchema.getValidators(JsonSchema.java:479) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:278) at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:68) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:279) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:262) at json.JsonValidatorExampleBean.ok(JsonValidatorExampleBean.java:80) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.apache.el.parser.AstValue.invoke(AstValue.java:247) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267) at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149) at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:814) at javax.faces.component.UICommand.broadcast(UICommand.java:300) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(Unknown Source) at com.sun.faces.lifecycle.Phase.doPhase(Unknown Source) at com.sun.faces.lifecycle.LifecycleImpl.execute(Unknown Source) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:346) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:887) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1684) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 7, column: 2] at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2337) at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:710) at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:635) at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2691) at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:870) at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:762) at com.fasterxml.jackson.databind.ObjectMapper._readTreeAndClose(ObjectMapper.java:4622) at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:3023) at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:370) ... 55 more

and this is java code:

/** * */ package json;

import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Serializable; import java.util.Set;

import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; import com.networknt.schema.SpecVersion; import com.networknt.schema.ValidationMessage;

// create class to validate JSON document @ManagedBean @RequestScoped public class JsonValidatorExampleBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String message,errorMessage;
private String schema = "{\r\n"
		+ "  \"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\r\n"
		+ "  \"type\": \"object\",\r\n"
		+ "  \"properties\": {\r\n"
		+ "    \"x\": {\r\n"
		+ "      \"type\":\"string\",\r\n"
		+ "      \"$ref\": \"https://github.com/adewg/ICAR/blob/3340b2041f73afe47079426588bcef1eaf2b004e/enums/icarDepartureKindType.json\"\r\n"
		+ "    }\r\n"
		+ "  }\r\n"
		+ "}";
private String data = "{\r\n"
		+ "  \"x\":\"X\"\r\n"
		+ "}";

public void ok() {

	// create instance of the ObjectMapper class
	ObjectMapper objectMapper = new ObjectMapper();

	// create an instance of the JsonSchemaFactory using version flag
	JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);

	// store the JSON data in InputStream
	try {
		// InputStream jsonStream = new FileInputStream( data );
		//InputStream schemaStream = getClass().getResourceAsStream("LEI/LEIEventCore.json");

		// read data from the stream and store it into JsonNode
		JsonNode json = objectMapper.readTree(data);

		// get schema from the schemaStream and store it into JsonSchema
		JsonSchema jschema = schemaFactory.getSchema(schema);

		// create set of validation message and store result in it
		Set<ValidationMessage> validationResult = jschema.validate(json);

		// show the validation errors
		if (validationResult.isEmpty()) {

			// show custom message if there is no validation error
			message = "There is no validation errors";
			errorMessage="";

		} else {
			message="";
			errorMessage = "There are errors\r";
			// show all the validation error
			validationResult.forEach(vm -> errorMessage += vm.getMessage() + "\r");

		}
	} catch (Exception e) {
		e.printStackTrace();
		errorMessage=e.getMessage();
	}
}

public String getMessage() {
	return message;
}

public void setMessage(String message) {
	this.message = message;
}

public String getSchema() {
	return schema;
}

public void setSchema(String schema) {
	this.schema = schema;
}

public String getData() {
	return data;
}

public void setData(String data) {
	this.data = data;
}

public String getErrorMessage() {
	return errorMessage;
}

public void setErrorMessage(String errorMessage) {
	this.errorMessage = errorMessage;
}

}

image

and when I am testing locally it works perfectly:

image

any idea what's wrong?

mahirgamal avatar May 16 '22 23:05 mahirgamal

@mahirgamal We cannot merge the https branch to the master branch. It requires adding a self-signed certificate to the JDK security cacerts with keytool. Without this step, the local build will fail for all the test cases that have https://localhost:1234 references. Let's keep all the https tests in https branch and I will sync the master branch changes to this branch periodically.

stevehu avatar May 17 '22 13:05 stevehu