Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading a json schema from classpath. Fail to resolve $ref #854

Closed
avishaybp81 opened this issue Aug 28, 2023 · 1 comment · Fixed by #931
Closed

Loading a json schema from classpath. Fail to resolve $ref #854

avishaybp81 opened this issue Aug 28, 2023 · 1 comment · Fixed by #931

Comments

@avishaybp81
Copy link

I load a schema from the classpath.

private JsonSchema getJsonSchemaFromClasspath(String name) {
    JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
    InputStream is = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream(name);
    return factory.getSchema(is);
}

If there is a $ref in the schema that looks like the below

"metadata": {
  "$ref": "request_metadata.json"
}

I fail to load the schema with the error below.
Caused by: com.networknt.schema.JsonSchemaException: #/properties/metadata/$ref: Reference request_metadata.json cannot be resolved

any idea how to solve it?

@takanuva15
Copy link

takanuva15 commented Oct 18, 2023

Hi, I am able to reference other schema files in my resources using the following notation:

"key-to-validate-in-other-schema": {
  "$ref": "classpath:///resources/schemas/model/other-schema.yml"
}

Assuming your schema is in src/test/resources/request_metadata.json, you might be able to just do this for the ref: classpath:///request_metadata.json

Please let me know if that works for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants