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

Issue 526 reproduction #527

Closed
wants to merge 2 commits into from

Conversation

davidpricedev
Copy link

@davidpricedev davidpricedev commented Feb 28, 2022

This reproduces the issue described in #526.

I'd expect the example json document to validate against the bundled schema, but it fails with http errors trying to load example.com instead.

15:06:40.995 [main] ERROR c.networknt.schema.JsonSchemaFactory - Failed to load json schema!
java.net.UnknownHostException: schemas.example.com
...

"description": "The example phone numbers",
"type": "array",
"items": {
"$ref": "https://schemas.example.com/example-service/objects/1.0.0/phone.schema.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

For refs that are inside the same file, we use it like this:
"$ref": "#/definitions/Policy"

with the definition looking like:

"definitions": {
    "Policy": {
      "type": "object",
      "properties": {
        "id": {
      ...

The # is used for stuff relative to the same file.

The full file is here.

If you want to use ids that are not real urls, you could implement an URI factory like:

static class CloudEventURIFactory implements URIFactory {

        private static final String baseUrl = "https://console.redhat.com/api";
        private final URLFactory urlFactory = new URLFactory();
        private final String base;

        CloudEventURIFactory() {
            String fullPath = RHELSystem.class.getResource(schemaPath).toString();
            base = fullPath.substring(0, fullPath.length() - schemaPath.length());
        }

        @Override
        public URI create(String uri) {
            return urlFactory.create(replaceBase(uri));
        }

        @Override
        public URI create(URI baseURI, String segment) {
            return urlFactory.create(URI.create(replaceBase(baseURI.toString())), segment);
        }

        private String replaceBase(String uri) {
            if (uri.startsWith(baseUrl)) {
                uri = base + uri.substring(baseUrl.length(), uri.length() - 1);
            }

            return uri;
        }
    }

and adding it to your JsonSchemaFactory:

JsonSchemaFactory.Builder().
                .uriFactory(new CloudEventURIFactory(), "https")
                .build();

Full example here: https://github.com/RedHatInsights/event-schemas-java/blob/main/src/main/java/com/redhat/cloud/event/parser/ConsoleCloudEventParser.java#L35C15-L63

I hope that above information is useful, I didn't go a lot into the details.

@stevehu
Copy link
Contributor

stevehu commented Feb 1, 2024

Issue #526 has been resolved. Thanks.

@stevehu stevehu closed this Feb 1, 2024
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 this pull request may close these issues.

3 participants